Passed
Push — master ( 05ae14...4ed66d )
by
unknown
02:34
created

GetLeavesCalendarQueryHandler   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 41
dl 0
loc 53
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 44 1
1
import { Inject } from '@nestjs/common';
2
import { QueryHandler } from '@nestjs/cqrs';
3
import { GetLeavesCalendarQuery } from './GetLeavesCalendarQuery';
4
import { IDateUtils } from 'src/Application/IDateUtils';
5
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
6
7
@QueryHandler(GetLeavesCalendarQuery)
8
export class GetLeavesCalendarQueryHandler {
9
  constructor(
10
    @Inject('IDateUtils')
11
    private readonly dateUtils: IDateUtils,
12
    @Inject('ILeaveRequestRepository')
13
    private readonly leaveRequestRepository: ILeaveRequestRepository
14
  ) {}
15
16
  async execute(query: GetLeavesCalendarQuery): Promise<string> {
17
    const leaveRequests = await this.leaveRequestRepository.findAcceptedLeaveRequests();
18
19
    // See: https://www.rfc-editor.org/rfc/rfc5545
20
21
    const lines = [
22
      'BEGIN:VCALENDAR',
23
      'VERSION:2.0',
24
      'PRODID:-//Fairness//Permacoop//FR',
25
      'CALSCALE:GREGORIAN',
26
      'X-WR-CALNAME:Congés Fairness',
27
      'X-APPLE-CALENDAR-COLOR:#00CA9E'
28
    ];
29
30
    leaveRequests.forEach(leaveRequest => {
31
      // See https://www.rfc-editor.org/rfc/rfc5545#section-3.6.1
32
      // DTSTART is inclusive, and DTEND is non-inclusive.
33
      // But we store leave request dates as inclusive on both ends.
34
      const inclusiveStartDate = new Date(leaveRequest.getStartDate());
35
      const inclusiveEndDate = new Date(leaveRequest.getEndDate());
36
      const nonInclusiveEndDate = this.dateUtils.addDaysToDate(
37
        inclusiveEndDate,
38
        1
39
      );
40
41
      lines.push(
42
        'BEGIN:VEVENT',
43
        `DTSTART;VALUE=DATE:${this.dateUtils.format(
44
          inclusiveStartDate,
45
          'yyyyMMdd'
46
        )}`,
47
        `DTEND;VALUE=DATE:${this.dateUtils.format(
48
          nonInclusiveEndDate,
49
          'yyyyMMdd'
50
        )}`,
51
        `SUMMARY:Congés ${leaveRequest.getUser().getFullName()}`,
52
        'END:VEVENT'
53
      );
54
    });
55
56
    lines.push('END:VCALENDAR');
57
58
    return lines.join('\r\n');
59
  }
60
}
61