Passed
Pull Request — master (#431)
by
unknown
05:25
created

CreateLeaveRequestCommandHandler.execute   B

Complexity

Conditions 4

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 58
c 0
b 0
f 0
rs 8.896
cc 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod';
9
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException';
10
import { DoesLeaveRequestLackPostponedWorkedFreeDays } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestLackPostponedWorkedFreeDays';
11
12
@CommandHandler(CreateLeaveRequestCommand)
13
export class CreateLeaveRequestCommandHandler {
14
  constructor(
15
    @Inject('ILeaveRequestRepository')
16
    private readonly leaveRequestRepository: ILeaveRequestRepository,
17
    private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod,
18
    private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod,
19
    private readonly DoesLeaveRequestLackPostponedWorkedFreeDays: DoesLeaveRequestLackPostponedWorkedFreeDays
20
  ) {}
21
22
  public async execute(command: CreateLeaveRequestCommand): Promise<string> {
23
    const {
24
      user,
25
      endDate,
26
      endsAllDay,
27
      type,
28
      startDate,
29
      startsAllDay
30
    } = command;
31
32
    let { comment } = command;
33
34
    if (
35
      true ===
36
      (await this.doesLeaveRequestExistForPeriod.isSatisfiedBy(
37
        user,
38
        startDate,
39
        endDate
40
      ))
41
    ) {
42
      throw new LeaveRequestAlreadyExistForThisPeriodException();
43
    }
44
45
    if (
46
      true ===
47
      (await this.doesLeaveExistForPeriod.isSatisfiedBy(
48
        user,
49
        startDate,
50
        endDate
51
      ))
52
    ) {
53
      throw new EventsOrLeavesAlreadyExistForThisPeriodException();
54
    }
55
56
    if (
57
      true ===
58
      this.DoesLeaveRequestLackPostponedWorkedFreeDays.isSatisfiedBy(
59
        startDate,
60
        endDate
61
      )
62
    ) {
63
      comment += ` ⚠️ Cette demande de congé contient un ou plusieurs jour(s) férié(s) glissant non pris en compte.`;
64
    }
65
66
    const leaveRequest = await this.leaveRequestRepository.save(
67
      new LeaveRequest(
68
        user,
69
        type,
70
        startDate,
71
        startsAllDay,
72
        endDate,
73
        endsAllDay,
74
        comment
75
      )
76
    );
77
78
    return leaveRequest.getId();
79
  }
80
}
81