1
|
|
|
import {Inject} from '@nestjs/common'; |
2
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
3
|
|
|
import {RefuseHolidayCommand} from './RefuseHolidayCommand'; |
4
|
|
|
import {IHolidayRepository} from 'src/Domain/HumanResource/Holiday/Repository/IHolidayRepository'; |
5
|
|
|
import {Holiday} from 'src/Domain/HumanResource/Holiday/Holiday.entity'; |
6
|
|
|
import {HolidayAlreadyExistForThisPeriodException} from 'src/Domain/HumanResource/Holiday/Exception/HolidayAlreadyExistForThisPeriodException'; |
7
|
|
|
import {HolidayNotFoundException} from 'src/Domain/HumanResource/Holiday/Exception/HolidayNotFoundException'; |
8
|
|
|
import {IDateUtils} from 'src/Application/IDateUtils'; |
9
|
|
|
import {CanHolidayBeRefused} from 'src/Domain/HumanResource/Holiday/Specification/CanHolidayBeRefused'; |
10
|
|
|
import {HolidayCantBeRefusedException} from 'src/Domain/HumanResource/Holiday/Exception/HolidayCantBeRefusedException'; |
11
|
|
|
|
12
|
|
|
@CommandHandler(RefuseHolidayCommand) |
13
|
|
|
export class RefuseHolidayCommandHandler { |
14
|
|
|
constructor( |
15
|
|
|
@Inject('IHolidayRepository') |
16
|
|
|
private readonly holidayRepository: IHolidayRepository, |
17
|
|
|
@Inject('IDateUtils') |
18
|
|
|
private readonly dateUtils: IDateUtils, |
19
|
|
|
private readonly canHolidayBeRefused: CanHolidayBeRefused |
20
|
|
|
) {} |
21
|
|
|
|
22
|
|
|
public async execute(command: RefuseHolidayCommand): Promise<string> { |
23
|
|
|
const {user, id} = command; |
24
|
|
|
|
25
|
|
|
const holiday = await this.holidayRepository.findOneById(id); |
26
|
|
|
if (!holiday) { |
27
|
|
|
throw new HolidayNotFoundException(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (false === this.canHolidayBeRefused.isSatisfiedBy(holiday, user)) { |
31
|
|
|
throw new HolidayCantBeRefusedException(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
holiday.refuse(user, this.dateUtils.getCurrentDate().toISOString()); |
35
|
|
|
await this.holidayRepository.save(holiday); |
36
|
|
|
|
37
|
|
|
return holiday.getId(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|