Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 36 |
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 | |||
7 | @CommandHandler(UpdateContactCommand) |
||
8 | export class UpdateContactCommandHandler { |
||
9 | constructor( |
||
10 | @Inject('IContactRepository') |
||
11 | private readonly contactRepository: IContactRepository |
||
12 | ) {} |
||
13 | |||
14 | public async execute(command: UpdateContactCommand): Promise<void> { |
||
15 | const { |
||
16 | id, |
||
17 | firstName, |
||
18 | lastName, |
||
19 | company, |
||
20 | email, |
||
21 | phoneNumber, |
||
22 | notes |
||
23 | } = command; |
||
24 | |||
25 | const contact = await this.contactRepository.findOneById(id); |
||
26 | |||
27 | if (!contact) { |
||
28 | throw new ContactNotFoundException(); |
||
29 | } |
||
30 | |||
31 | contact.update(firstName, lastName, company, email, phoneNumber, notes); |
||
32 | |||
33 | await this.contactRepository.save(contact); |
||
34 | } |
||
35 | } |
||
36 |