Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export class AddNoteToBudgetCommand {
constructor(
public readonly budgetId: string,
public readonly noteContent: string,
public readonly authorId: string,
public readonly timestamp: Date = new Date()
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { AddNoteToBudgetCommand } from './add-note.command';
import { FunctionHandler } from '@ngfi/functions';
import { FunctionContext } from '@ngfi/functions';
import { HandlerTools } from '@iote/cqrs';

// Result type
export interface AddNoteToBudgetResult {
success: boolean;
noteId?: string;
error?: string;
}

/**
* Handler for adding notes to budgets.
* Implements the Command pattern in CQRS architecture.
*/
export class AddNoteToBudgetHandler
extends FunctionHandler<AddNoteToBudgetCommand, AddNoteToBudgetResult>
{
/**
* Executes the add note command with validation and error handling
*
* @param command - The command containing budget note data
* @param context - Function execution context with auth info
* @param tools - Handler tools providing repository access
* @returns Result indicating success or failure with noteId
*/
public async execute(
command: AddNoteToBudgetCommand,
context: FunctionContext,
tools: HandlerTools
): Promise<AddNoteToBudgetResult> {
try {
// Validation: Check note content
if (!command.noteContent || command.noteContent.trim() === '') {
return {
success: false,
error: 'Note content cannot be empty'
};
}

// Validation: Check budget ID
if (!command.budgetId) {
return {
success: false,
error: 'Budget ID is required'
};
}

// Get repository from tools (provided by the CQRS framework)
const repository = tools.getRepository();

// Add note to budget via repository
const noteId = await repository.addNote({
budgetId: command.budgetId,
content: command.noteContent,
authorId: command.authorId,
createdAt: command.timestamp
});

return {
success: true,
noteId
};
} catch (error) {
return {
success: false,
error: (error as Error).message || 'Failed to add note to budget'
};
}
}
}