Files
task-stream/libs/api/tracker/application/src/lib/commands/create-project.command-handler.ts
2026-10-03 13:40:26 +02:00

31 lines
1.1 KiB
TypeScript

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<CreateProjectCommand>
{
constructor(
private readonly projectFactory: ProjectFactory,
private readonly projectKeys: ProjectKeysRepository,
private readonly eventPublisher: EventPublisher,
) {}
async execute(command: CreateProjectCommand): Promise<CommandResult> {
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);
}
}