Passed
Pull Request — master (#75)
by Mathieu
01:51
created

server/src/Domain/FairCalendar/GetEventsOverview.ts   A

Complexity

Total Complexity 9
Complexity/F 9

Size

Lines of Code 85
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 72
mnd 8
bc 8
fnc 1
dl 0
loc 85
rs 10
bpm 8
cpm 9
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C GetEventsOverview.index 0 78 9
1
import {Event, EventType} from './Event.entity';
2
import {IEventsOverview} from './IEventsOverview';
3
4
export class GetEventsOverview {
5
  public index(events: Event[]): IEventsOverview {
6
    console.log('inNNNN');
7
    const eventsByDate = [];
8
    const overview: IEventsOverview = {
9
      mission: 0,
10
      dojo: 0,
11
      formationConference: 0,
12
      holiday: 0,
13
      medicalLeave: 0,
14
      support: 0,
15
      workFree: 0,
16
      other: 0,
17
      mealTicket: 0
18
    };
19
20
    for (const event of events) {
21
      const dayIndex = new Date(event.getDate()).getDate() - 1;
22
      const time = event.getTime() / 100;
23
      const type = event.getType();
24
25
      if (eventsByDate[dayIndex]) {
26
        eventsByDate[dayIndex].push({time, type});
27
      } else {
28
        eventsByDate[dayIndex] = [{time, type}];
29
      }
30
31
      switch (event.getType()) {
32
        case EventType.MISSION:
33
          overview.mission += time;
34
          break;
35
        case EventType.DOJO:
36
          overview.dojo += time;
37
          break;
38
        case EventType.FORMATION_CONFERENCE:
39
          overview.formationConference += time;
40
          break;
41
        case EventType.HOLIDAY:
42
          overview.holiday += time;
43
          break;
44
        case EventType.MEDICAL_LEAVE:
45
          overview.medicalLeave += time;
46
          break;
47
        case EventType.SUPPORT:
48
          overview.support += time;
49
          break;
50
        case EventType.WORK_FREE:
51
          overview.workFree += time;
52
          break;
53
        case EventType.OTHER:
54
          overview.other += time;
55
          break;
56
      }
57
    }
58
59
    for (const sortedEvent of eventsByDate) {
60
      if (!sortedEvent) {
61
        continue;
62
      }
63
64
      let totalPerDay = 0;
65
66
      for (const {time, type} of sortedEvent) {
67
        if (
68
          type !== EventType.WORK_FREE &&
69
          type !== EventType.MEDICAL_LEAVE &&
70
          type !== EventType.HOLIDAY &&
71
          type !== EventType.OTHER
72
        ) {
73
          totalPerDay += time;
74
        }
75
      }
76
77
      if (totalPerDay > 0.5) {
78
        overview.mealTicket++;
79
      }
80
    }
81
    console.log(overview);
82
    return overview;
83
  }
84
}
85