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

server/src/Application/FairCalendar/Query/GetMonthlyEventsQueryHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 54
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetMonthlyEventsQueryHandler.execute 0 31 4
1
import {Inject} from '@nestjs/common';
2
import {QueryHandler} from '@nestjs/cqrs';
3
import {GetMonthlyEventsQuery} from './GetMonthlyEventsQuery';
4
import {IEventRepository} from 'src/Domain/FairCalendar/Repository/IEventRepository';
5
import {EventView} from '../View/EventView';
6
import {IDateUtils} from 'src/Application/IDateUtils';
7
import {GetEventsOverview} from 'src/Domain/FairCalendar/GetEventsOverview';
8
import {MonthlyEventsView} from '../View/MonthlyEventsView';
9
import {ProjectView} from 'src/Application/Project/View/ProjectView';
10
import {TaskView} from 'src/Application/Task/View/TaskView';
11
12
@QueryHandler(GetMonthlyEventsQuery)
13
export class GetMonthlyEventsQueryHandler {
14
  constructor(
15
    @Inject('IEventRepository')
16
    private readonly eventRepository: IEventRepository,
17
    @Inject('IDateUtils')
18
    private readonly dateUtils: IDateUtils,
19
    private readonly getEventsOverview: GetEventsOverview
20
  ) {}
21
22
  public async execute(
23
    query: GetMonthlyEventsQuery
24
  ): Promise<MonthlyEventsView> {
25
    const {date, userId} = query;
26
    const eventViews: EventView[] = [];
27
    const events = await this.eventRepository.findMonthlyEvents(
28
      this.dateUtils.format(date, 'y-MM-dd'),
29
      userId
30
    );
31
32
    const overview = this.getEventsOverview.index(events);
33
34
    for (const event of events) {
35
      const project = event.getProject();
36
      const task = event.getTask();
37
38
      eventViews.push(
39
        new EventView(
40
          event.getId(),
41
          event.getType(),
42
          event.getTime() / 100,
43
          event.getDate(),
44
          event.getSummary(),
45
          project ? new ProjectView(project.getId(), project.getName()) : null,
46
          task ? new TaskView(task.getId(), task.getName()) : null
47
        )
48
      );
49
    }
50
51
    return new MonthlyEventsView(eventViews, overview);
52
  }
53
}
54