import { CommandHandler } from '@nestjs/cqrs' ; import { Inject } from '@nestjs/common' ; import { CreateQuoteItemsCommand } from './CreateQuoteItemsCommand' ; import { IQuoteItemRepository } from 'src/Domain/Accounting/Repository/IQuoteItemRepository' ; import { QuoteItem } from 'src/Domain/Accounting/QuoteItem.entity' ; import { IQuoteRepository } from 'src/Domain/Accounting/Repository/IQuoteRepository' ; import { Quote } from 'src/Domain/Accounting/Quote.entity' ; import { QuoteNotFoundException } from 'src/Domain/Accounting/Exception/QuoteNotFoundException' ; @ CommandHandler ( CreateQuoteItemsCommand ) export class CreateQuoteItemsCommandHandler { constructor ( @ Inject ( 'IQuoteItemRepository' ) private readonly quoteItemRepository , @ Inject ( 'IQuoteRepository' ) private readonly quoteRepository ) { } public async execute ( command ) { const quote = await this . quoteRepository . find ( command . quoteId ) ; if ( ! ( quote instanceof Quote ) ) { throw new QuoteNotFoundException ( ) ; } for ( const { title , quantity , dailyRate } of command . items ) { this . quoteItemRepository . save ( new QuoteItem ( title , Math . round ( quantity * 100 ) , Math . round ( dailyRate * 100 ) , quote ) ) ; } } }