|
1
|
|
|
import {Inject} from '@nestjs/common'; |
|
2
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
|
3
|
|
|
import {IHolidayRepository} from 'src/Domain/HumanResource/Holiday/Repository/IHolidayRepository'; |
|
4
|
|
|
import {HolidayNotFoundException} from 'src/Domain/HumanResource/Holiday/Exception/HolidayNotFoundException'; |
|
5
|
|
|
import {IDateUtils} from 'src/Application/IDateUtils'; |
|
6
|
|
|
import {HolidayCantBeModeratedException} from 'src/Domain/HumanResource/Holiday/Exception/HolidayCantBeModeratedException'; |
|
7
|
|
|
import {CanHolidayBeModerated} from 'src/Domain/HumanResource/Holiday/Specification/CanHolidayBeModerated'; |
|
8
|
|
|
import {AcceptHolidayCommand} from './AcceptHolidayCommand'; |
|
9
|
|
|
import {IEventBus} from 'src/Application/IEventBus'; |
|
10
|
|
|
import {AcceptedHolidayEvent} from '../Event/AcceptedHolidayEvent'; |
|
11
|
|
|
|
|
12
|
|
|
@CommandHandler(AcceptHolidayCommand) |
|
13
|
|
|
export class AcceptHolidayCommandHandler { |
|
14
|
|
|
constructor( |
|
15
|
|
|
@Inject('IHolidayRepository') |
|
16
|
|
|
private readonly holidayRepository: IHolidayRepository, |
|
17
|
|
|
@Inject('IEventBus') |
|
18
|
|
|
private readonly eventBus: IEventBus, |
|
19
|
|
|
@Inject('IDateUtils') |
|
20
|
|
|
private readonly dateUtils: IDateUtils, |
|
21
|
|
|
private readonly canHolidayBeModerated: CanHolidayBeModerated |
|
22
|
|
|
) {} |
|
23
|
|
|
|
|
24
|
|
|
public async execute(command: AcceptHolidayCommand): Promise<string> { |
|
25
|
|
|
const {moderator, moderationComment, id} = command; |
|
26
|
|
|
|
|
27
|
|
|
const holiday = await this.holidayRepository.findOneById(id); |
|
28
|
|
|
if (!holiday) { |
|
29
|
|
|
throw new HolidayNotFoundException(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ( |
|
33
|
|
|
false === this.canHolidayBeModerated.isSatisfiedBy(holiday, moderator) |
|
34
|
|
|
) { |
|
35
|
|
|
throw new HolidayCantBeModeratedException(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
holiday.accept( |
|
39
|
|
|
moderator, |
|
40
|
|
|
this.dateUtils.getCurrentDateToISOString(), |
|
41
|
|
|
moderationComment |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
await this.holidayRepository.save(holiday); |
|
45
|
|
|
this.eventBus.publish(new AcceptedHolidayEvent(holiday)); |
|
46
|
|
|
|
|
47
|
|
|
return holiday.getId(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|