Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 68 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { Inject } from '@nestjs/common'; |
||
2 | import { CommandHandler } from '@nestjs/cqrs'; |
||
3 | import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand'; |
||
4 | import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
||
5 | import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity'; |
||
6 | import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod'; |
||
7 | import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException'; |
||
8 | import { DoesEventsExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesEventsExistForPeriod'; |
||
9 | import { EventsAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsAlreadyExistForThisPeriodException'; |
||
10 | |||
11 | @CommandHandler(CreateLeaveRequestCommand) |
||
12 | export class CreateLeaveRequestCommandHandler { |
||
13 | constructor( |
||
14 | @Inject('ILeaveRequestRepository') |
||
15 | private readonly leaveRequestRepository: ILeaveRequestRepository, |
||
16 | private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod, |
||
17 | private readonly doesEventsExistForPeriod: DoesEventsExistForPeriod |
||
18 | ) {} |
||
19 | |||
20 | public async execute(command: CreateLeaveRequestCommand): Promise<string> { |
||
21 | const { |
||
22 | user, |
||
23 | endDate, |
||
24 | endsAllDay, |
||
25 | type, |
||
26 | startDate, |
||
27 | startsAllDay, |
||
28 | comment |
||
29 | } = command; |
||
30 | |||
31 | if ( |
||
32 | true === |
||
33 | (await this.doesLeaveRequestExistForPeriod.isSatisfiedBy( |
||
34 | user, |
||
35 | startDate, |
||
36 | endDate |
||
37 | )) |
||
38 | ) { |
||
39 | throw new LeaveRequestAlreadyExistForThisPeriodException(); |
||
40 | } |
||
41 | |||
42 | if ( |
||
43 | true === |
||
44 | (await this.doesEventsExistForPeriod.isSatisfiedBy( |
||
45 | user, |
||
46 | startDate, |
||
47 | endDate |
||
48 | )) |
||
49 | ) { |
||
50 | throw new EventsAlreadyExistForThisPeriodException(); |
||
51 | } |
||
52 | |||
53 | const leaveRequest = await this.leaveRequestRepository.save( |
||
54 | new LeaveRequest( |
||
55 | user, |
||
56 | type, |
||
57 | startDate, |
||
58 | startsAllDay, |
||
59 | endDate, |
||
60 | endsAllDay, |
||
61 | comment |
||
62 | ) |
||
63 | ); |
||
64 | |||
65 | return leaveRequest.getId(); |
||
66 | } |
||
67 | } |
||
68 |