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

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

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 39
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 32
mnd 3
bc 3
fnc 1
dl 0
loc 39
bpm 3
cpm 4
noi 0
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetFairCalendarOverview.index 0 25 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