Passed
Pull Request — master (#211)
by
unknown
05:26
created

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

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 28
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 28
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 CreateContactCommandHandler.execute 0 11 2
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { Contact } from 'src/Domain/Contact/Contact.entity';
4
import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository';
5
import { CreateContactCommand } from './CreateContactCommand';
6
7
@CommandHandler(CreateContactCommand)
8
export class CreateContactCommandHandler {
9
  constructor(
10
    @Inject('IContactRepository')
11
    private readonly contactRepository: IContactRepository
12
  ) {}
13
14
  public async execute(command: CreateContactCommand): Promise<string> {
15
    const { firstName, lastName, company, phoneNumber, email, notes } = command;
16
17
    if (!firstName && !lastName && !company) {
18
      throw new EmptyContactException();
19
    }
20
21
    return (
22
      await this.contactRepository.save(
23
        new Contact(firstName, lastName, company, email, phoneNumber, notes)
24
      )
25
    ).getId();
26
  }
27
}
28