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
|
|
|
|