Passed
Pull Request — master (#78)
by Mathieu
01:33
created

server/src/Application/Billing/Command/DailyRate/CreateDailyRateCommandHandler.ts   A

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 61
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 52
mnd 4
bc 4
fnc 1
dl 0
loc 61
rs 10
bpm 4
cpm 5
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B CreateDailyRateCommandHandler.execute 0 31 5
1
import {CommandHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {CreateDailyRateCommand} from '../DailyRate/CreateDailyRateCommand';
4
import {IDailyRateRepository} from 'src/Domain/Billing/Repository/IDailyRateRepository';
5
import {DailyRate} from 'src/Domain/Billing/DailyRate.entity';
6
import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository';
7
import {CustomerNotFoundException} from 'src/Domain/Customer/Exception/CustomerNotFoundException';
8
import {ITaskRepository} from 'src/Domain/Task/Repository/ITaskRepository';
9
import {IUserRepository} from 'src/Domain/User/Repository/IUserRepository';
10
import {UserNotFoundException} from 'src/Domain/User/Exception/UserNotFoundException';
11
import {TaskNotFoundException} from 'src/Domain/Task/Exception/TaskNotFoundException';
12
import {IsDailyRateAlreadyExist} from 'src/Domain/Billing/Specification/IsDailyRateAlreadyExist';
13
import {DailyRateAlreadyExistException} from 'src/Domain/Billing/Exception/DailyRateAlreadyExistException';
14
15
@CommandHandler(CreateDailyRateCommand)
16
export class CreateDailyRateCommandHandler {
17
  constructor(
18
    @Inject('IDailyRateRepository')
19
    private readonly dailyRateRepository: IDailyRateRepository,
20
    @Inject('ICustomerRepository')
21
    private readonly customerRepository: ICustomerRepository,
22
    @Inject('ITaskRepository')
23
    private readonly taskRepository: ITaskRepository,
24
    @Inject('IUserRepository')
25
    private readonly userRepository: IUserRepository,
26
    private readonly isDailyRateAlreadyExist: IsDailyRateAlreadyExist
27
  ) {}
28
29
  public async execute(command: CreateDailyRateCommand): Promise<string> {
30
    const {customerId, userId, taskId, amount} = command;
31
32
    const user = await this.userRepository.findOneById(userId);
33
    if (!user) {
34
      throw new UserNotFoundException();
35
    }
36
37
    const customer = await this.customerRepository.findOneById(customerId);
38
    if (!customer) {
39
      throw new CustomerNotFoundException();
40
    }
41
42
    const task = await this.taskRepository.findOneById(taskId);
43
    if (!task) {
44
      throw new TaskNotFoundException();
45
    }
46
47
    if (
48
      true ===
49
      (await this.isDailyRateAlreadyExist.isSatisfiedBy(user, task, customer))
50
    ) {
51
      throw new DailyRateAlreadyExistException();
52
    }
53
54
    const dailyRate = await this.dailyRateRepository.save(
55
      new DailyRate(Math.round(amount * 100), user, customer, task)
56
    );
57
58
    return dailyRate.getId();
59
  }
60
}
61