import { Inject, Injectable } from '@nestjs/common'; import { COMMENT_REPOSITORY, type ICommentRepository, } from '../../../domain/repositories/comment.repository.interface.js'; import { ISSUE_REPOSITORY, type IIssueRepository, } from '../../../../issue/domain/repositories/issue.repository.interface.js'; import { IssueNotFoundException } from '../../../../issue/domain/exceptions/issue-not-found.exception.js'; import { Comment } from '../../../domain/entities/comment.entity.js'; import { CreateCommentCommand } from './create-comment.command.js'; import { CommentDto } from '../../dto/comment.dto.js'; import { CommentAuthorExpanderService } from '../../services/comment-author-expander.service.js'; import { CommentEventsService } from '../../services/comment-events.service.js'; @Injectable() export class CreateCommentUseCase { constructor( @Inject(COMMENT_REPOSITORY) private readonly commentRepository: ICommentRepository, @Inject(ISSUE_REPOSITORY) private readonly issueRepository: IIssueRepository, private readonly commentAuthorExpander: CommentAuthorExpanderService, private readonly commentEventsService: CommentEventsService, ) {} async execute(command: CreateCommentCommand): Promise { // Комментировать можно только видимую пользователю задачу const issue = await this.issueRepository.findById(command.issueId, command.authorId); if (!issue) { throw new IssueNotFoundException(command.issueId); } const comment = Comment.create({ issueId: command.issueId, authorId: command.authorId, content: command.content, }); const saved = await this.commentRepository.create(comment); const dto = await this.commentAuthorExpander.expand(saved); this.commentEventsService.emit('comment.created', dto); return dto; } }