Passed
Pull Request — master (#148)
by Mathieu
03:17
created

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

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 44
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A RefuseLeaveRequestCommandHandler.execute 0 23 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { RefuseLeaveRequestCommand } from './RefuseLeaveRequestCommand';
4
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
5
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
6
import { IDateUtils } from 'src/Application/IDateUtils';
7
import { LeaveRequestCantBeModeratedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeModeratedException';
8
import { CanLeaveRequestBeModerated } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeModerated';
9
10
@CommandHandler(RefuseLeaveRequestCommand)
11
export class RefuseLeaveRequestCommandHandler {
12
  constructor(
13
    @Inject('ILeaveRequestRepository')
14
    private readonly leaveRequestRepository: ILeaveRequestRepository,
15
    @Inject('IDateUtils')
16
    private readonly dateUtils: IDateUtils,
17
    private readonly canLeaveRequestBeModerated: CanLeaveRequestBeModerated
18
  ) {}
19
20
  public async execute(command: RefuseLeaveRequestCommand): Promise<string> {
21
    const {moderator, id, moderationComment} = command;
22
23
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
24
    if (!leaveRequest) {
25
      throw new LeaveRequestNotFoundException();
26
    }
27
28
    if (
29
      false === this.canLeaveRequestBeModerated.isSatisfiedBy(leaveRequest, moderator)
30
    ) {
31
      throw new LeaveRequestCantBeModeratedException();
32
    }
33
34
    leaveRequest.refuse(
35
      moderator,
36
      this.dateUtils.getCurrentDateToISOString(),
37
      moderationComment
38
    );
39
    await this.leaveRequestRepository.save(leaveRequest);
40
41
    return leaveRequest.getId();
42
  }
43
}
44