Passed
Pull Request — master (#182)
by
unknown
07:15
created

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

Complexity

Total Complexity 4
Complexity/F 2

Size

Lines of Code 69
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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