diff --git a/libs/models/budgetting/budget-notes/src/lib/domain/add-note.command.ts b/libs/models/budgetting/budget-notes/src/lib/domain/add-note.command.ts new file mode 100644 index 00000000..f80f0101 --- /dev/null +++ b/libs/models/budgetting/budget-notes/src/lib/domain/add-note.command.ts @@ -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() + ) {} +} diff --git a/libs/models/budgetting/budget-notes/src/lib/domain/add-note.handler.ts b/libs/models/budgetting/budget-notes/src/lib/domain/add-note.handler.ts new file mode 100644 index 00000000..b51276c9 --- /dev/null +++ b/libs/models/budgetting/budget-notes/src/lib/domain/add-note.handler.ts @@ -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 +{ + /** + * 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 { + 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' + }; + } + } +}