| Total Complexity | 4 |
| Complexity/F | 4 |
| Lines of Code | 46 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import {QueryHandler} from '@nestjs/cqrs'; |
||
| 2 | import {Inject} from '@nestjs/common'; |
||
| 3 | import {GetQuotesQuery} from './GetQuotesQuery'; |
||
| 4 | import {IQuoteRepository} from 'src/Domain/Accounting/Repository/IQuoteRepository'; |
||
| 5 | import {CustomerView} from 'src/Application/Customer/View/CustomerView'; |
||
| 6 | import {QuoteView} from '../../View/DailyRate/QuoteView'; |
||
| 7 | import {ProjectView} from 'src/Application/Project/View/ProjectView'; |
||
| 8 | |||
| 9 | @QueryHandler(GetQuotesQuery) |
||
| 10 | export class GetQuotesQueryHandler { |
||
| 11 | constructor( |
||
| 12 | @Inject('IQuoteRepository') |
||
| 13 | private readonly quoteRepository: IQuoteRepository |
||
| 14 | ) {} |
||
| 15 | |||
| 16 | public async execute(query: GetQuotesQuery): Promise<QuoteView[]> { |
||
| 17 | const quotes = await this.quoteRepository.findAll(); |
||
| 18 | const results: QuoteView[] = []; |
||
| 19 | |||
| 20 | for (const quote of quotes) { |
||
| 21 | const project = quote.getProject(); |
||
| 22 | const customer = quote.getCustomer(); |
||
| 23 | let amountExcludingVat = 0; |
||
| 24 | |||
| 25 | for (const item of quote.getItems()) { |
||
| 26 | amountExcludingVat += |
||
| 27 | (item.getQuantity() / 100) * (item.getDailyRate() / 100); |
||
| 28 | } |
||
| 29 | |||
| 30 | results.push( |
||
| 31 | new QuoteView( |
||
| 32 | quote.getId(), |
||
| 33 | quote.getQuoteId(), |
||
| 34 | quote.getStatus(), |
||
| 35 | quote.getCreatedAt(), |
||
| 36 | amountExcludingVat * 1.2, |
||
| 37 | new CustomerView(customer.getId(), customer.getName()), |
||
| 38 | project ? new ProjectView(project.getId(), project.getName()) : null |
||
| 39 | ) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | return results; |
||
| 44 | } |
||
| 45 | } |
||
| 46 |