|
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
|
|
|
|