Passed
Pull Request — master (#165)
by Mathieu
01:31
created

server/src/Application/Accounting/Query/Invoice/GetInvoicesQueryHandler.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 49
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetInvoicesQueryHandler.execute 0 31 2
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { GetInvoicesQuery } from './GetInvoicesQuery';
4
import { Pagination } from 'src/Application/Common/Pagination';
5
import { IInvoiceRepository } from 'src/Domain/Accounting/Repository/IInvoiceRepository';
6
import { InvoiceView } from '../../View/DailyRate/InvoiceView';
7
import { ProjectView } from 'src/Application/Project/View/ProjectView';
8
import { CustomerView } from 'src/Application/Customer/View/CustomerView';
9
10
@QueryHandler(GetInvoicesQuery)
11
export class GetInvoicesQueryHandler {
12
  constructor(
13
    @Inject('IInvoiceRepository')
14
    private readonly invoiceRepository: IInvoiceRepository
15
  ) {}
16
17
  public async execute(query: GetInvoicesQuery): Promise<Pagination<InvoiceView>> {
18
    const results: InvoiceView[] = [];
19
    const [ invoices, total ] = await this.invoiceRepository.findInvoices(
20
      query.page
21
    );
22
23
    for (const invoice of invoices) {
24
      const project = invoice.getProject();
25
      const customer = project.getCustomer();
26
27
      results.push(
28
        new InvoiceView(
29
          invoice.getId(),
30
          invoice.getInvoiceId(),
31
          invoice.getStatus(),
32
          invoice.getCreatedAt(),
33
          invoice.getExpiryDate(),
34
          100,
35
          new ProjectView(
36
            project.getId(),
37
            project.getName(),
38
            null,
39
            null,
40
            new CustomerView(customer.getId(), customer.getName())
41
          )
42
        )
43
      );
44
    }
45
46
    return new Pagination<InvoiceView>(results, total);
47
  }
48
}
49