Passed
Pull Request — master (#113)
by Mathieu
02:09
created

server/src/Application/HumanResource/Holiday/Query/GetHolidaysQueryHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 57
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 46
mnd 3
bc 3
fnc 1
dl 0
loc 57
bpm 3
cpm 4
noi 0
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetHolidaysQueryHandler.execute 0 38 4
1
import {QueryHandler} from '@nestjs/cqrs';
2
import {GetHolidaysQuery} from './GetHolidaysQuery';
3
import {Inject} from '@nestjs/common';
4
import {IHolidayRepository} from 'src/Domain/HumanResource/Holiday/Repository/IHolidayRepository';
5
import {IDateUtils} from 'src/Application/IDateUtils';
6
import {HolidayView} from '../View/HolidayView';
7
import {UserSummaryView} from '../../User/View/UserSummaryView';
8
9
@QueryHandler(GetHolidaysQuery)
10
export class GetHolidaysQueryHandler {
11
  constructor(
12
    @Inject('IHolidayRepository')
13
    private readonly holidayRepository: IHolidayRepository,
14
    @Inject('IDateUtils')
15
    private readonly dateUtils: IDateUtils
16
  ) {}
17
18
  public async execute(query: GetHolidaysQuery): Promise<HolidayView[]> {
19
    const holidays = await this.holidayRepository.findHolidays();
20
    const holidayViews: HolidayView[] = [];
21
22
    for (const holiday of holidays) {
23
      const user = holiday.getUser();
24
      let duration = this.dateUtils.getWorkedDaysDuringAPeriod(
25
        new Date(holiday.getStartDate()),
26
        new Date(holiday.getEndDate())
27
      ).length;
28
29
      if (false === holiday.isStartsAllDay()) {
30
        duration -= 0.5;
31
      }
32
33
      if (false === holiday.isEndsAllDay()) {
34
        duration -= 0.5;
35
      }
36
37
      holidayViews.push(
38
        new HolidayView(
39
          holiday.getId(),
40
          holiday.getLeaveType(),
41
          holiday.getStatus(),
42
          holiday.getStartDate(),
43
          holiday.getEndDate(),
44
          duration,
45
          new UserSummaryView(
46
            user.getId(),
47
            user.getFirstName(),
48
            user.getLastName()
49
          )
50
        )
51
      );
52
    }
53
54
    return holidayViews;
55
  }
56
}
57