Passed
Pull Request — master (#188)
by Mathieu
01:57
created

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

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 34
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A DeleteLeaveRequestCommandHandler.execute 0 16 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { DeleteLeaveRequestCommand } from './DeleteLeaveRequestCommand';
4
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
5
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
6
import { CanLeaveRequestBeRemoved } from 'src/Domain/HumanResource/Leave/Specification/CanLeaveRequestBeRemoved';
7
import { LeaveRequestCantBeRemovedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeRemovedException';
8
9
@CommandHandler(DeleteLeaveRequestCommand)
10
export class DeleteLeaveRequestCommandHandler {
11
  constructor(
12
    @Inject('ILeaveRequestRepository')
13
    private readonly leaveRequestRepository: ILeaveRequestRepository,
14
    private readonly canLeaveRequestBeRemoved: CanLeaveRequestBeRemoved
15
  ) {}
16
17
  public async execute(command: DeleteLeaveRequestCommand): Promise<void> {
18
    const { owner, id } = command;
19
20
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
21
    if (!leaveRequest) {
22
      throw new LeaveRequestNotFoundException();
23
    }
24
25
    if (
26
      false === this.canLeaveRequestBeRemoved.isSatisfiedBy(leaveRequest, owner)
27
    ) {
28
      throw new LeaveRequestCantBeRemovedException();
29
    }
30
31
    this.leaveRequestRepository.remove(leaveRequest);
32
  }
33
}
34