| Total Complexity | 2 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import {Inject} from '@nestjs/common'; |
||
| 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 | } |
||
| 54 |