Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 38 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |