Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 43 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |