|
1
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
|
2
|
|
|
import {Inject} from '@nestjs/common'; |
|
3
|
|
|
import {UpdateEventCommand} from './UpdateEventCommand'; |
|
4
|
|
|
import {ITaskRepository} from 'src/Domain/Task/Repository/ITaskRepository'; |
|
5
|
|
|
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository'; |
|
6
|
|
|
import {IEventRepository} from 'src/Domain/FairCalendar/Repository/IEventRepository'; |
|
7
|
|
|
import {EventType} from 'src/Domain/FairCalendar/Event.entity'; |
|
8
|
|
|
import {MaximumEventReachedException} from 'src/Domain/FairCalendar/Exception/MaximumEventReachedException'; |
|
9
|
|
|
import {ProjectOrTaskMissingException} from 'src/Domain/FairCalendar/Exception/ProjectOrTaskMissingException'; |
|
10
|
|
|
import {EventNotFoundException} from 'src/Domain/FairCalendar/Exception/EventNotFoundException'; |
|
11
|
|
|
import {EventDoesntBelongToUserException} from 'src/Domain/FairCalendar/Exception/EventDoesntBelongToUserException'; |
|
12
|
|
|
import {DoesEventBelongToUser} from 'src/Domain/FairCalendar/Specification/DoesEventBelongToUser'; |
|
13
|
|
|
import {AbstractProjectAndTaskGetter} from './AbstractProjectAndTaskGetter'; |
|
14
|
|
|
import {IsMaximumTimeSpentReachedOnEdition} from 'src/Domain/FairCalendar/Specification/IsMaximumTimeSpentReachedOnEdition'; |
|
15
|
|
|
|
|
16
|
|
|
@CommandHandler(UpdateEventCommand) |
|
17
|
|
|
export class UpdateEventCommandHandler extends AbstractProjectAndTaskGetter { |
|
18
|
|
|
constructor( |
|
19
|
|
|
@Inject('ITaskRepository') taskRepository: ITaskRepository, |
|
20
|
|
|
@Inject('IProjectRepository') projectRepository: IProjectRepository, |
|
21
|
|
|
@Inject('IEventRepository') |
|
22
|
|
|
private readonly eventRepository: IEventRepository, |
|
23
|
|
|
private readonly doesEventBelongToUser: DoesEventBelongToUser, |
|
24
|
|
|
private readonly isMaximumTimeSpentReachedOnEdition: IsMaximumTimeSpentReachedOnEdition |
|
25
|
|
|
) { |
|
26
|
|
|
super(taskRepository, projectRepository); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public async execute(command: UpdateEventCommand): Promise<string> { |
|
30
|
|
|
const {id, type, projectId, taskId, summary, time, user} = command; |
|
31
|
|
|
|
|
32
|
|
|
const event = await this.eventRepository.findOneById(id); |
|
33
|
|
|
if (!event) { |
|
34
|
|
|
throw new EventNotFoundException(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (false === this.doesEventBelongToUser.isSatisfiedBy(event, user)) { |
|
38
|
|
|
throw new EventDoesntBelongToUserException(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (type === EventType.MISSION && (!projectId || !taskId)) { |
|
42
|
|
|
throw new ProjectOrTaskMissingException(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
const project = await this.getProject(projectId); |
|
46
|
|
|
const task = await this.getTask(taskId); |
|
47
|
|
|
|
|
48
|
|
|
if ( |
|
49
|
|
|
true === |
|
50
|
|
|
(await this.isMaximumTimeSpentReachedOnEdition.isSatisfiedBy(event, time)) |
|
51
|
|
|
) { |
|
52
|
|
|
throw new MaximumEventReachedException(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
event.update(type, time, project, task, summary); |
|
56
|
|
|
await this.eventRepository.save(event); |
|
57
|
|
|
|
|
58
|
|
|
return event.getId(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|