All files / comment comment.service.ts

100% Statements 52/52
100% Branches 10/10
100% Functions 9/9
100% Lines 50/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 1285x 5x 5x   5x         5x 5x 5x 5x         5x 5x     5x   22x 22x 22x               3x 3x 1x     2x   2x 3x     2x   2x           2x   3x   7x                           2x 2x 1x     1x 1x 1x 1x 1x 1x 1x 1x   1x                 1x                     3x 3x 2x     1x   1x 1x       3x 3x 2x     1x   1x 1x      
import { Injectable, NotFoundException } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { CommentQueryRepository } from 'src/comment/comment.query.repository';
import { ApiCommentGetRequestQueryDto } from 'src/comment/dto/api-comment-get-request-query.dto';
import { ApiCommentGetResponseDto } from 'src/comment/dto/api-comment-get-response.dto';
import { ApiCommentPostNotificationResponseDto } from 'src/comment/dto/api-comment-post-notification-response.dto';
import { ApiCommentPostRequestBodyDto } from 'src/comment/dto/api-community-post-request-body.dto';
import { ApiCommentPutRequestBodyDto } from 'src/comment/dto/api-community-put-request-body.dto';
import { UuidResponseDto } from 'src/commons/dtos/uuid-response.dto';
import { isEmpty } from 'src/commons/util/is/is-empty';
import { generateUUID } from 'src/commons/util/uuid';
import { CommunityQueryRepository } from 'src/community/community.query.repository';
import { CommentEntity } from 'src/entities/comment.entity';
import { CommunityEntity } from 'src/entities/community.entity';
import { UserEntity } from 'src/entities/user.entity';
import { NotificationDetailDto } from 'src/notification/dto/notification-detail.dto';
import { UserDto } from 'src/user/dto/user.dto';
import { UserQueryRepository } from 'src/user/user.query.repository';
import { ERROR } from '../commons/constants/error';
 
@Injectable()
export class CommentService {
  constructor(
    private readonly commentQueryRepository: CommentQueryRepository,
    private readonly communityQueryRepository: CommunityQueryRepository,
    private readonly userQueryRepository: UserQueryRepository,
  ) {}
 
  async commentList(
    uuid: string,
    dto: ApiCommentGetRequestQueryDto,
    user: UserDto,
  ): Promise<ApiCommentGetResponseDto> {
    const community: CommunityEntity = await this.communityQueryRepository.findOne(uuid);
    if (isEmpty(community)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    const comments: CommentEntity[] = await this.commentQueryRepository.find(uuid, dto);
 
    const userUuids = Array.from(
      new Set([...comments.map((comment) => comment.user_uuid), community.user_uuid]),
    );
 
    const userList: UserEntity[] = await this.userQueryRepository.findUserList(userUuids);
 
    return plainToInstance(
      ApiCommentGetResponseDto,
      {
        community_review: community.review,
        community_user_uuid: community.user_uuid,
        community_user_name: community.user_name,
        community_user_profile_image: userList.find((u) => u.uuid === community.user_uuid)
          .profile_image,
        comments: comments.map((comment) => ({
          ...comment,
          user_profile_image: userList.find((u) => u.uuid === comment.user_uuid).profile_image,
          isAuthor: comment.user_uuid === user.uuid,
        })),
        last_item_id: comments.length === dto.size ? comments[comments.length - 1].id : 0,
      },
      { excludeExtraneousValues: true },
    );
  }
 
  async commentPost(
    uuid: string,
    user: UserDto,
    dto: ApiCommentPostRequestBodyDto,
  ): Promise<ApiCommentPostNotificationResponseDto> {
    const community: CommunityEntity = await this.communityQueryRepository.findOne(uuid);
    if (isEmpty(community)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    const commentEntity = new CommentEntity();
    commentEntity.uuid = generateUUID();
    commentEntity.user_uuid = user.uuid;
    commentEntity.user_name = user.nickname;
    commentEntity.comment = dto.comment;
    commentEntity.target_uuid = community.uuid;
    commentEntity.target_user_uuid = community.user_uuid;
    await this.commentQueryRepository.save(commentEntity);
 
    const notification: NotificationDetailDto = {
      uuid: generateUUID(),
      user_uuid: user.uuid,
      target_type: 'comment',
      target_uuid: community.uuid,
      target_user_uuid: community.user_uuid,
      content: `회원님의 게시물에 ${user.nickname}님이 한줄평을 남겼어요.`,
    };
 
    return {
      data: { uuid: commentEntity.uuid },
      notification,
    };
  }
 
  async commentUpdate(
    user: UserDto,
    dto: ApiCommentPutRequestBodyDto,
    uuid: string,
  ): Promise<UuidResponseDto> {
    const comment: CommentEntity = await this.commentQueryRepository.findOne(uuid);
    if (isEmpty(comment) || comment.user_uuid !== user.uuid) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    const updatedComment = { ...comment, comment: dto.comment };
 
    await this.commentQueryRepository.save(updatedComment as CommentEntity);
    return { uuid: updatedComment.uuid };
  }
 
  async commentDelete(user: UserDto, uuid: string): Promise<UuidResponseDto> {
    const comment: CommentEntity = await this.commentQueryRepository.findOne(uuid);
    if (isEmpty(comment) || comment.user_uuid !== user.uuid) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    const updatedComment = { ...comment, archived_at: new Date() };
 
    await this.commentQueryRepository.save(updatedComment as CommentEntity);
    return { uuid: updatedComment.uuid };
  }
}