Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 44 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import {Inject} from '@nestjs/common'; |
||
2 | import {CommandHandler} from '@nestjs/cqrs'; |
||
3 | import {RefuseLeaveCommand} from './RefuseLeaveRequestCommand'; |
||
4 | import {ILeaveRepository} from 'src/Infrastructure/HumanResource/Leave/Repository/node_modules/src/Domain/HumanResource/Leave/Repository/ILeaveRepository'; |
||
5 | import {LeaveRequestNotFoundException} from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException'; |
||
6 | import {IDateUtils} from 'src/Application/IDateUtils'; |
||
7 | import {LeaveRequestCantBeModeratedException} from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeModeratedException'; |
||
8 | import {CanLeaveRequestBeModerated} from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeModerated'; |
||
9 | |||
10 | @CommandHandler(RefuseLeaveCommand) |
||
11 | export class RefuseLeaveCommandHandler { |
||
12 | constructor( |
||
13 | @Inject('ILeaveRepository') |
||
14 | private readonly leaveRepository: ILeaveRepository, |
||
15 | @Inject('IDateUtils') |
||
16 | private readonly dateUtils: IDateUtils, |
||
17 | private readonly CanLeaveRequestBeModerated: CanLeaveRequestBeModerated |
||
18 | ) {} |
||
19 | |||
20 | public async execute(command: RefuseLeaveCommand): Promise<string> { |
||
21 | const {moderator, id, moderationComment} = command; |
||
22 | |||
23 | const leave = await this.leaveRepository.findOneById(id); |
||
24 | if (!leave) { |
||
25 | throw new LeaveRequestNotFoundException(); |
||
26 | } |
||
27 | |||
28 | if ( |
||
29 | false === this.CanLeaveRequestBeModerated.isSatisfiedBy(leave, moderator) |
||
30 | ) { |
||
31 | throw new LeaveRequestCantBeModeratedException(); |
||
32 | } |
||
33 | |||
34 | leave.refuse( |
||
35 | moderator, |
||
36 | this.dateUtils.getCurrentDateToISOString(), |
||
37 | moderationComment |
||
38 | ); |
||
39 | await this.leaveRepository.save(leave); |
||
40 | |||
41 | return leave.getId(); |
||
42 | } |
||
43 | } |
||
44 |