All files / reaction reaction.service.ts

100% Statements 39/39
100% Branches 8/8
100% Functions 3/3
100% Lines 37/37

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 795x 5x   5x 5x 5x   5x     5x       5x   18x 18x             4x 4x 1x   3x   3x 2x 1x     1x 1x 1x       1x 1x 1x 1x 1x 1x 1x   1x                 1x       3x   3x 3x 1x     2x 1x     1x   1x      
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { ERROR } from 'src/commons/constants/error';
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 { CommunityEntity } from 'src/entities/community.entity';
import { ReactionEntity } from 'src/entities/reaction.entity';
import { NotificationDetailDto } from 'src/notification/dto/notification-detail.dto';
import { ApiReactionPostCommunityResponseDto } from 'src/reaction/dto/api-reaction-post-community-response.dto';
import { ReactionQueryRepository } from 'src/reaction/reaction.query.repository';
import { UserDto } from 'src/user/dto/user.dto';
 
@Injectable()
export class ReactionService {
  constructor(
    private readonly communityQueryRepository: CommunityQueryRepository,
    private readonly reactionQueryRepository: ReactionQueryRepository,
  ) {}
 
  async reactionToCommunity(
    user: UserDto,
    uuid: string,
  ): Promise<ApiReactionPostCommunityResponseDto> {
    const community = await this.communityQueryRepository.findOne(uuid);
    if (isEmpty(community)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
    const reaction = await this.reactionQueryRepository.findOne(uuid, user);
 
    if (reaction) {
      if (reaction.like === 1) {
        throw new ConflictException(ERROR.DUPLICATION);
      }
 
      if (reaction.like === 0) {
        await this.reactionQueryRepository.updateCourseLike(reaction);
        return { data: { uuid }, notification: null };
      }
    }
 
    const newReaction = new ReactionEntity();
    newReaction.uuid = generateUUID();
    newReaction.target_uuid = uuid;
    newReaction.user_uuid = user.uuid;
    newReaction.user_name = user.nickname;
    newReaction.like = 1;
    await this.reactionQueryRepository.courseLike(newReaction);
 
    const notification: NotificationDetailDto = {
      uuid: generateUUID(),
      user_uuid: user.uuid,
      target_type: 'reaction',
      target_uuid: community.uuid,
      target_user_uuid: community.user_uuid,
      content: `회원님의 게시물을 ${user.nickname}님이 좋아합니다.`,
    };
 
    return { data: { uuid }, notification };
  }
 
  async reactionDeleteToCommunity(user: UserDto, uuid: string): Promise<UuidResponseDto> {
    const community: CommunityEntity = await this.communityQueryRepository.findOne(uuid);
 
    const reaction: ReactionEntity = await this.reactionQueryRepository.findOne(uuid, user);
    if (isEmpty(reaction) || isEmpty(community)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    if (reaction.like === 0) {
      throw new ConflictException(ERROR.DUPLICATION);
    }
 
    await this.reactionQueryRepository.updateCourseLikeDelete(reaction);
 
    return { uuid };
  }
}