Passed
Push — master ( 74fcec...dc846b )
by
unknown
02:16
created

server/src/Application/Contact/Command/UpdateContactCommandHandler.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 43
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A UpdateContactCommandHandler.execute 0 25 3
1
import { CommandHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { UpdateContactCommand } from './UpdateContactCommand';
4
import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository';
5
import { ContactNotFoundException } from 'src/Domain/Contact/Exception/ContactNotFoundException';
6
import { IsContactEmpty } from 'src/Domain/Contact/Specification/IsContactEmpty';
7
import { EmptyContactException } from 'src/Domain/Contact/Exception/EmptyContactException';
8
9
@CommandHandler(UpdateContactCommand)
10
export class UpdateContactCommandHandler {
11
  constructor(
12
    @Inject('IContactRepository')
13
    private readonly contactRepository: IContactRepository,
14
    private readonly isContactEmpty: IsContactEmpty
15
  ) {}
16
17
  public async execute(command: UpdateContactCommand): Promise<void> {
18
    const {
19
      id,
20
      firstName,
21
      lastName,
22
      company,
23
      email,
24
      phoneNumber,
25
      notes
26
    } = command;
27
28
    const contact = await this.contactRepository.findOneById(id);
29
30
    if (!contact) {
31
      throw new ContactNotFoundException();
32
    }
33
34
    if (this.isContactEmpty.isSatisfiedBy(firstName, lastName, company)) {
35
      throw new EmptyContactException();
36
    }
37
38
    contact.update(firstName, lastName, company, email, phoneNumber, notes);
39
40
    await this.contactRepository.save(contact);
41
  }
42
}
43