Completed
Push — master ( e65836...fa4d5c )
by Mathieu
12s queued 10s
created

server/src/Application/Project/Command/CreateProjectCommandHandler.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 40
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreateProjectCommandHandler.execute 0 18 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { CreateProjectCommand } from './CreateProjectCommand';
4
import { IProjectRepository } from 'src/Domain/Project/Repository/IProjectRepository';
5
import { Project } from 'src/Domain/Project/Project.entity';
6
import { CustomerNotFoundException } from 'src/Domain/Customer/Exception/CustomerNotFoundException';
7
import { IsProjectAlreadyExist } from 'src/Domain/Project/Specification/IsProjectAlreadyExist';
8
import { ProjectAlreadyExistException } from 'src/Domain/Project/Exception/ProjectAlreadyExistException';
9
import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository';
10
11
@CommandHandler(CreateProjectCommand)
12
export class CreateProjectCommandHandler {
13
  constructor(
14
    @Inject('IProjectRepository')
15
    private readonly projectRepository: IProjectRepository,
16
    @Inject('ICustomerRepository')
17
    private readonly customerRepository: ICustomerRepository,
18
    private readonly isProjectAlreadyExist: IsProjectAlreadyExist
19
  ) {}
20
21
  public async execute(command: CreateProjectCommand): Promise<string> {
22
    const { name, dayDuration, customerId, invoiceUnit } = command;
23
24
    const customer = await this.customerRepository.findOneById(customerId);
25
    if (!customer) {
26
      throw new CustomerNotFoundException();
27
    }
28
29
    if (true === (await this.isProjectAlreadyExist.isSatisfiedBy(name))) {
30
      throw new ProjectAlreadyExistException();
31
    }
32
33
    const project = await this.projectRepository.save(
34
      new Project(name, dayDuration, invoiceUnit, customer)
35
    );
36
37
    return project.getId();
38
  }
39
}
40