src/Application/HumanResource/Leave/Command/UpdateLeaveRequestCommandHandler.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 55
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A UpdateLeaveRequestCommandHandler.execute 0 37 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
4
import { UpdateLeaveRequestCommand } from './UpdateLeaveRequestCommand';
5
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
6
import { DoesLeaveRequestBelongToUser } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestBelongToUser';
7
import { LeaveRequestCantBeUpdatedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeUpdatedException';
8
9
@CommandHandler(UpdateLeaveRequestCommand)
10
export class UpdateLeaveRequestCommandHandler {
11
  constructor(
12
    @Inject('ILeaveRequestRepository')
13
    private readonly leaveRequestRepository: ILeaveRequestRepository,
14
    private readonly doesLeaveRequestBelongToUser: DoesLeaveRequestBelongToUser
15
  ) {}
16
17
  public async execute(command: UpdateLeaveRequestCommand): Promise<string> {
18
    const {
19
      id,
20
      type,
21
      startDate,
22
      startsAllDay,
23
      endDate,
24
      endsAllDay,
25
      comment,
26
      user
27
    } = command;
28
29
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
30
31
    if (!leaveRequest) {
32
      throw new LeaveRequestNotFoundException();
33
    }
34
35
    if (
36
      false ===
37
      this.doesLeaveRequestBelongToUser.isSatisfiedBy(leaveRequest, user)
38
    ) {
39
      throw new LeaveRequestCantBeUpdatedException();
40
    }
41
42
    leaveRequest.update(
43
      type,
44
      startDate,
45
      startsAllDay,
46
      endDate,
47
      endsAllDay,
48
      comment
49
    );
50
    await this.leaveRequestRepository.save(leaveRequest);
51
52
    return leaveRequest.getId();
53
  }
54
}
55