Passed
Pull Request — master (#287)
by
unknown
02:11
created

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

Complexity

Total Complexity 1
Complexity/F 1

Size

Lines of Code 51
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetLeavesCalendarQueryHandler.execute 0 34 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
      lines.push(
32
        'BEGIN:VEVENT',
33
        `DTSTART;VALUE=DATE:${this.dateUtils.format(
34
          new Date(leaveRequest.getStartDate()),
35
          'yyyyMMdd'
36
        )}`,
37
        `DTEND;VALUE=DATE:${this.dateUtils.format(
38
          new Date(leaveRequest.getEndDate()),
39
          'yyyyMMdd'
40
        )}`,
41
        `SUMMARY:Congés ${leaveRequest.getUser().getFullName()}`,
42
        'END:VEVENT'
43
      );
44
    });
45
46
    lines.push('END:VCALENDAR');
47
48
    return lines.join('\r\n');
49
  }
50
}
51