All files / community community.service.ts

100% Statements 87/87
100% Branches 15/15
100% Functions 18/18
100% Lines 83/83

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 2205x 5x 5x 5x 5x 5x       5x 5x 5x 5x 5x   5x   5x     5x 5x 5x   5x   5x     5x   30x 30x 30x 30x 30x 30x               3x 3x 1x     2x 2x 1x     1x 1x 1x 1x 1x 1x 1x 1x   1x   1x             3x 3x 1x     2x     2x   2x     4x   4x                   2x         2x 1x     1x   1x 2x 2x     1x       3x 3x 2x                               3x 3x 1x     2x                 2x               1x               1x     1x                             3x 3x 2x     1x 1x 1x   1x       3x 3x 2x     1x 1x   1x       2x   2x                  
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { isNotEmpty } from 'class-validator';
import { BookmarkQueryRepository } from 'src/bookmark/bookmark.query.repository';
import { CommentQueryRepository } from 'src/comment/comment.query.repository';
import { ERROR } from 'src/commons/constants/error';
import { CursorPaginatedResponseDto } from 'src/commons/dtos/cursor-paginated-response.dto';
import { LastItemIdResponseDto } from 'src/commons/dtos/last-item-id-response.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 { ApiCommunityGetCheckPostedResponseDto } from 'src/community/dto/api-community-get-check-posted-response.dto';
import { ApiCommunityGetDetailResponseDto } from 'src/community/dto/api-community-get-detail-response.dto';
import { ApiCommunityGetMyCourseRequestQueryDto } from 'src/community/dto/api-community-get-my-course-request-query.dto';
import { ApiCommunityGetMyCourseResponseDto } from 'src/community/dto/api-community-get-my-course-response.dto';
import { ApiCommunityGetRequestQueryDto } from 'src/community/dto/api-community-get-request-query.dto';
import { ApiCommunityGetResponseDto } from 'src/community/dto/api-community-get-response.dto';
import { ApiCommunityPostRequestBodyDto } from 'src/community/dto/api-community-post-request-body.dto';
import { ApiCommunityPutRequestBodyDto } from 'src/community/dto/api-community-put-request-body.dto';
import { CommunityCoursePlaceDetailDto } from 'src/community/dto/community-course-place-detail.dto';
import { CourseQueryRepository } from 'src/course/course.query.repository';
import { CommunityEntity } from 'src/entities/community.entity';
import { CourseEntity } from 'src/entities/course.entity';
import { ReactionQueryRepository } from 'src/reaction/reaction.query.repository';
import { UserDto } from 'src/user/dto/user.dto';
import { UserQueryRepository } from 'src/user/user.query.repository';
 
@Injectable()
export class CommunityService {
  constructor(
    private readonly communityQueryRepository: CommunityQueryRepository,
    private readonly bookmarkQueryRepository: BookmarkQueryRepository,
    private readonly courseQueryRepository: CourseQueryRepository,
    private readonly reactionQueryRepository: ReactionQueryRepository,
    private readonly userQueryRepository: UserQueryRepository,
    private readonly commentQueryRepository: CommentQueryRepository,
  ) {}
 
