-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathGetUsersElementsQueryHandler.ts
More file actions
140 lines (122 loc) · 5.02 KB
/
Copy pathGetUsersElementsQueryHandler.ts
File metadata and controls
140 lines (122 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { QueryHandler } from '@nestjs/cqrs';
import { Inject } from '@nestjs/common';
import { IUserRepository } from 'src/Domain/HumanResource/User/Repository/IUserRepository';
import { UserElementsView } from '../View/UserElementsView';
import { GetUsersElementsQuery } from './GetUsersElementsQuery';
import { GetMealTicketsPerMonthQueryHandler } from '../../MealTicket/Query/GetMealTicketsPerMonthQueryHandler';
import { GetMealTicketsPerMonthQuery } from '../../MealTicket/Query/GetMealTicketsPerMonthQuery';
import { GetLeavesByMonthQueryHandler } from '../../Leave/Query/GetLeavesByMonthQueryHandler';
import { GetLeavesByMonthQuery } from '../../Leave/Query/GetLeavesByMonthQuery';
import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
import { IDateUtils } from 'src/Application/IDateUtils';
import { MonthDate } from 'src/Application/Common/MonthDate';
import { UserLeavesView } from '../View/UserLeavesView';
import { LeaveRequestSlotView } from '../../Leave/View/LeaveRequestSlotView';
@QueryHandler(GetUsersElementsQuery)
export class GetUsersElementsQueryHandler {
constructor(
@Inject('IUserRepository')
private readonly userRepository: IUserRepository,
private readonly getLeavesByMonthQueryHandler: GetLeavesByMonthQueryHandler,
private readonly getMealTicketsPerMonth: GetMealTicketsPerMonthQueryHandler,
@Inject('IDateUtils')
private readonly dateUtils: IDateUtils
) {}
public async execute(
query: GetUsersElementsQuery
): Promise<UserElementsView[]> {
const userViews: UserElementsView[] = [];
const date = query.date;
const [users, leaves, mealTickets] = await Promise.all([
this.userRepository.findUsersWithPayslipInfo(),
this.getLeavesByMonthQueryHandler.execute(
new GetLeavesByMonthQuery(date)
),
this.getMealTicketsPerMonth.execute(new GetMealTicketsPerMonthQuery(date))
]);
const mealTicketsByUser: Record<string, number> = {};
mealTickets.forEach(view => {
mealTicketsByUser[view.userId] = view.mealTickets;
});
for (const user of users) {
const userLeaves = leaves.getLeavesByUser(user);
userViews.push(
new UserElementsView(
user.getFirstName(),
user.getLastName(),
user.getUserAdministrative().getContract(),
user.getUserAdministrative().isExecutivePosition(),
user.getUserAdministrative().getJoiningDate(),
user.getUserAdministrative().getAnnualEarnings() * 0.01,
user.getUserAdministrative().getAnnualEarnings() / 1200,
user.getUserAdministrative().getWorkingTime(),
user.getUserAdministrative().getTransportFee() * 0.01,
user.getUserAdministrative().getSustainableMobilityFee() * 0.01,
user.getUserAdministrative().getSportsPassFee() * 0.01,
mealTicketsByUser[user.getId()],
user.getUserAdministrative().haveHealthInsurance(),
this.createUserLeavesView(userLeaves.paid, date),
this.createUserLeavesView(userLeaves.unpaid, date),
this.createUserLeavesView(userLeaves.medical, date),
this.createUserLeavesView(userLeaves.special, date),
this.createUserLeavesView(userLeaves.postponedWorkedFreeDay, date),
this.createUserLeavesView(userLeaves.relocation, date)
)
);
}
return userViews;
}
private createUserLeavesView(
leaves: LeaveRequest[],
date: Date
): UserLeavesView {
const monthDate = this.dateUtils.getMonth(date);
let leaveCount = 0;
const leavesSlotViews: LeaveRequestSlotView[] = [];
for (const leave of leaves) {
const monthScopedLeave = this.getMonthScopedLeave(leave, monthDate);
leaveCount += this.dateUtils.getLeaveDuration(
monthScopedLeave.startDate,
monthScopedLeave.startsAllDay,
monthScopedLeave.endDate,
monthScopedLeave.endsAllDay
);
leavesSlotViews.push(
new LeaveRequestSlotView(
monthScopedLeave.startDate,
monthScopedLeave.startsAllDay,
monthScopedLeave.endDate,
monthScopedLeave.endsAllDay
)
);
}
return new UserLeavesView(leaveCount, leavesSlotViews);
}
private getMonthScopedLeave(
leave: LeaveRequest,
month: MonthDate
): LeaveSlot {
const scopedLeave = new LeaveSlot();
if (new Date(leave.getStartDate()) >= month.getFirstDay()) {
scopedLeave.startDate = leave.getStartDate();
scopedLeave.startsAllDay = leave.isStartsAllDay();
} else {
scopedLeave.startDate = month.getFirstDay().toISOString();
scopedLeave.startsAllDay = true;
}
if (new Date(leave.getEndDate()) <= month.getLastDay()) {
scopedLeave.endDate = leave.getEndDate();
scopedLeave.endsAllDay = leave.isEndsAllDay();
} else {
scopedLeave.endDate = month.getLastDay().toISOString();
scopedLeave.endsAllDay = true;
}
return scopedLeave;
}
}
class LeaveSlot {
public startDate: string;
public startsAllDay: boolean;
public endDate: string;
public endsAllDay: boolean;
}