import { Inject, Injectable } from '@nestjs/common'; import { ISSUE_REPOSITORY, type IIssueRepository } from '../../../domain/repositories/issue.repository.interface.js'; import { IssueNotFoundException } from '../../../domain/exceptions/issue-not-found.exception.js'; import { IssueEventsService } from '../../services/issue-events.service.js'; import { IssueApplicationMapper } from '../../mappers/issue-application.mapper.js'; import { UnobserveIssueCommand } from './unobserve-issue.command.js'; /** * Наблюдатель сам отписывается от задачи. Редактировать задачу он не может, * поэтому это отдельная операция, а не часть UpdateIssueUseCase. * Повторный вызов (или вызов не наблюдателем) ничего не меняет. */ @Injectable() export class UnobserveIssueUseCase { constructor( @Inject(ISSUE_REPOSITORY) private readonly issueRepository: IIssueRepository, private readonly issueEventsService: IssueEventsService, ) {} async execute(command: UnobserveIssueCommand): Promise { const issue = await this.issueRepository.findById(command.id, command.userId); if (!issue) { throw new IssueNotFoundException(command.id); } if (!issue.observers.includes(command.userId)) return; issue.removeObserver(command.userId); await this.issueRepository.update(issue, command.userId); this.issueEventsService.emitIssueUpdated(IssueApplicationMapper.toDto(issue)); } }