All files / user user.query.repository.ts

100% Statements 20/20
100% Branches 4/4
100% Functions 8/8
100% Lines 16/16

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 6618x   18x     18x   18x     20x       1x           2x         2x                   1x       1x           1x           2x   2x 2x   2x       2x          
import { InjectRepository } from '@nestjs/typeorm';
import { SocialUser } from 'src/auth/interfaces/auth.interface';
import { UserEntity } from 'src/entities/user.entity';
import { ApiUserPutUpdateRequestBodyDto } from 'src/user/dto/api-user-put-update-request-body.dto';
import { UserDto } from 'src/user/dto/user.dto';
import { In, Repository, UpdateResult } from 'typeorm';
 
export class UserQueryRepository {
  constructor(
    @InjectRepository(UserEntity)
    private repository: Repository<UserEntity>,
  ) {}
 
  async findUser(user: SocialUser): Promise<UserEntity> {
    return this.repository.findOne({
      where: { email: user.email, type: user.type },
    });
  }
 
  async createUser(user: SocialUser, uuid: string): Promise<UserEntity> {
    const userData = {
      ...user,
      photo: user.type === 'kakao' ? user.photo : null,
    };
 
    return this.repository.save({
      uuid,
      email: userData.email,
      name: userData.nickname,
      profile_image: userData.photo,
      type: userData.type,
    });
  }
 
  async save(userEntity: UserEntity): Promise<UserEntity> {
    return this.repository.save(userEntity);
  }
 
  async findId(id: number): Promise<UserEntity> {
    return this.repository.findOne({
      where: { id },
    });
  }
 
  async findOne(uuid: string): Promise<UserEntity> {
    return this.repository.findOne({
      where: { uuid },
    });
  }
 
  async profileUpdate(dto: ApiUserPutUpdateRequestBodyDto, user: UserDto): Promise<UpdateResult> {
    const updateFields: Partial<ApiUserPutUpdateRequestBodyDto> = {};
 
    if (dto.name) updateFields.name = dto.name;
    if (dto.profile_image) updateFields.profile_image = dto.profile_image;
 
    return this.repository.update({ id: user.id }, updateFields);
  }
 
  async findUserList(uuids: string[]): Promise<UserEntity[]> {
    return this.repository.find({
      where: { uuid: In(uuids) },
    });
  }
}