Passed
Push — master ( 908d5b...34aa9c )
by Mathieu
03:32 queued 01:21
created

server/src/Application/HumanResource/MealTicket/Query/CountMealTicketPerMonthQueryHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 68
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 4
mnd 2
bc 2
fnc 2
bpm 1
cpm 2
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A CountMealTicketPerMonthQueryHandler.buildMealTicketSummary 0 21 3
A CountMealTicketPerMonthQueryHandler.execute 0 21 1
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