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

server/src/Application/FairCalendar/Command/AddEventCommandHandler.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 56
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 49
mnd 2
bc 2
fnc 1
dl 0
loc 56
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A AddEventCommandHandler.execute 0 27 3
1
import {CommandHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {AddEventCommand} from './AddEventCommand';
4
import {ITaskRepository} from 'src/Domain/Task/Repository/ITaskRepository';
5
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository';
6
import {IEventRepository} from 'src/Domain/FairCalendar/Repository/IEventRepository';
7
import {IsMaximumTimeSpentReached} from 'src/Domain/FairCalendar/Specification/IsMaximumTimeSpentReached';
8
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity';
9
import {MaximumEventReachedException} from 'src/Domain/FairCalendar/Exception/MaximumEventReachedException';
10
import {IDateUtils} from 'src/Application/IDateUtils';
11
import {ProjectOrTaskMissingException} from 'src/Domain/FairCalendar/Exception/ProjectOrTaskMissingException';
12
import {AbstractProjectAndTaskGetter} from './AbstractProjectAndTaskGetter';
13
14
@CommandHandler(AddEventCommand)
15
export class AddEventCommandHandler extends AbstractProjectAndTaskGetter {
16
  constructor(
17
    @Inject('ITaskRepository') taskRepository: ITaskRepository,
18
    @Inject('IProjectRepository') projectRepository: IProjectRepository,
19
    @Inject('IEventRepository')
20
    private readonly eventRepository: IEventRepository,
21
    @Inject('IDateUtils')
22
    private readonly dateUtils: IDateUtils,
23
    private readonly isMaximumTimeSpentReached: IsMaximumTimeSpentReached
24
  ) {
25
    super(taskRepository, projectRepository);
26
  }
27
28
  public async execute(command: AddEventCommand): Promise<string> {
29
    const {type, date, projectId, taskId, summary, time, user} = command;
30
31
    if (type === EventType.MISSION && (!projectId || !taskId)) {
32
      throw new ProjectOrTaskMissingException();
33
    }
34
35
    const project = await this.getProject(projectId);
36
    const task = await this.getTask(taskId);
37
    const event = new Event(
38
      type,
39
      user,
40
      time,
41
      this.dateUtils.format(date, 'y-MM-dd'),
42
      project,
43
      task,
44
      summary
45
    );
46
47
    if (true === (await this.isMaximumTimeSpentReached.isSatisfiedBy(event))) {
48
      throw new MaximumEventReachedException();
49
    }
50
51
    const savedEvent = await this.eventRepository.save(event);
52
53
    return savedEvent.getId();
54
  }
55
}
56