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

AcceptLeaveRequestCommandHandler.execute   B

Complexity

Conditions 4

Size

Total Lines 60
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 60
rs 8.8478
c 0
b 0
f 0
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 { IDateUtils } from 'src/Application/IDateUtils';
4
import { CanLeaveRequestBeModerated } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeModerated';
5
import { AcceptLeaveRequestCommand } from './AcceptLeaveRequestCommand';
6
import { IEventBus } from 'src/Application/IEventBus';
7
import { AcceptedLeaveRequestEvent } from '../Event/AcceptedLeaveRequestEvent';
8
import { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod';
9
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException';
10
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
11
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
12
import { LeaveRequestCantBeModeratedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeModeratedException';
13
import { CreateNotificationCommand } from 'src/Application/Notification/Command/CreateNotificationCommand';
14
import { NotificationType } from 'src/Domain/Notification/Notification.entity';
15
import { ICommandBus } from 'src/Application/ICommandBus';
16
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator';
17
18
@CommandHandler(AcceptLeaveRequestCommand)
19
export class AcceptLeaveRequestCommandHandler {
20
  constructor(
21
    @Inject('ILeaveRequestRepository')
22
    private readonly leaveRequestRepository: ILeaveRequestRepository,
23
    @Inject('IEventBus')
24
    private readonly eventBus: IEventBus,
25
    @Inject('IDateUtils')
26
    private readonly dateUtils: IDateUtils,
27
    private readonly canLeaveRequestBeModerated: CanLeaveRequestBeModerated,
28
    private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod,
29
    @Inject('ICommandBus')
30
    private readonly commandBus: ICommandBus,
31
    @Inject('ITranslator')
32
    private readonly translator: ITranslator
33
  ) {}
34
35
  public async execute(command: AcceptLeaveRequestCommand): Promise<string> {
36
    const { moderator, moderationComment, id } = command;
37
38
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
39
    if (!leaveRequest) {
40
      throw new LeaveRequestNotFoundException();
41
    }
42
43
    if (
44
      false ===
45
      this.canLeaveRequestBeModerated.isSatisfiedBy(leaveRequest, moderator)
46
    ) {
47
      throw new LeaveRequestCantBeModeratedException();
48
    }
49
50
    if (
51
      true ===
52
      (await this.doesLeaveExistForPeriod.isSatisfiedBy(
53
        leaveRequest.getUser(),
54
        leaveRequest.getStartDate(),
55
        leaveRequest.getEndDate()
56
      ))
57
    ) {
58
      throw new EventsOrLeavesAlreadyExistForThisPeriodException();
59
    }
60
61
    leaveRequest.accept(
62
      moderator,
63
      this.dateUtils.getCurrentDateToISOString(),
64
      moderationComment
65
    );
66
67
    await this.leaveRequestRepository.save(leaveRequest);
68
    this.eventBus.publish(new AcceptedLeaveRequestEvent(leaveRequest));
69
70
    this.commandBus.execute(
71
      new CreateNotificationCommand(
72
        NotificationType.REACTION,
73
        this.translator.translate(
74
          'leave-requests-approve-notification-emoji-name'
75
        ),
76
        leaveRequest
77
      )
78
    );
79
80
    this.commandBus.execute(
81
      new CreateNotificationCommand(
82
        NotificationType.COMMENT,
83
        this.translator.translate(
84
          'leave-requests-approve-notification-message',
85
          {
86
            moderatorFirstName: moderator.getFirstName()
87
          }
88
        ),
89
        leaveRequest
90
      )
91
    );
92
93
    return leaveRequest.getId();
94
  }
95
}
96