src/Application/HumanResource/User/Command/UpdateProfileCommandHandler.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 39
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 33
mnd 2
bc 2
fnc 1
dl 0
loc 39
bpm 2
cpm 3
noi 0
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A UpdateProfileCommandHandler.execute 0 19 3
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { IUserRepository } from 'src/Domain/HumanResource/User/Repository/IUserRepository';
4
import { IPasswordEncoder } from 'src/Application/IPasswordEncoder';
5
import { IsEmailAlreadyExist } from 'src/Domain/HumanResource/User/Specification/IsEmailAlreadyExist';
6
import { EmailAlreadyExistException } from 'src/Domain/HumanResource/User/Exception/EmailAlreadyExistException';
7
import { UpdateProfileCommand } from './UpdateProfileCommand';
8
9
@CommandHandler(UpdateProfileCommand)
10
export class UpdateProfileCommandHandler {
11
  constructor(
12
    @Inject('IUserRepository')
13
    private readonly userRepository: IUserRepository,
14
    @Inject('IPasswordEncoder')
15
    private readonly passwordEncoder: IPasswordEncoder,
16
    private readonly isEmailAlreadyExist: IsEmailAlreadyExist
17
  ) {}
18
19
  public async execute(command: UpdateProfileCommand): Promise<void> {
20
    const { firstName, lastName, password, user } = command;
21
    const email = command.email.toLowerCase();
22
23
    if (
24
      email !== user.getEmail() &&
25
      true === (await this.isEmailAlreadyExist.isSatisfiedBy(email))
26
    ) {
27
      throw new EmailAlreadyExistException();
28
    }
29
30
    user.update(firstName, lastName, email);
31
32
    if (password) {
33
      user.updatePassword(await this.passwordEncoder.hash(password));
34
    }
35
36
    await this.userRepository.save(user);
37
  }
38
}
39