Total Complexity | 4 |
Complexity/F | 4 |
Lines of Code | 46 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { Inject } from '@nestjs/common'; |
||
2 | import { IDateUtils } from 'src/Application/IDateUtils'; |
||
3 | import { IMealTicketRemovalRepository } from '../Repository/IMealTicketRemovalRepository'; |
||
4 | import { MealTicketRemoval } from '../MealTicketRemoval.entity'; |
||
5 | import { LeaveRequest } from '../../Leave/LeaveRequest.entity'; |
||
6 | import { IsMealTicketRemovalAlreadyExist } from '../Specification/IsMealTicketRemovalAlreadyExist'; |
||
7 | |||
8 | export class LeaveRequestToMealTicketRemovalConverter { |
||
9 | constructor( |
||
10 | @Inject('IMealTicketRemovalRepository') |
||
11 | private readonly mealTicketRemovalRepository: IMealTicketRemovalRepository, |
||
12 | @Inject('IDateUtils') |
||
13 | private readonly dateUtils: IDateUtils, |
||
14 | private readonly isMealTicketRemovalAlreadyExist: IsMealTicketRemovalAlreadyExist, |
||
15 | ) {} |
||
16 | |||
17 | public async convert(leaveRequest: LeaveRequest): Promise<void> { |
||
18 | const exceptions: MealTicketRemoval[] = []; |
||
19 | const dates = this.dateUtils.getWorkedDaysDuringAPeriod( |
||
20 | new Date(leaveRequest.getStartDate()), |
||
21 | new Date(leaveRequest.getEndDate()) |
||
22 | ); |
||
23 | |||
24 | if (!dates || 0 === dates.length) { |
||
25 | return; |
||
26 | } |
||
27 | |||
28 | const user = leaveRequest.getUser(); |
||
29 | |||
30 | for (const date of dates) { |
||
31 | if (true === (await this.isMealTicketRemovalAlreadyExist.isSatisfiedBy(user, new Date(date)))) { |
||
32 | continue; |
||
33 | } |
||
34 | |||
35 | exceptions.push( |
||
36 | new MealTicketRemoval( |
||
37 | date.toISOString(), |
||
38 | leaveRequest.getUser() |
||
39 | ) |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | this.mealTicketRemovalRepository.save(exceptions); |
||
44 | } |
||
45 | } |
||
46 |