Files
task-stream/libs/api/tracker/application/src/lib/commands/watch-issue.command-handler.ts
Josep Piguillem 88928b402f feat: mentions and following tickets
Mention project members in comments with @, follow and unfollow tickets,
and get told when something changes on the tickets you follow. Commenting
on a ticket starts following it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 09:41:32 +02:00

34 lines
1.3 KiB
TypeScript

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<WatchIssueCommand>
{
constructor(private readonly access: WriteAccess) {}
async execute(command: WatchIssueCommand): Promise<CommandResult> {
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);
}
}