| Total Complexity | 3 |
| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import {Inject} from '@nestjs/common'; |
||
| 9 | |||
| 10 | @CommandHandler(RefuseHolidayCommand) |
||
| 11 | export class RefuseHolidayCommandHandler { |
||
| 12 | constructor( |
||
| 13 | @Inject('IHolidayRepository') |
||
| 14 | private readonly holidayRepository: IHolidayRepository, |
||
| 15 | @Inject('IDateUtils') |
||
| 16 | private readonly dateUtils: IDateUtils, |
||
| 17 | private readonly canHolidayBeModerated: CanHolidayBeModerated |
||
| 18 | ) {} |
||
| 19 | |||
| 20 | public async execute(command: RefuseHolidayCommand): Promise<string> { |
||
| 21 | const {moderator, id, moderationComment} = command; |
||
| 22 | |||
| 23 | const holiday = await this.holidayRepository.findOneById(id); |
||
| 24 | if (!holiday) { |
||
| 25 | throw new HolidayNotFoundException(); |
||
| 26 | } |
||
| 27 | |||
| 28 | if ( |
||
| 29 | false === this.canHolidayBeModerated.isSatisfiedBy(holiday, moderator) |
||
| 30 | ) { |
||
| 31 | throw new HolidayCantBeModeratedException(); |
||
| 32 | } |
||
| 33 | |||
| 34 | holiday.refuse( |
||
| 35 | moderator, |
||
| 36 | this.dateUtils.getCurrentDateToISOString(), |
||
| 37 | moderationComment |
||
| 38 | ); |
||
| 39 | await this.holidayRepository.save(holiday); |
||
| 40 | |||
| 41 | return holiday.getId(); |
||
| 42 | } |
||
| 44 |