|
1
|
|
|
import { QueryHandler } from '@nestjs/cqrs'; |
|
2
|
|
|
import { Inject } from '@nestjs/common'; |
|
3
|
|
|
import { IMealTicketRemovalRepository } from 'src/Domain/HumanResource/MealTicket/Repository/IMealTicketRemovalRepository'; |
|
4
|
|
|
import { CountMealTicketPerMonthQuery } from './CountMealTicketPerMonthQuery'; |
|
5
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
|
6
|
|
|
import { AvailableMealTicketStrategy } from 'src/Domain/HumanResource/MealTicket/Strategy/AvailableMealTicketStrategy'; |
|
7
|
|
|
import { MealTicketGroupedByMonthSummary } from 'src/Domain/HumanResource/MealTicket/Strategy/MealTicketGroupedByMonthSummary'; |
|
8
|
|
|
import { MealTicketRemovalSummaryDTO } from 'src/Infrastructure/HumanResource/MealTicket/DTO/MealTicketRemovalSummaryDTO'; |
|
9
|
|
|
import { MealTicketSummaryView } from '../Views/MealTicketSummaryView'; |
|
10
|
|
|
|
|
11
|
|
|
@QueryHandler(CountMealTicketPerMonthQuery) |
|
12
|
|
|
export class CountMealTicketPerMonthQueryHandler { |
|
13
|
|
|
constructor( |
|
14
|
|
|
@Inject('IDateUtils') |
|
15
|
|
|
private readonly dateUtils: IDateUtils, |
|
16
|
|
|
@Inject('IMealTicketRemovalRepository') |
|
17
|
|
|
private readonly mealTicketRemovalRepository: IMealTicketRemovalRepository |
|
18
|
|
|
) { } |
|
19
|
|
|
|
|
20
|
|
|
private buildMealTicketSummary = ( |
|
21
|
|
|
mealTicketRemovals: MealTicketRemovalSummaryDTO[], |
|
22
|
|
|
mealTicketGroupedByMonthSummaries: MealTicketGroupedByMonthSummary[] |
|
23
|
|
|
): MealTicketSummaryView[] => { |
|
24
|
|
|
return mealTicketGroupedByMonthSummaries.map(summary => { |
|
25
|
|
|
const month = summary.month; |
|
26
|
|
|
const base = summary.mealTicketCount; |
|
27
|
|
|
let total = base; |
|
28
|
|
|
|
|
29
|
|
|
const foundTicketRemoval = mealTicketRemovals.find(item => { |
|
30
|
|
|
return summary.month == item.monthnumber; |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
if (foundTicketRemoval) { |
|
34
|
|
|
total = total - foundTicketRemoval.count; |
|
35
|
|
|
} |
|
36
|
|
|
return new MealTicketSummaryView( |
|
37
|
|
|
month, |
|
38
|
|
|
base, |
|
39
|
|
|
foundTicketRemoval ? foundTicketRemoval.count : 0, |
|
40
|
|
|
total |
|
41
|
|
|
); |
|
42
|
|
|
}); |
|
43
|
|
|
}; |
|
44
|
|
|
|
|
45
|
|
|
public async execute( |
|
46
|
|
|
command: CountMealTicketPerMonthQuery |
|
47
|
|
|
): Promise<MealTicketSummaryView[]> { |
|
48
|
|
|
const { user, currentDate } = command; |
|
49
|
|
|
const workingDaysByMonth = this.dateUtils.getAllWorkingDayOfYearByMonth( |
|
50
|
|
|
currentDate |
|
51
|
|
|
); |
|
52
|
|
|
const yearlyAvailableMealTickets = AvailableMealTicketStrategy.getMealTicketCountForEachMonthOfTheYear( |
|
53
|
|
|
workingDaysByMonth |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
const mealTicketRemovals = await this.mealTicketRemovalRepository.getAllByUserGroupedByMonth( |
|
57
|
|
|
user, |
|
58
|
|
|
currentDate |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return this.buildMealTicketSummary( |
|
63
|
|
|
mealTicketRemovals, |
|
64
|
|
|
yearlyAvailableMealTickets |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|