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

server/src/Application/Customer/Command/CreateCustomerCommandHandler.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 39
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 39
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 CreateCustomerCommandHandler.execute 0 17 2
1
import {Inject} from '@nestjs/common';
2
import {CommandHandler} from '@nestjs/cqrs';
3
import {Customer} from 'src/Domain/Customer/Customer.entity';
4
import {CreateCustomerCommand} from './CreateCustomerCommand';
5
import {ICustomerRepository} from 'src/Domain/Customer/Repository/ICustomerRepository';
6
import {CustomerAlreadyExistException} from 'src/Domain/Customer/Exception/CustomerAlreadyExistException';
7
import {IsCustomerAlreadyExist} from 'src/Domain/Customer/Specification/IsCustomerAlreadyExist';
8
import {IAddressRepository} from 'src/Domain/Customer/Repository/IAddressRepository';
9
import {Address} from 'src/Domain/Customer/Address.entity';
10
11
@CommandHandler(CreateCustomerCommand)
12
export class CreateCustomerCommandHandler {
13
  constructor(
14
    @Inject('ICustomerRepository')
15
    private readonly customerRepository: ICustomerRepository,
16
    @Inject('IAddressRepository')
17
    private readonly addressRepository: IAddressRepository,
18
    private readonly isCustomerAlreadyExist: IsCustomerAlreadyExist
19
  ) {}
20
21
  public async execute(command: CreateCustomerCommand): Promise<string> {
22
    const {name, street, city, country, zipCode} = command;
23
24
    if (true === (await this.isCustomerAlreadyExist.isSatisfiedBy(name))) {
25
      throw new CustomerAlreadyExistException();
26
    }
27
28
    const address = await this.addressRepository.save(
29
      new Address(street, city, zipCode, country)
30
    );
31
32
    const customer = await this.customerRepository.save(
33
      new Customer(name, address)
34
    );
35
36
    return customer.getId();
37
  }
38
}
39