Total Complexity | 4 |
Complexity/F | 4 |
Lines of Code | 62 |
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 | import {Pagination} from 'src/Application/Common/Pagination'; |
||
9 | |||
10 | @QueryHandler(GetHolidaysQuery) |
||
11 | export class GetHolidaysQueryHandler { |
||
12 | constructor( |
||
13 | @Inject('IHolidayRepository') |
||
14 | private readonly holidayRepository: IHolidayRepository, |
||
15 | @Inject('IDateUtils') |
||
16 | private readonly dateUtils: IDateUtils |
||
17 | ) {} |
||
18 | |||
19 | public async execute( |
||
20 | query: GetHolidaysQuery |
||
21 | ): Promise<Pagination<HolidayView>> { |
||
22 | const holidayViews: HolidayView[] = []; |
||
23 | const [holidays, total] = await this.holidayRepository.findHolidays( |
||
24 | query.page |
||
25 | ); |
||
26 | |||
27 | for (const holiday of holidays) { |
||
28 | const user = holiday.getUser(); |
||
29 | let duration = this.dateUtils.getWorkedDaysDuringAPeriod( |
||
30 | new Date(holiday.getStartDate()), |
||
31 | new Date(holiday.getEndDate()) |
||
32 | ).length; |
||
33 | |||
34 | if (false === holiday.isStartsAllDay()) { |
||
35 | duration -= 0.5; |
||
36 | } |
||
37 | |||
38 | if (false === holiday.isEndsAllDay()) { |
||
39 | duration -= 0.5; |
||
40 | } |
||
41 | |||
42 | holidayViews.push( |
||
43 | new HolidayView( |
||
44 | holiday.getId(), |
||
45 | holiday.getLeaveType(), |
||
46 | holiday.getStatus(), |
||
47 | holiday.getStartDate(), |
||
48 | holiday.getEndDate(), |
||
49 | duration, |
||
50 | new UserSummaryView( |
||
51 | user.getId(), |
||
52 | user.getFirstName(), |
||
53 | user.getLastName() |
||
54 | ) |
||
55 | ) |
||
56 | ); |
||
57 | } |
||
58 | |||
59 | return new Pagination<HolidayView>(holidayViews, total); |
||
60 | } |
||
61 | } |
||
62 |