Completed
Push — master ( 1360ae...ad0215 )
by Mathieu
17s queued 12s
created

server/src/Domain/FairCalendar/Converter/HolidayToEventsConverter.ts   A

Complexity

Total Complexity 7
Complexity/F 2.33

Size

Lines of Code 62
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 48
mnd 4
bc 4
fnc 3
dl 0
loc 62
rs 10
bpm 1.3333
cpm 2.3333
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A HolidayToEventsConverter.convert 0 22 3
A HolidayToEventsConverter.getType 0 5 2
A HolidayToEventsConverter.getTime 0 11 2
1
import {Inject} from '@nestjs/common';
2
import {
3
  Holiday,
4
  HolidayLeaveType
5
} from '../../HumanResource/Holiday/Holiday.entity';
6
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity';
7
import {IEventRepository} from '../Repository/IEventRepository';
8
import {IDateUtils} from 'src/Application/IDateUtils';
9
10
export class HolidayToEventsConverter {
11
  constructor(
12
    @Inject('IEventRepository')
13
    private readonly eventRepository: IEventRepository,
14
    @Inject('IDateUtils')
15
    private readonly dateUtils: IDateUtils
16
  ) {}
17
18
  public convert(holiday: Holiday): void {
19
    const user = holiday.getUser();
20
    const dates = this.dateUtils.getWorkedDaysDuringAPeriod(
21
      new Date(holiday.getStartDate()),
22
      new Date(holiday.getEndDate())
23
    );
24
25
    if (!dates || 0 === dates.length) {
26
      return;
27
    }
28
29
    const firstDate = dates[0].toISOString();
30
    const lastDate = dates[dates.length - 1].toISOString();
31
32
    for (const date of dates) {
33
      this.eventRepository.save(
34
        new Event(
35
          this.getType(holiday),
36
          user,
37
          this.getTime(holiday, firstDate, lastDate, date.toISOString()),
38
          date.toISOString()
39
        )
40
      );
41
    }
42
  }
43
44
  private getTime(
45
    holiday: Holiday,
46
    firstDate: string,
47
    lastDate: string,
48
    currentDate: string
49
  ): number {
50
    return (firstDate === currentDate && false === holiday.isStartsAllDay()) ||
51
      (lastDate === currentDate && false === holiday.isEndsAllDay())
52
      ? 50
53
      : 100;
54
  }
55
56
  private getType(holiday: Holiday): EventType {
57
    return holiday.getLeaveType() === HolidayLeaveType.MEDICAL
58
      ? EventType.MEDICAL_LEAVE
59
      : EventType.HOLIDAY;
60
  }
61
}
62