| Total Complexity | 4 |
| Complexity/F | 4 |
| Lines of Code | 57 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import {Inject} from '@nestjs/common'; |
||
| 2 | import {CommandHandler} from '@nestjs/cqrs'; |
||
| 3 | import { UserAdministrativeMissingException } from 'src/Domain/HumanResource/User/Exception/UserAdministrativeMissingException'; |
||
| 4 | import {UserNotFoundException} from 'src/Domain/HumanResource/User/Exception/UserNotFoundException'; |
||
| 5 | import {IUserAdministrativeRepository} from 'src/Domain/HumanResource/User/Repository/IUserAdministrativeRepository'; |
||
| 6 | import {IUserRepository} from 'src/Domain/HumanResource/User/Repository/IUserRepository'; |
||
| 7 | import {UpdateUserCommand} from './UpdateUserCommand'; |
||
| 8 | |||
| 9 | @CommandHandler(UpdateUserCommand) |
||
| 10 | export class UpdateUserCommandHandler { |
||
| 11 | constructor( |
||
| 12 | @Inject('IUserRepository') |
||
| 13 | private readonly userRepository: IUserRepository, |
||
| 14 | @Inject('IUserAdministrativeRepository') |
||
| 15 | private readonly userAdministrativeRepository: IUserAdministrativeRepository |
||
| 16 | ) {} |
||
| 17 | |||
| 18 | public async execute(command: UpdateUserCommand): Promise<void> { |
||
| 19 | const { |
||
| 20 | id, |
||
| 21 | role, |
||
| 22 | annualEarnings, |
||
| 23 | contract, |
||
| 24 | executivePosition, |
||
| 25 | healthInsurance, |
||
| 26 | joiningDate, |
||
| 27 | leavingDate, |
||
| 28 | transportFee |
||
| 29 | } = command; |
||
| 30 | |||
| 31 | const user = await this.userRepository.findOneById(id); |
||
| 32 | if (!user) { |
||
| 33 | throw new UserNotFoundException(); |
||
| 34 | } |
||
| 35 | |||
| 36 | const userAdministrative = await this.userAdministrativeRepository.findOneByUserId(id); |
||
| 37 | |||
| 38 | if (!userAdministrative) { |
||
| 39 | throw new UserAdministrativeMissingException(); |
||
| 40 | } |
||
| 41 | |||
| 42 | user.updateRole(role); |
||
| 43 | userAdministrative.update( |
||
| 44 | Math.round(annualEarnings * 100), |
||
| 45 | contract, |
||
| 46 | executivePosition, |
||
| 47 | healthInsurance, |
||
| 48 | joiningDate, |
||
| 49 | leavingDate, |
||
| 50 | transportFee ? Math.round(transportFee * 100) : 0 |
||
| 51 | ); |
||
| 52 | |||
| 53 | await this.userRepository.save(user); |
||
| 54 | await this.userAdministrativeRepository.save(userAdministrative); |
||
| 55 | } |
||
| 56 | } |
||
| 57 |