All files / src/application/user register-user.use-case.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 2/2
100% Lines 9/9

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                            24x 24x       218x   218x 2x     216x   216x             216x   215x      
import { randomUUID } from 'node:crypto';
import { User } from '../../domain/user/user';
import { UserRepository } from './user-repository';
import { PasswordHasher } from './password-hasher';
import { EmailAlreadyInUseError } from './errors/email-already-in-use.error';
 
export interface RegisterUserInput {
  email: string;
  password: string;
  name: string;
}
 
export class RegisterUserUseCase {
  constructor(
    private readonly userRepository: UserRepository,
    private readonly passwordHasher: PasswordHasher,
  ) {}
 
  async execute(input: RegisterUserInput): Promise<User> {
    const existingUser = await this.userRepository.findByEmail(input.email);
 
    if (existingUser) {
      throw new EmailAlreadyInUseError(input.email);
    }
 
    const passwordHash = await this.passwordHasher.hash(input.password);
 
    const user = User.create({
      id: randomUUID(),
      email: input.email,
      name: input.name,
      passwordHash,
    });
 
    await this.userRepository.save(user);
 
    return user;
  }
}