import { Injectable } from '@nestjs/common'; import { Comment } from '../../domain/entities/comment.entity.js'; import { CommentDto } from '../dto/comment.dto.js'; import { CommentApplicationMapper } from '../mappers/comment-application.mapper.js'; import { GetUsersUseCase } from '../../../user/application/use-cases/get-users/get-users.use-case.js'; import { GetUsersQuery } from '../../../user/application/use-cases/get-users/get-users.query.js'; import { GetFilesUseCase } from '../../../file/application/use-cases/get-files/get-files.use-case.js'; import { GetFilesQuery } from '../../../file/application/use-cases/get-files/get-files.query.js'; /** Раскрывает автора и вложения комментария (или списка комментариев) */ @Injectable() export class CommentExpanderService { constructor( private readonly getUsersUseCase: GetUsersUseCase, private readonly getFilesUseCase: GetFilesUseCase, ) {} async expand(comment: Comment): Promise { const [dto] = await this.expandAll([comment]); return dto!; } async expandAll(comments: Comment[]): Promise { if (comments.length === 0) return []; const userIds = [...new Set(comments.map((comment) => comment.authorId).filter((id): id is string => Boolean(id)))]; const fileIds = comments.flatMap((comment) => comment.fileIds); const [users, files] = await Promise.all([ userIds.length > 0 ? this.getUsersUseCase.execute(new GetUsersQuery(userIds)) : [], fileIds.length > 0 ? this.getFilesUseCase.execute(new GetFilesQuery(fileIds)) : [], ]); const usersById = new Map(users.map((user) => [user.id, user])); const filesById = new Map(files.map((file) => [file.id, file])); return comments.map((comment) => CommentApplicationMapper.toDto(comment, usersById, filesById)); } }