All files / api/user/infrastructure user.repository.ts

0% Statements 0/19
0% Branches 0/1
0% Functions 0/5
0% Lines 0/17

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                                                                                                   
import { map } from '@COMMON/util';
import { IUserAggregate } from '@INTERFACE/user/domain';
import { IUserRepository } from '@INTERFACE/user/infrastructure';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { IsNull, Repository } from 'typeorm';
import { UserEntity } from './user.entity';
import { aggregate_to_entity, entity_to_aggregate } from './user.mapper';
 
@Injectable()
export class UserRepository implements IUserRepository {
  constructor(
    @InjectRepository(UserEntity)
    private readonly repository: Repository<UserEntity>,
  ) {}
 
  async findOne(
    profile: IUserAggregate.Profile,
  ): Promise<IUserAggregate.State | null> {
    const user = await this.repository
      .createQueryBuilder('user')
      .withDeleted()
      .where('user.id = :id', { id: profile.id })
      .getOne(); // soft-delete된 데이터도 불러온다.
    Iif (user?.deleted_at != null) {
      await this.repository.restore(user.id);
    }
    return map(user, entity_to_aggregate(profile));
  }
 
  async save(state: IUserAggregate.State): Promise<IUserAggregate.State> {
    return entity_to_aggregate(state)(
      await this.repository.save(aggregate_to_entity(state)),
    );
  }
 
  async update(
    { id }: Pick<IUserAggregate.State, 'id'>,
    data: Partial<Pick<IUserAggregate.State, 'role'>>,
  ): Promise<void> {
    await this.repository.update({ id, deleted_at: IsNull() }, data); // soft-deleted entity는 update하지 않는다.
    return;
  }
 
  async remove({ id }: Pick<IUserAggregate.State, 'id'>): Promise<void> {
    await this.repository.softDelete(id);
    return;
  }
}