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

server/src/Application/Billing/Command/Quote/CreateQuoteItemsCommandHandler.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 38
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreateQuoteItemsCommandHandler.execute 0 15 3
1
import {CommandHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {CreateQuoteItemsCommand} from './CreateQuoteItemsCommand';
4
import {IQuoteItemRepository} from 'src/Domain/Billing/Repository/IQuoteItemRepository';
5
import {QuoteItem} from 'src/Domain/Billing/QuoteItem.entity';
6
import {IQuoteRepository} from 'src/Domain/Billing/Repository/IQuoteRepository';
7
import {Quote} from 'src/Domain/Billing/Quote.entity';
8
import {QuoteNotFoundException} from 'src/Domain/Billing/Exception/QuoteNotFoundException';
9
10
@CommandHandler(CreateQuoteItemsCommand)
11
export class CreateQuoteItemsCommandHandler {
12
  constructor(
13
    @Inject('IQuoteItemRepository')
14
    private readonly quoteItemRepository: IQuoteItemRepository,
15
    @Inject('IQuoteRepository')
16
    private readonly quoteRepository: IQuoteRepository
17
  ) {}
18
19
  public async execute(command: CreateQuoteItemsCommand): Promise<void> {
20
    const quote = await this.quoteRepository.find(command.quoteId);
21
    if (!(quote instanceof Quote)) {
22
      throw new QuoteNotFoundException();
23
    }
24
25
    for (const {title, quantity, dailyRate, vat} of command.items) {
26
      this.quoteItemRepository.save(
27
        new QuoteItem(
28
          title,
29
          quantity,
30
          Math.round(dailyRate * 100),
31
          Math.round(vat * 100),
32
          quote
33
        )
34
      );
35
    }
36
  }
37
}
38