import { Inject, Injectable } from '@nestjs/common'; import { Issue } from '../../../domain/entities/issue.entity.js'; import { ISSUE_REPOSITORY, type IIssueRepository } from '../../../domain/repositories/issue.repository.interface.js'; import { PROJECT_REPOSITORY, type IProjectRepository, } from '../../../../project/domain/repositories/project.repository.interface.js'; import { CreateIssueCommand } from './create-issue.command.js'; import { IssueWithUsersDto } from '../../dto/issue-with-users.dto.js'; import { IssueEventsService } from '../../services/issue-events.service.js'; import { IssueUserExpanderService } from '../../services/issue-user-expander.service.js'; import { IssueCreatedEvent } from '../../../domain/events/issue-created.event.js'; @Injectable() export class CreateIssueUseCase { constructor( @Inject(ISSUE_REPOSITORY) private readonly issueRepository: IIssueRepository, @Inject(PROJECT_REPOSITORY) private readonly projectRepository: IProjectRepository, private readonly issueEventsService: IssueEventsService, private readonly issueUserExpander: IssueUserExpanderService, ) {} async execute(command: CreateIssueCommand): Promise { const draft = Issue.create({ key: command.key, title: command.title, content: command.content, priority: command.priority, assignee: command.assignee, reporter: command.reporter, projectId: command.projectId, }); const issue = await this.issueRepository.create(draft); const dto = await this.issueUserExpander.expand(issue); if (issue.assignee) { await this.projectRepository.addMember(issue.projectId, issue.assignee); } const domainEvents = draft.pullDomainEvents(); if (domainEvents.some((event) => event instanceof IssueCreatedEvent)) { this.issueEventsService.emitIssueCreated(dto); } return dto; } }