Passed
Push — master ( deb1f6...0e29a7 )
by Mathieu
02:21 queued 35s
created

GetFairCalendarOverview.index   A

Complexity

Conditions 4

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 25
c 0
b 0
f 0
rs 9.352
cc 4
1
import { Inject } from '@nestjs/common';
2
import { FairCalendarView } from 'src/Application/FairCalendar/View/FairCalendarView';
3
import { CooperativeNotFoundException } from '../Settings/Repository/CooperativeNotFoundException';
4
import { ICooperativeRepository } from '../Settings/Repository/ICooperativeRepository';
5
import { ICalendarOverview } from './ICalendarOverview';
6
7
export class GetFairCalendarOverview {
8
  constructor(
9
    @Inject('ICooperativeRepository')
10
    private readonly cooperativeRepository: ICooperativeRepository
11
  ) {}
12
13
  public async index(items: FairCalendarView[]): Promise<ICalendarOverview> {
14
    const cooperative = await this.cooperativeRepository.find();
15
    if (!cooperative) {
16
      throw new CooperativeNotFoundException();
17
    }
18
19
    const overviewInDays: ICalendarOverview = {
20
      mission: 0,
21
      dojo: 0,
22
      formationConference: 0,
23
      leave: 0,
24
      support: 0,
25
      other: 0
26
    };
27
28
    for (const { time, type: itemType } of items) {
29
      const type = itemType.startsWith('leave_') ? 'leave' : itemType;
30
      const days = time / cooperative.getDayDuration();
31
32
      overviewInDays[type] =
33
        Math.round((overviewInDays[type] + days) * 100) / 100;
34
    }
35
36
    return overviewInDays;
37
  }
38
}
39