Completed
Push — master ( fa76b5...b46f0e )
by Mathieu
21s queued 11s
created

server/src/Application/HumanResource/Leave/Query/GetLeaveRequestsQueryHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 62
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

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