Passed
Pull Request — master (#213)
by
unknown
01:45
created

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

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 44
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A UpdateLeaveRequestCommandHandler.execute 0 29 2
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
7
@CommandHandler(UpdateLeaveRequestCommand)
8
export class UpdateLeaveRequestCommandHandler {
9
  constructor(
10
    @Inject('ILeaveRequestRepository')
11
    private readonly leaveRequestRepository: ILeaveRequestRepository
12
  ) { }
13
14
  public async execute(command: UpdateLeaveRequestCommand): Promise<string> {
15
    const {
16
      id,
17
      type,
18
      startDate,
19
      startsAllDay,
20
      endDate,
21
      endsAllDay,
22
      comment
23
    } = command;
24
25
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
26
27
    if (!leaveRequest) {
28
      throw new LeaveRequestNotFoundException();
29
    }
30
31
    leaveRequest.update(
32
      type,
33
      startDate,
34
      startsAllDay,
35
      endDate,
36
      endsAllDay,
37
      comment
38
    );
39
    await this.leaveRequestRepository.save(leaveRequest);
40
41
    return leaveRequest.getId();
42
  }
43
}
44