1
|
|
|
import { Inject } from '@nestjs/common'; |
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
3
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
4
|
|
|
import { CanLeaveRequestBeModerated } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeModerated'; |
5
|
|
|
import { AcceptLeaveRequestCommand } from './AcceptLeaveRequestCommand'; |
6
|
|
|
import { IEventBus } from 'src/Application/IEventBus'; |
7
|
|
|
import { AcceptedLeaveRequestEvent } from '../Event/AcceptedLeaveRequestEvent'; |
8
|
|
|
import { DoesEventsExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesEventsExistForPeriod'; |
9
|
|
|
import { EventsAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsAlreadyExistForThisPeriodException'; |
10
|
|
|
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
11
|
|
|
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException'; |
12
|
|
|
import { LeaveRequestCantBeModeratedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeModeratedException'; |
13
|
|
|
|
14
|
|
|
@CommandHandler(AcceptLeaveRequestCommand) |
15
|
|
|
export class AcceptLeaveRequestCommandHandler { |
16
|
|
|
constructor( |
17
|
|
|
@Inject('ILeaveRequestRepository') |
18
|
|
|
private readonly leaveRequestRepository: ILeaveRequestRepository, |
19
|
|
|
@Inject('IEventBus') |
20
|
|
|
private readonly eventBus: IEventBus, |
21
|
|
|
@Inject('IDateUtils') |
22
|
|
|
private readonly dateUtils: IDateUtils, |
23
|
|
|
private readonly canLeaveRequestBeModerated: CanLeaveRequestBeModerated, |
24
|
|
|
private readonly doesEventsExistForPeriod: DoesEventsExistForPeriod |
25
|
|
|
) {} |
26
|
|
|
|
27
|
|
|
public async execute(command: AcceptLeaveRequestCommand): Promise<string> { |
28
|
|
|
const { moderator, moderationComment, id } = command; |
29
|
|
|
|
30
|
|
|
const leaveRequest = await this.leaveRequestRepository.findOneById(id); |
31
|
|
|
if (!leaveRequest) { |
32
|
|
|
throw new LeaveRequestNotFoundException(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ( |
36
|
|
|
false === this.canLeaveRequestBeModerated.isSatisfiedBy(leaveRequest, moderator) |
37
|
|
|
) { |
38
|
|
|
throw new LeaveRequestCantBeModeratedException(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ( |
42
|
|
|
true === |
43
|
|
|
(await this.doesEventsExistForPeriod.isSatisfiedBy( |
44
|
|
|
leaveRequest.getUser(), |
45
|
|
|
leaveRequest.getStartDate(), |
46
|
|
|
leaveRequest.getEndDate() |
47
|
|
|
)) |
48
|
|
|
) { |
49
|
|
|
throw new EventsAlreadyExistForThisPeriodException(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// todo : check for leaves presence |
53
|
|
|
|
54
|
|
|
leaveRequest.accept( |
55
|
|
|
moderator, |
56
|
|
|
this.dateUtils.getCurrentDateToISOString(), |
57
|
|
|
moderationComment |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
await this.leaveRequestRepository.save(leaveRequest); |
61
|
|
|
this.eventBus.publish(new AcceptedLeaveRequestEvent(leaveRequest)); |
62
|
|
|
|
63
|
|
|
return leaveRequest.getId(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|