Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 54 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import {Inject} from '@nestjs/common'; |
||
2 | import {CommandHandler} from '@nestjs/cqrs'; |
||
3 | import {CreateHolidayCommand} from './CreateHolidayCommand'; |
||
4 | import {IHolidayRepository} from 'src/Domain/HumanResource/Holiday/Repository/IHolidayRepository'; |
||
5 | import {Holiday} from 'src/Domain/HumanResource/Holiday/Holiday.entity'; |
||
6 | import {DoesHolidayExistForPeriod} from 'src/Domain/HumanResource/Holiday/Specification/DoesHolidayExistForPeriod'; |
||
7 | import {HolidayAlreadyExistForThisPeriodException} from 'src/Domain/HumanResource/Holiday/Exception/HolidayAlreadyExistForThisPeriodException'; |
||
8 | |||
9 | @CommandHandler(CreateHolidayCommand) |
||
10 | export class CreateHolidayCommandHandler { |
||
11 | constructor( |
||
12 | @Inject('IHolidayRepository') |
||
13 | private readonly holidayRepository: IHolidayRepository, |
||
14 | private readonly doesHolidayExistForPeriod: DoesHolidayExistForPeriod |
||
15 | ) {} |
||
16 | |||
17 | public async execute(command: CreateHolidayCommand): Promise<string> { |
||
18 | const { |
||
19 | user, |
||
20 | endDate, |
||
21 | endsAllDay, |
||
22 | leaveType, |
||
23 | startDate, |
||
24 | startsAllDay, |
||
25 | comment |
||
26 | } = command; |
||
27 | |||
28 | if ( |
||
29 | true === |
||
30 | (await this.doesHolidayExistForPeriod.isSatisfiedBy( |
||
31 | user, |
||
32 | startDate, |
||
33 | endDate |
||
34 | )) |
||
35 | ) { |
||
36 | throw new HolidayAlreadyExistForThisPeriodException(); |
||
37 | } |
||
38 | |||
39 | const holiday = await this.holidayRepository.save( |
||
40 | new Holiday( |
||
41 | user, |
||
42 | leaveType, |
||
43 | startDate, |
||
44 | startsAllDay, |
||
45 | endDate, |
||
46 | endsAllDay, |
||
47 | comment |
||
48 | ) |
||
49 | ); |
||
50 | |||
51 | return holiday.getId(); |
||
52 | } |
||
53 | } |
||
54 |