Passed
Pull Request — master (#148)
by Mathieu
01:37
created

server/src/Application/HumanResource/Leave/Command/CreateLeaveRequestCommandHandler.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 68
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreateLeaveCommandHandler.execute 0 47 3
1
import {Inject} from '@nestjs/common';
2
import {CommandHandler} from '@nestjs/cqrs';
3
import {CreateLeaveCommand} from './CreateLeaveRequestCommand';
4
import {ILeaveRepository} from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository';
5
import {Leave} from 'src/Domain/HumanResource/Leave/Leave.entity';
6
import {DoesLeaveRequestExistForPeriod} from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod';
7
import {LeaveAlreadyExistForThisPeriodException} from 'src/Domain/HumanResource/Leave/Exception/LeaveAlreadyExistForThisPeriodException';
8
import {DoesEventsExistForPeriod} from 'src/Domain/FairCalendar/Specification/DoesEventsExistForPeriod';
9
import {EventsAlreadyExistForThisPeriodException} from 'src/Domain/FairCalendar/Exception/EventsAlreadyExistForThisPeriodException';
10
11
@CommandHandler(CreateLeaveCommand)
12
export class CreateLeaveCommandHandler {
13
  constructor(
14
    @Inject('ILeaveRepository')
15
    private readonly leaveRepository: ILeaveRepository,
16
    private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod,
17
    private readonly doesEventsExistForPeriod: DoesEventsExistForPeriod
18
  ) {}
19
20
  public async execute(command: CreateLeaveCommand): Promise<string> {
21
    const {
22
      user,
23
      endDate,
24
      endsAllDay,
25
      leaveType,
26
      startDate,
27
      startsAllDay,
28
      comment
29
    } = command;
30
31
    if (
32
      true ===
33
      (await this.doesLeaveRequestExistForPeriod.isSatisfiedBy(
34
        user,
35
        startDate,
36
        endDate
37
      ))
38
    ) {
39
      throw new LeaveAlreadyExistForThisPeriodException();
40
    }
41
42
    if (
43
      true ===
44
      (await this.doesEventsExistForPeriod.isSatisfiedBy(
45
        user,
46
        startDate,
47
        endDate
48
      ))
49
    ) {
50
      throw new EventsAlreadyExistForThisPeriodException();
51
    }
52
53
    const leave = await this.leaveRepository.save(
54
      new Leave(
55
        user,
56
        leaveType,
57
        startDate,
58
        startsAllDay,
59
        endDate,
60
        endsAllDay,
61
        comment
62
      )
63
    );
64
65
    return leave.getId();
66
  }
67
}
68