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

server/src/Application/Billing/Query/DailyRate/GetDailyRateByIdQueryHandler.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 42
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetDailyRateByIdQueryHandler.execute 0 22 2
1
import {QueryHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {GetDailyRateByIdQuery} from './GetDailyRateByIdQuery';
4
import {TaskView} from 'src/Application/Task/View/TaskView';
5
import {IDailyRateRepository} from 'src/Domain/Billing/Repository/IDailyRateRepository';
6
import {DailyRateView} from '../../View/DailyRate/DailyRateView';
7
import {DailyRateNotFoundException} from 'src/Domain/Billing/Exception/DailyRateNotFoundException';
8
import {CustomerView} from 'src/Application/Customer/View/CustomerView';
9
import {UserView} from 'src/Application/User/View/UserView';
10
11
@QueryHandler(GetDailyRateByIdQuery)
12
export class GetDailyRateByIdQueryHandler {
13
  constructor(
14
    @Inject('IDailyRateRepository')
15
    private readonly dailyRateRepository: IDailyRateRepository
16
  ) {}
17
18
  public async execute(query: GetDailyRateByIdQuery): Promise<DailyRateView> {
19
    const dailyRate = await this.dailyRateRepository.findOneById(query.id);
20
    if (!dailyRate) {
21
      throw new DailyRateNotFoundException();
22
    }
23
24
    const user = dailyRate.getUser();
25
    const customer = dailyRate.getCustomer();
26
    const task = dailyRate.getTask();
27
28
    return new DailyRateView(
29
      dailyRate.getId(),
30
      dailyRate.getAmount() / 100,
31
      new UserView(
32
        user.getId(),
33
        user.getFirstName(),
34
        user.getLastName(),
35
        user.getEmail()
36
      ),
37
      new TaskView(task.getId(), task.getName()),
38
      new CustomerView(customer.getId(), customer.getName())
39
    );
40
  }
41
}
42