Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 35 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 { DoesLeaveRequestBelongToUser } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestBelongToUser'; |
||
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 doesLeaveRequestBelongToUser: DoesLeaveRequestBelongToUser |
||
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 === |
||
27 | this.doesLeaveRequestBelongToUser.isSatisfiedBy(leaveRequest, owner) |
||
28 | ) { |
||
29 | throw new LeaveRequestCantBeRemovedException(); |
||
30 | } |
||
31 | |||
32 | this.leaveRequestRepository.remove(leaveRequest); |
||
33 | } |
||
34 | } |
||
35 |