  async communityPost(
    uuid: string,
    user: UserDto,
    dto: ApiCommunityPostRequestBodyDto,
  ): Promise<UuidResponseDto> {
    const course = await this.courseQueryRepository.findOne(uuid);
    if (isEmpty(course)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    const community = await this.communityQueryRepository.findCommunityByCourse(uuid, user);
    if (isNotEmpty(community)) {
      throw new ConflictException(ERROR.DUPLICATION);
    }
 
    const communityEntity = new CommunityEntity();
    communityEntity.uuid = generateUUID();
    communityEntity.course_uuid = uuid;
    communityEntity.user_name = user.nickname;
    communityEntity.user_uuid = user.uuid;
    communityEntity.course_name = course.course_name;
    communityEntity.review = dto.review;
    communityEntity.score = dto.score;
 
    await this.communityQueryRepository.save(communityEntity);
 
    return { uuid: communityEntity.uuid };
  }
 
  async communityMyCourseList(
    dto: ApiCommunityGetMyCourseRequestQueryDto,
    user: UserDto,
  ): Promise<LastItemIdResponseDto<ApiCommunityGetMyCourseResponseDto>> {
    const myCourseList: CourseEntity[] = await this.courseQueryRepository.findMyCourse(dto, user);
    if (myCourseList.length === 0) {
      return { items: [], last_item_id: 0 };
    }
 
    const myCommunity: CommunityEntity[] = await this.communityQueryRepository.myCommunity(user);
 
    const lastItemId =
      myCourseList.length === dto.size ? myCourseList[myCourseList.length - 1].id : 0;
 
    return {
      items: plainToInstance(ApiCommunityGetMyCourseResponseDto, myCourseList, {
        excludeExtraneousValues: true,
      }).map((courseDto) => ({
        ...courseDto,
        is_posted: myCommunity.map((item) => item.course_uuid).includes(courseDto.course_uuid),
      })),
      last_item_id: lastItemId,
    };
  }
 
  async communityList(
    dto: ApiCommunityGetRequestQueryDto,
    user: UserDto,
  ): Promise<CursorPaginatedResponseDto<ApiCommunityGetResponseDto>> {
    const [totalCount, communityResult] = await Promise.all([
      this.communityQueryRepository.countTotalCommunity(dto, user),
      this.communityQueryRepository.findCommunityList(dto, user),
    ]);
 
    if (totalCount === 0) {
      return { items: [], total_count: 0, next_page: null };
    }
 
    const { communityList, nextCursor } = communityResult;
 
    const [courseList, userList] = await Promise.all([
      this.courseQueryRepository.findList(communityList.map((item) => item.course_uuid)),
      this.userQueryRepository.findUserList(communityList.map((item) => item.user_uuid)),
    ]);
 
    return {
      items: plainToInstance(ApiCommunityGetResponseDto, communityList, {
        excludeExtraneousValues: true,
      }).map((community) => {
        const relatedCourse = courseList.find((c) => c.uuid === community.course_uuid);
        const relatedUser = userList.find((u) => u.uuid === community.user_uuid);
        return {
          ...community,
          customs: relatedCourse.customs,
          line: relatedCourse.line,
          subway: relatedCourse.subway,
          course_image: relatedCourse.course_image,
          user_name: relatedUser.name,
          user_profile_image: relatedUser.profile_image,
        };
      }),
      total_count: totalCount,
      next_page: nextCursor,
    };
  }
 
  async communityDetail(uuid: string, user: UserDto): Promise<ApiCommunityGetDetailResponseDto> {
    const community: CommunityEntity = await this.communityQueryRepository.findOne(uuid);
    if (isEmpty(community)) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    const [bookmark, course, coursePlaces, reactions, communityUser, comment] = await Promise.all([
      this.bookmarkQueryRepository.findMyCourse(community.course_uuid),
      this.courseQueryRepository.findOne(community.course_uuid),
      this.courseQueryRepository.findPlace(community.course_uuid),
      this.reactionQueryRepository.findCommunityDetailReaction(uuid),
      this.userQueryRepository.findOne(community.user_uuid),
      this.commentQueryRepository.findMyComment(uuid, user),
    ]);
 
    return new ApiCommunityGetDetailResponseDto({
      uuid,
      course_uuid: community.course_uuid,
      user_uuid: communityUser.uuid,
      user_name: communityUser.name,
      user_profile_image: communityUser.profile_image,
      review: community.review,
      score: community.score,
      is_bookmarked: bookmark.map((item) => item.user_uuid).includes(user.uuid),
      is_commented: !!(comment || community.user_uuid === user.uuid),
      course_name: community.course_name,
      course_image: course.course_image,
      customs: course.customs,
      subway: course.subway,
      count: coursePlaces.length,
      like: reactions.length,
      is_liked: reactions.map((item) => item.user_uuid).includes(user.uuid),
      places: plainToInstance(
        CommunityCoursePlaceDetailDto,
        coursePlaces.map((coursePlace) => ({
          ...coursePlace.place,
          sort: coursePlace.sort,
          uuid: coursePlace.place_uuid,
        })),
        { excludeExtraneousValues: true },
      ),
    });
  }
 
  async communityPut(
    user: UserDto,
    dto: ApiCommunityPutRequestBodyDto,
    uuid: string,
  ): Promise<UuidResponseDto> {
    const community: CommunityEntity = await this.communityQueryRepository.findOne(uuid);
    if (isEmpty(community) || community.user_uuid !== user.uuid) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    community.review = dto.review;
    community.score = dto.score;
    await this.communityQueryRepository.save(community);
 
    return { uuid };
  }
 
  async communityDelete(user: UserDto, uuid: string): Promise<UuidResponseDto> {
    const community: CommunityEntity = await this.communityQueryRepository.findOne(uuid);
    if (isEmpty(community) || community.user_uuid !== user.uuid) {
      throw new NotFoundException(ERROR.NOT_EXIST_DATA);
    }
 
    community.archived_at = new Date();
    await this.communityQueryRepository.save(community);
 
    return { uuid };
  }
 
  async checkPosted(uuid: string): Promise<ApiCommunityGetCheckPostedResponseDto> {
    const community: CommunityEntity = await this.communityQueryRepository.findCourse(uuid);
 
    return plainToInstance(
      ApiCommunityGetCheckPostedResponseDto,
      { is_posted: !isEmpty(community) },
      {
        excludeExtraneousValues: true,
      },
    );
  }
}