31 lines
1.1 KiB
TypeScript
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);
|
|
}
|
|
}
|