All files / src/modules/domain/auth/user/services user.service.ts

0% Statements 0/19
0% Branches 0/3
0% Functions 0/2
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                                                                       
import { BadRequestException, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { omit } from 'lodash';
import { AUTH_ERRORS } from 'src/exception/exception.const';
import { UserInfoRepository } from 'src/modules/common/database/postgres/account/repositories/user-info.repository';
import { AuthUserRepository } from 'src/modules/common/database/postgres/auth/repositories/auth-user.repository';
import { EventTypeEnum } from 'src/modules/common/event/shared/event.const';
import { encodePassword } from '../../shared/auth.util';
import { IAuthUser, IRegisterAuthUser } from '../shared/user.interface';
 
@Injectable()
export class UserService {
  constructor(
    private readonly authUserRepository: AuthUserRepository,
    private readonly userInfoRepository: UserInfoRepository,
    private readonly eventEmitter: EventEmitter2,
  ) {}
 
  async register(input: IRegisterAuthUser): Promise<IAuthUser> {
    Iif (await this.userInfoRepository.checkExistedByEmail(input.email))
      throw new BadRequestException(AUTH_ERRORS.USER_EXISTED);
 
    const user = await this.authUserRepository.create({
      ...input,
      ...(input.password && { password: encodePassword(input.password) }),
    });
 
    this.eventEmitter.emit(EventTypeEnum.UserCreated, {
      userId: user.userId,
      email: input.email,
    });
 
    return omit(user, 'password');
  }
}