Passed
Pull Request — master (#436)
by Mathieu
02:46
created

CreateLeaveRequestCommandHandler.execute   B

Complexity

Conditions 3

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 55
rs 8.9919
c 0
b 0
f 0
cc 3

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 { IMattermostNotifier } from 'src/Application/IMattermostNotifier';
11
import { ICommandBus } from 'src/Application/ICommandBus';
12
import { CreateNotificationCommand } from 'src/Application/Notification/Command/CreateNotificationCommand';
13
import { NotificationType } from 'src/Domain/Notification/Notification.entity';
14
15
@CommandHandler(CreateLeaveRequestCommand)
16
export class CreateLeaveRequestCommandHandler {
17
  constructor(
18
    @Inject('ILeaveRequestRepository')
19
    private readonly leaveRequestRepository: ILeaveRequestRepository,
20
    @Inject('ICommandBus')
21
    private readonly commandBus: ICommandBus,
22
    private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod,
23
    private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod,
24
  ) {}
25
26
  public async execute(command: CreateLeaveRequestCommand): Promise<string> {
27
    const {
28
      user,
29
      endDate,
30
      endsAllDay,
31
      type,
32
      startDate,
33
      startsAllDay,
34
      comment
35
    } = command;
36
37
    if (
38
      true ===
39
      (await this.doesLeaveRequestExistForPeriod.isSatisfiedBy(
40
        user,
41
        startDate,
42
        endDate
43
      ))
44
    ) {
45
      throw new LeaveRequestAlreadyExistForThisPeriodException();
46
    }
47
48
    if (
49
      true ===
50
      (await this.doesLeaveExistForPeriod.isSatisfiedBy(
51
        user,
52
        startDate,
53
        endDate
54
      ))
55
    ) {
56
      throw new EventsOrLeavesAlreadyExistForThisPeriodException();
57
    }
58
59
    const leaveRequest = await this.leaveRequestRepository.save(
60
      new LeaveRequest(
61
        user,
62
        type,
63
        startDate,
64
        startsAllDay,
65
        endDate,
66
        endsAllDay,
67
        comment
68
      )
69
    );
70
71
    this.commandBus.execute(
72
      new CreateNotificationCommand(
73
        NotificationType.POST,
74
        'Demande de congés',
75
        leaveRequest
76
      )
77
    );
78
79
    return leaveRequest.getId();
80
  }
81
}
82