1
|
|
|
import { QueryHandler } from '@nestjs/cqrs'; |
2
|
|
|
import { GetLeaveRequestsQuery } from './GetLeaveRequestsQuery'; |
3
|
|
|
import { Inject } from '@nestjs/common'; |
4
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
5
|
|
|
import { LeaveRequestView } from '../View/LeaveRequestView'; |
6
|
|
|
import { UserSummaryView } from '../../User/View/UserSummaryView'; |
7
|
|
|
import { Pagination } from 'src/Application/Common/Pagination'; |
8
|
|
|
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
9
|
|
|
|
10
|
|
|
@QueryHandler(GetLeaveRequestsQuery) |
11
|
|
|
export class GetLeaveRequestsQueryHandler { |
12
|
|
|
constructor( |
13
|
|
|
@Inject('IleaveRequestRepository') |
14
|
|
|
private readonly leaveRequestRepository: ILeaveRequestRepository, |
15
|
|
|
@Inject('IDateUtils') |
16
|
|
|
private readonly dateUtils: IDateUtils |
17
|
|
|
) {} |
18
|
|
|
|
19
|
|
|
public async execute( |
20
|
|
|
query: GetLeaveRequestsQuery |
21
|
|
|
): Promise<Pagination<LeaveRequestView>> { |
22
|
|
|
const leaveRequestViews: LeaveRequestView[] = []; |
23
|
|
|
const [ leaves, total ] = await this.leaveRequestRepository.findLeaveRequests( |
24
|
|
|
query.page |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
for (const leave of leaves) { |
28
|
|
|
const user = leave.getUser(); |
29
|
|
|
let duration = this.dateUtils.getWorkedDaysDuringAPeriod( |
30
|
|
|
new Date(leave.getStartDate()), |
31
|
|
|
new Date(leave.getEndDate()) |
32
|
|
|
).length; |
33
|
|
|
|
34
|
|
|
if (false === leave.isStartsAllDay()) { |
35
|
|
|
duration -= 0.5; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (false === leave.isEndsAllDay()) { |
39
|
|
|
duration -= 0.5; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
leaveRequestViews.push( |
43
|
|
|
new LeaveRequestView( |
44
|
|
|
leave.getId(), |
45
|
|
|
leave.getType(), |
46
|
|
|
leave.getStatus(), |
47
|
|
|
leave.getStartDate(), |
48
|
|
|
leave.getEndDate(), |
49
|
|
|
duration, |
50
|
|
|
new UserSummaryView( |
51
|
|
|
user.getId(), |
52
|
|
|
user.getFirstName(), |
53
|
|
|
user.getLastName() |
54
|
|
|
) |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return new Pagination<LeaveRequestView>(leaveRequestViews, total); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|