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

server/src/Application/Project/Query/GetProjectsQueryHandler.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 44
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 44
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 GetProjectsQueryHandler.execute 0 27 2
1
import {QueryHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {GetProjectsQuery} from './GetProjectsQuery';
4
import {ProjectView} from '../View/ProjectView';
5
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository';
6
import {CustomerView} from 'src/Application/Customer/View/CustomerView';
7
import {Pagination} from 'src/Application/Common/Pagination';
8
9
@QueryHandler(GetProjectsQuery)
10
export class GetProjectsQueryHandler {
11
  constructor(
12
    @Inject('IProjectRepository')
13
    private readonly projectRepository: IProjectRepository
14
  ) {}
15
16
  public async execute(
17
    query: GetProjectsQuery
18
  ): Promise<Pagination<ProjectView>> {
19
    const {customerId, page} = query;
20
21
    const projectViews: ProjectView[] = [];
22
    const [projects, total] = await this.projectRepository.findProjects(
23
      page,
24
      customerId
25
    );
26
27
    for (const project of projects) {
28
      const customer = project.getCustomer();
29
30
      projectViews.push(
31
        new ProjectView(
32
          project.getId(),
33
          project.getName(),
34
          project.getDayDuration(),
35
          project.getInvoiceUnit(),
36
          new CustomerView(customer.getId(), customer.getName())
37
        )
38
      );
39
    }
40
41
    return new Pagination<ProjectView>(projectViews, total);
42
  }
43
}
44