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 { IsMaximumTimeSpentReached } from 'src/Domain/FairCalendar/Specification/IsMaximumTimeSpentReached'; |
8
|
|
|
import { Event, EventType } from 'src/Domain/FairCalendar/Event.entity'; |
9
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
10
|
|
|
import { ProjectOrTaskMissingException } from 'src/Domain/FairCalendar/Exception/ProjectOrTaskMissingException'; |
11
|
|
|
import { AbstractProjectAndTaskGetter } from './AbstractProjectAndTaskGetter'; |
12
|
|
|
import { NoDateDuringThisPeriodException } from 'src/Domain/FairCalendar/Exception/NoDateDuringThisPeriodException'; |
13
|
|
|
import { AddEventsView } from '../View/AddEventsView'; |
14
|
|
|
|
15
|
|
|
@CommandHandler(AddEventCommand) |
16
|
|
|
export class AddEventCommandHandler extends AbstractProjectAndTaskGetter { |
17
|
|
|
constructor( |
18
|
|
|
@Inject('ITaskRepository') taskRepository: ITaskRepository, |
19
|
|
|
@Inject('IProjectRepository') projectRepository: IProjectRepository, |
20
|
|
|
@Inject('IEventRepository') |
21
|
|
|
private readonly eventRepository: IEventRepository, |
22
|
|
|
@Inject('IDateUtils') |
23
|
|
|
private readonly dateUtils: IDateUtils, |
24
|
|
|
private readonly isMaximumTimeSpentReached: IsMaximumTimeSpentReached |
25
|
|
|
) { |
26
|
|
|
super(taskRepository, projectRepository); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public async execute(command: AddEventCommand): Promise<AddEventsView> { |
30
|
|
|
const {type, startDate, endDate, projectId, taskId, summary, time, user} = command; |
31
|
|
|
const errors: string[] = []; |
32
|
|
|
|
33
|
|
|
if (type === EventType.MISSION && (!projectId || !taskId)) { |
34
|
|
|
throw new ProjectOrTaskMissingException(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
const days = this.dateUtils.getWorkedDaysDuringAPeriod(startDate, endDate); |
38
|
|
|
|
39
|
|
|
if (0 === days.length) { |
40
|
|
|
throw new NoDateDuringThisPeriodException(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
const [project, task] = await Promise.all([ |
44
|
|
|
this.getProject(projectId), |
45
|
|
|
this.getTask(taskId) |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
for (const day of days) { |
49
|
|
|
const date = this.dateUtils.format(day, 'y-MM-dd'); |
50
|
|
|
const event = new Event( |
51
|
|
|
type, |
52
|
|
|
user, |
53
|
|
|
time, |
54
|
|
|
date, |
55
|
|
|
project, |
56
|
|
|
task, |
57
|
|
|
summary |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
if (true === (await this.isMaximumTimeSpentReached.isSatisfiedBy(event))) { |
61
|
|
|
errors.push(date); |
62
|
|
|
|
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
this.eventRepository.save(event); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new AddEventsView(errors); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|