Total Complexity | 4 |
Complexity/F | 4 |
Lines of Code | 57 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |