Passed
Pull Request — master (#148)
by Mathieu
04:04
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 CreateLeaveRequestCommandHandler.execute 0 47 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand';
4
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
5
import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
6
import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod';
7
import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException';
8
import { DoesEventsExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesEventsExistForPeriod';
9
import { EventsAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsAlreadyExistForThisPeriodException';
10
11
@CommandHandler(CreateLeaveRequestCommand)
12
export class CreateLeaveRequestCommandHandler {
13
  constructor(
14
    @Inject('ILeaveRequestRepository')
15
    private readonly leaveRequestRepository: ILeaveRequestRepository,
16
    private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod,
17
    private readonly doesEventsExistForPeriod: DoesEventsExistForPeriod
18
  ) {}
19
20
  public async execute(command: CreateLeaveRequestCommand): Promise<string> {
21
    const {
22
      user,
23
      endDate,
24
      endsAllDay,
25
      type,
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 LeaveRequestAlreadyExistForThisPeriodException();
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 leaveRequest = await this.leaveRequestRepository.save(
54
      new LeaveRequest(
55
        user,
56
        type,
57
        startDate,
58
        startsAllDay,
59
        endDate,
60
        endsAllDay,
61
        comment
62
      )
63
    );
64
65
    return leaveRequest.getId();
66
  }
67
}
68