1
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
2
|
|
|
import {Inject} from '@nestjs/common'; |
3
|
|
|
import {AddEventCommand} from './AddEventCommand'; |
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 {ProjectNotFoundException} from 'src/Domain/Project/Exception/ProjectNotFoundException'; |
8
|
|
|
import {TaskNotFoundException} from 'src/Domain/Task/Exception/TaskNotFoundException'; |
9
|
|
|
import {IsMaximumTimeSpentReached} from 'src/Domain/FairCalendar/Specification/IsMaximumTimeSpentReached'; |
10
|
|
|
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity'; |
11
|
|
|
import {MaximumEventReachedException} from 'src/Domain/FairCalendar/Exception/MaximumEventReachedException'; |
12
|
|
|
import {IDateUtils} from 'src/Application/IDateUtils'; |
13
|
|
|
import {Task} from 'src/Domain/Task/Task.entity'; |
14
|
|
|
import {Project} from 'src/Domain/Project/Project.entity'; |
15
|
|
|
import {ProjectOrTaskMissingException} from 'src/Domain/FairCalendar/Exception/ProjectOrTaskMissingException'; |
16
|
|
|
|
17
|
|
|
@CommandHandler(AddEventCommand) |
18
|
|
|
export class AddEventCommandHandler { |
19
|
|
|
constructor( |
20
|
|
|
@Inject('ITaskRepository') |
21
|
|
|
private readonly taskRepository: ITaskRepository, |
22
|
|
|
@Inject('IProjectRepository') |
23
|
|
|
private readonly projectRepository: IProjectRepository, |
24
|
|
|
@Inject('IEventRepository') |
25
|
|
|
private readonly eventRepository: IEventRepository, |
26
|
|
|
@Inject('IDateUtils') |
27
|
|
|
private readonly dateUtils: IDateUtils, |
28
|
|
|
private readonly isMaximumTimeSpentReached: IsMaximumTimeSpentReached |
29
|
|
|
) {} |
30
|
|
|
|
31
|
|
|
public async execute(command: AddEventCommand): Promise<string> { |
32
|
|
|
const {type, date, projectId, taskId, summary, time, user} = command; |
33
|
|
|
|
34
|
|
|
if (type === EventType.MISSION && (!projectId || !taskId)) { |
35
|
|
|
throw new ProjectOrTaskMissingException(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
const project = await this.getProject(projectId); |
39
|
|
|
const task = await this.getTask(taskId); |
40
|
|
|
const event = new Event( |
41
|
|
|
type, |
42
|
|
|
user, |
43
|
|
|
time, |
44
|
|
|
this.dateUtils.format(date, 'y-MM-dd'), |
45
|
|
|
project, |
46
|
|
|
task, |
47
|
|
|
summary |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
if (true === (await this.isMaximumTimeSpentReached.isSatisfiedBy(event))) { |
51
|
|
|
throw new MaximumEventReachedException(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
const savedEvent = await this.eventRepository.save(event); |
55
|
|
|
|
56
|
|
|
return savedEvent.getId(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private async getTask(taskId?: string): Promise<Task | null> { |
60
|
|
|
if (!taskId) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
const task = await this.taskRepository.findOneById(taskId); |
65
|
|
|
if (!task) { |
66
|
|
|
throw new TaskNotFoundException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return task; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private async getProject(projectId?: string): Promise<Project | null> { |
73
|
|
|
if (!projectId) { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
const project = await this.projectRepository.findOneById(projectId); |
78
|
|
|
if (!project) { |
79
|
|
|
throw new ProjectNotFoundException(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return project; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|