import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; import { BusinessRuleError, ForbiddenError, } from '@task-stream/api/shared/domain'; import { CommandResult, ack } from './command-result'; import { VoteIssueCommand } from './vote-issue.command'; import { WriteAccess } from './write-access'; /** * Anyone signed in votes on a public project's tickets (that is the point: * the people it is for say what they want); on a private one, its members. */ @CommandHandler(VoteIssueCommand) export class VoteIssueCommandHandler implements ICommandHandler { constructor(private readonly access: WriteAccess) {} async execute(command: VoteIssueCommand): Promise { const issue = await this.access.issue(command.issueId); const project = await this.access.project(issue.projectId); if (!project.isPublic && !project.isMember(command.actorId)) throw new ForbiddenError( `You are not a member of project ${project.key}`, ); project.assertNotArchived(); this.access.assertSees(project, issue, command.actorId); if (issue.trashedAt) throw new BusinessRuleError(`${issue.key} is in the trash`); issue.vote(command.actorId, command.on); await issue.commitAndWait(); return ack(issue); } }