import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs'; import { ProjectFactory } from '@task-stream/api/tracker/domain'; import { ProjectKeysRepository } from '../ports/project-keys.repository'; import { CommandResult, ack } from './command-result'; import { CreateProjectCommand } from './create-project.command'; @CommandHandler(CreateProjectCommand) export class CreateProjectCommandHandler implements ICommandHandler { constructor( private readonly projectFactory: ProjectFactory, private readonly projectKeys: ProjectKeysRepository, private readonly eventPublisher: EventPublisher, ) {} async execute(command: CreateProjectCommand): Promise { const project = this.projectFactory.create({ key: command.key, name: command.name, description: command.description, ownerId: command.actorId, }); await this.projectKeys.reserve(project.key, project.id); this.eventPublisher.mergeObjectContext(project); await project.commitAndWait(); return ack(project); } }