1
|
|
|
import { QueryHandler } from '@nestjs/cqrs'; |
2
|
|
|
import { Inject } from '@nestjs/common'; |
3
|
|
|
import { IUserRepository } from 'src/Domain/HumanResource/User/Repository/IUserRepository'; |
4
|
|
|
import { UserElementsView } from '../View/UserElementsView'; |
5
|
|
|
import { GetUsersElementsQuery } from './GetUsersElementsQuery'; |
6
|
|
|
import { ILeaveRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository'; |
7
|
|
|
import { GetMealTicketsPerMonthQueryHandler } from '../../MealTicket/Query/GetMealTicketsPerMonthQueryHandler'; |
8
|
|
|
import { GetMealTicketsPerMonthQuery } from '../../MealTicket/Query/GetMealTicketsPerMonthQuery'; |
9
|
|
|
|
10
|
|
|
@QueryHandler(GetUsersElementsQuery) |
11
|
|
|
export class GetUsersElementsQueryHandler { |
12
|
|
|
constructor( |
13
|
|
|
@Inject('IUserRepository') |
14
|
|
|
private readonly userRepository: IUserRepository, |
15
|
|
|
@Inject('ILeaveRepository') |
16
|
|
|
private readonly leaveRepository: ILeaveRepository, |
17
|
|
|
private readonly getMealTicketsPerMonth: GetMealTicketsPerMonthQueryHandler |
18
|
|
|
) {} |
19
|
|
|
|
20
|
|
|
public async execute( |
21
|
|
|
query: GetUsersElementsQuery |
22
|
|
|
): Promise<UserElementsView[]> { |
23
|
|
|
|
24
|
|
|
const userViews: UserElementsView[] = []; |
25
|
|
|
|
26
|
|
|
const date = query.date; |
27
|
|
|
|
28
|
|
|
const [ users, leaves, mealTickets ] = await Promise.all([ |
29
|
|
|
this.userRepository.findUsersWithPayslipInfo(), |
30
|
|
|
this.leaveRepository.findAllMonthlyLeaves(date), |
31
|
|
|
this.getMealTicketsPerMonth.execute(new GetMealTicketsPerMonthQuery(date)) |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
const mealTicketsByUser: Record<string, number> = {}; |
35
|
|
|
mealTickets.forEach(view => { |
36
|
|
|
mealTicketsByUser[view.userId] = view.mealTickets; |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
for (const user of users) { |
40
|
|
|
userViews.push(new UserElementsView( |
41
|
|
|
user.getFirstName(), |
42
|
|
|
user.getLastName(), |
43
|
|
|
user.getUserAdministrative().getContract(), |
44
|
|
|
user.getUserAdministrative().isExecutivePosition(), |
45
|
|
|
user.getUserAdministrative().getJoiningDate(), |
46
|
|
|
user.getUserAdministrative().getAnnualEarnings() * 0.01, |
47
|
|
|
user.getUserAdministrative().getAnnualEarnings() / 1200, |
48
|
|
|
user.getUserAdministrative().getWorkingTime(), |
49
|
|
|
user.getUserAdministrative().getTransportFee() * 0.01, |
50
|
|
|
mealTicketsByUser[user.getId()], |
51
|
|
|
user.getUserAdministrative().haveHealthInsurance() ? 'yes' : 'no', |
52
|
|
|
0, |
53
|
|
|
0, |
54
|
|
|
0, |
55
|
|
|
0 |
56
|
|
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return userViews; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|