Passed
Pull Request — master (#148)
by Mathieu
01:37
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 RefuseLeaveCommandHandler.execute 0 23 3
1
import {Inject} from '@nestjs/common';
2
import {CommandHandler} from '@nestjs/cqrs';
3
import {RefuseLeaveCommand} from './RefuseLeaveRequestCommand';
4
import {ILeaveRepository} from 'src/Infrastructure/HumanResource/Leave/Repository/node_modules/src/Domain/HumanResource/Leave/Repository/ILeaveRepository';
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(RefuseLeaveCommand)
11
export class RefuseLeaveCommandHandler {
12
  constructor(
13
    @Inject('ILeaveRepository')
14
    private readonly leaveRepository: ILeaveRepository,
15
    @Inject('IDateUtils')
16
    private readonly dateUtils: IDateUtils,
17
    private readonly CanLeaveRequestBeModerated: CanLeaveRequestBeModerated
18
  ) {}
19
20
  public async execute(command: RefuseLeaveCommand): Promise<string> {
21
    const {moderator, id, moderationComment} = command;
22
23
    const leave = await this.leaveRepository.findOneById(id);
24
    if (!leave) {
25
      throw new LeaveRequestNotFoundException();
26
    }
27
28
    if (
29
      false === this.CanLeaveRequestBeModerated.isSatisfiedBy(leave, moderator)
30
    ) {
31
      throw new LeaveRequestCantBeModeratedException();
32
    }
33
34
    leave.refuse(
35
      moderator,
36
      this.dateUtils.getCurrentDateToISOString(),
37
      moderationComment
38
    );
39
    await this.leaveRepository.save(leave);
40
41
    return leave.getId();
42
  }
43
}
44