Passed
Pull Request — master (#70)
by Mathieu
02:27
created

server/src/Application/Billing/Command/CreateQuoteCommandHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 50
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreateQuoteCommandHandler.execute 0 23 4
1
import {CommandHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {CreateQuoteCommand} from './CreateQuoteCommand';
4
import {IQuoteRepository} from 'src/Domain/Billing/Repository/IQuoteRepository';
5
import {Quote} from 'src/Domain/Billing/Quote.entity';
6
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository';
7
import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository';
8
import {Customer} from 'src/Domain/Customer/Customer.entity';
9
import {CustomerNotFoundException} from 'src/Domain/Customer/Exception/CustomerNotFoundException';
10
import {ProjectNotFoundException} from 'src/Domain/Project/Exception/ProjectNotFoundException';
11
import {Project} from 'src/Domain/Project/Project.entity';
12
import {QuoteIdGenerator} from 'src/Domain/Billing/QuoteIdGenerator';
13
14
@CommandHandler(CreateQuoteCommand)
15
export class CreateQuoteCommandHandler {
16
  constructor(
17
    @Inject('IQuoteRepository')
18
    private readonly quoteRepository: IQuoteRepository,
19
    @Inject('ICustomerRepository')
20
    private readonly customerRepository: ICustomerRepository,
21
    @Inject('IProjectRepository')
22
    private readonly projectRepository: IProjectRepository,
23
    private readonly quoteIdGenerator: QuoteIdGenerator
24
  ) {}
25
26
  public async execute(command: CreateQuoteCommand): Promise<string> {
27
    const {customerId, status, user, projectId} = command;
28
29
    const customer = await this.customerRepository.findOneById(customerId);
30
    if (!(customer instanceof Customer)) {
31
      throw new CustomerNotFoundException();
32
    }
33
34
    let project = null;
35
    if (projectId) {
36
      project = await this.projectRepository.findOneById(projectId);
37
      if (!(project instanceof Project)) {
38
        throw new ProjectNotFoundException();
39
      }
40
    }
41
42
    const quoteId = await this.quoteIdGenerator.generate();
43
    const quote = await this.quoteRepository.save(
44
      new Quote(quoteId, status, user, customer, project)
45
    );
46
47
    return quote.getId();
48
  }
49
}
50