Passed
Pull Request — master (#188)
by Mathieu
02:03
created

server/src/Application/Customer/Query/GetCustomersQueryHandler.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 45
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 45
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 GetCustomersQueryHandler.execute 0 28 2
1
import {Inject} from '@nestjs/common';
2
import {QueryHandler} from '@nestjs/cqrs';
3
import {GetCustomersQuery} from './GetCustomersQuery';
4
import {CustomerView} from '../View/CustomerView';
5
import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository';
6
import {AddressView} from '../View/AddressView';
7
import {Pagination} from 'src/Application/Common/Pagination';
8
9
@QueryHandler(GetCustomersQuery)
10
export class GetCustomersQueryHandler {
11
  constructor(
12
    @Inject('ICustomerRepository')
13
    private readonly customerRepository: ICustomerRepository
14
  ) {}
15
16
  public async execute(
17
    query: GetCustomersQuery
18
  ): Promise<Pagination<CustomerView>> {
19
    const customerViews: CustomerView[] = [];
20
    const [customers, total] = await this.customerRepository.findCustomers(
21
      query.page
22
    );
23
24
    for (const customer of customers) {
25
      const address = customer.getAddress();
26
27
      customerViews.push(
28
        new CustomerView(
29
          customer.getId(),
30
          customer.getName(),
31
          new AddressView(
32
            address.getId(),
33
            address.getStreet(),
34
            address.getCity(),
35
            address.getZipCode(),
36
            address.getCountry()
37
          )
38
        )
39
      );
40
    }
41
42
    return new Pagination<CustomerView>(customerViews, total);
43
  }
44
}
45