src/Application/HumanResource/MealTicket/Command/CreateMealTicketRemovalCommandHandler.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 43
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreateMealTicketRemovalCommandHandler.execute 0 20 3
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { IMealTicketRemovalRepository } from 'src/Domain/HumanResource/MealTicket/Repository/IMealTicketRemovalRepository';
4
import { MealTicketRemoval } from 'src/Domain/HumanResource/MealTicket/MealTicketRemoval.entity';
5
import { MealTicketRemovalAlreadyExistException } from 'src/Domain/HumanResource/MealTicket/Exception/MealTicketRemovalAlreadyExistException';
6
import { IsMealTicketRemovalAlreadyExist } from 'src/Domain/HumanResource/MealTicket/Specification/IsMealTicketRemovalAlreadyExist';
7
import { CreateMealTicketRemovalCommand } from './CreateMealTicketRemovalCommand';
8
import { IDateUtils } from 'src/Application/IDateUtils';
9
import { NotAWorkingDateException } from 'src/Domain/HumanResource/MealTicket/Exception/NotAWorkingDateException';
10
11
@CommandHandler(CreateMealTicketRemovalCommand)
12
export class CreateMealTicketRemovalCommandHandler {
13
  constructor(
14
    @Inject('IMealTicketRemovalRepository')
15
    private readonly mealTicketRemovalRepository: IMealTicketRemovalRepository,
16
    @Inject('IDateUtils')
17
    private readonly dateUtils: IDateUtils,
18
    private readonly isMealTicketRemovalAlreadyExist: IsMealTicketRemovalAlreadyExist
19
  ) {}
20
21
  public async execute(command: CreateMealTicketRemovalCommand): Promise<void> {
22
    const { date, comment, user } = command;
23
24
    if (false === this.dateUtils.isAWorkingDay(new Date(date))) {
25
      throw new NotAWorkingDateException();
26
    }
27
28
    if (
29
      true ===
30
      (await this.isMealTicketRemovalAlreadyExist.isSatisfiedBy(
31
        user,
32
        new Date(date)
33
      ))
34
    ) {
35
      throw new MealTicketRemovalAlreadyExistException();
36
    }
37
38
    await this.mealTicketRemovalRepository.save([
39
      new MealTicketRemoval(date, user, comment)
40
    ]);
41
  }
42
}
43