import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; import { ForbiddenError } from '@task-stream/api/shared/domain'; import { CommandResult, ack } from './command-result'; import { WatchIssueCommand } from './watch-issue.command'; import { WriteAccess } from './write-access'; /** * Following is for members, viewers included: reading a ticket is enough to * want to hear about it. People only follow or unfollow for themselves. */ @CommandHandler(WatchIssueCommand) export class WatchIssueCommandHandler implements ICommandHandler { constructor(private readonly access: WriteAccess) {} async execute(command: WatchIssueCommand): Promise { const issue = await this.access.issue(command.issueId); const project = await this.access.project(issue.projectId); if (command.actorId !== command.userId) { throw new ForbiddenError('You can only follow tickets yourself'); } if (!project.isMember(command.userId)) { throw new ForbiddenError( `You are not a member of project ${project.key}`, ); } if (command.watching) issue.watch(command.actorId, command.userId); else issue.unwatch(command.actorId, command.userId); await issue.commitAndWait(); return ack(issue); } }