Passed
Push — master ( 943b8e...9278a4 )
by Mathieu
01:38
created

server/src/Application/Accounting/Query/Quote/GetQuotesQueryHandler.ts   A

Complexity

Total Complexity 4
Complexity/F 4

Size

Lines of Code 46
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetQuotesQueryHandler.execute 0 29 4
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