Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 29 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { Inject } from '@nestjs/common'; |
||
2 | import { CommandHandler } from '@nestjs/cqrs'; |
||
3 | import { Contact } from 'src/Domain/Contact/Contact.entity'; |
||
4 | import { EmptyContactException } from 'src/Domain/Contact/Exception/EmptyContactException'; |
||
5 | import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository'; |
||
6 | import { CreateContactCommand } from './CreateContactCommand'; |
||
7 | |||
8 | @CommandHandler(CreateContactCommand) |
||
9 | export class CreateContactCommandHandler { |
||
10 | constructor( |
||
11 | @Inject('IContactRepository') |
||
12 | private readonly contactRepository: IContactRepository |
||
13 | ) {} |
||
14 | |||
15 | public async execute(command: CreateContactCommand): Promise<string> { |
||
16 | const { firstName, lastName, company, email, phoneNumber, notes } = command; |
||
17 | |||
18 | if (!firstName && !lastName && !company) { |
||
19 | throw new EmptyContactException(); |
||
20 | } |
||
21 | |||
22 | const contact = await this.contactRepository.save( |
||
23 | new Contact(firstName, lastName, company, email, phoneNumber, notes) |
||
24 | ); |
||
25 | |||
26 | return contact.getId(); |
||
27 | } |
||
28 | } |
||
29 |