Passed
Pull Request — master (#210)
by Nicolas
03:45 queued 02:08
created

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

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 46
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetOnLeaveUsersQueryHandler.execute 0 27 2
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { IDateUtils } from 'src/Application/IDateUtils';
4
import { UserSummaryView } from '../../User/View/UserSummaryView';
5
import { ILeaveRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository';
6
import { GetOnLeaveUsersQuery } from "./GetOnLeaveUsersQuery";
7
import { OnLeaveUserView } from "../View/OnLeaveUserView";
8
9
@QueryHandler(GetOnLeaveUsersQuery)
10
export class GetOnLeaveUsersQueryHandler {
11
    constructor(
12
        @Inject('ILeaveRepository')
13
        private readonly leaveRepository: ILeaveRepository,
14
        @Inject('IDateUtils')
15
        private readonly dateUtils: IDateUtils
16
    ) {}
17
18
    public async execute({}: GetOnLeaveUsersQuery): Promise<Array<OnLeaveUserView>> {
19
        const onLeaveUserViews: OnLeaveUserView[] = [];
20
21
        const date = this.dateUtils.getCurrentDate();
22
        const day = date.getDate();
23
        const month = date.getMonth() + 1;
24
        const year = date.getFullYear();
25
26
        const leaves = await this.leaveRepository.findLeavesForDate(year + '-' + month + '-' + day);
27
28
        for (const leave of leaves) {
29
            const user = leave.getLeaveRequest().getUser();
30
31
            onLeaveUserViews.push(
32
                new OnLeaveUserView(
33
                    leave.getId(),
34
                    new UserSummaryView(
35
                        user.getId(),
36
                        user.getFirstName(),
37
                        user.getLastName()
38
                    )
39
                )
40
            );
41
        }
42
43
        return onLeaveUserViews;
44
    }
45
}
46