import { Listen } from '@hg-ts/events'; import { BaseAggregate } from '../base.aggregate.js'; import { ExampleCreatedEvent } from './example-created.event.js'; import { ExampleUpdatedEvent } from './example-updated.event.js'; type ExampleAggregateDto = { id: string; name: string; }; export class ExampleAggregate extends BaseAggregate { private name: string; public constructor(dto: ExampleAggregateDto) { super(dto); this.name = dto.name; } public update(newName: string): void { this.produceEvent(new ExampleUpdatedEvent(this.id, newName)); } public toDto(): ExampleAggregateDto { return { id: this.id, name: this.name, }; } public static create(name: string): ExampleAggregate { const aggregate = new ExampleAggregate({ id: crypto.randomUUID(), name }); aggregate.produceEvent(new ExampleCreatedEvent(aggregate.id, name)); return aggregate; } @Listen(ExampleUpdatedEvent) protected onUpdated(event: ExampleUpdatedEvent): void { this.name = event.body.name; } }