Passed
Pull Request — master (#75)
by Mathieu
02:33 queued 01:09
created

DeleteEventCommandHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 19
dl 0
loc 21
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 14 3
1
import {CommandHandler} from '@nestjs/cqrs';
2
import {Inject} from '@nestjs/common';
3
import {IEventRepository} from 'src/Domain/FairCalendar/Repository/IEventRepository';
4
import {DeleteEventCommand} from './DeleteEventCommand';
5
import {NotEventOwnerException} from 'src/Domain/FairCalendar/Exception/NotEventOwnerException';
6
import {EventNotFoundException} from 'src/Domain/FairCalendar/Exception/EventNotFoundException';
7
import {Event} from 'src/Domain/FairCalendar/Event.entity';
8
9
@CommandHandler(DeleteEventCommand)
10
export class DeleteEventCommandHandler {
11
  constructor(
12
    @Inject('IEventRepository')
13
    private readonly eventRepository: IEventRepository
14
  ) {}
15
16
  public async execute(command: DeleteEventCommand): Promise<void> {
17
    const {id, user} = command;
18
    const event = await this.eventRepository.findOneById(id);
19
20
    if (!(event instanceof Event)) {
21
      throw new EventNotFoundException();
22
    }
23
24
    if (event.getUser().getId() !== user.getId()) {
25
      throw new NotEventOwnerException();
26
    }
27
28
    this.eventRepository.delete(event);
29
  }
30
}
31