|
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: Date[] = []; |
|
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
|
|
|
]); const addedEvents: string[] = []; |
|
47
|
|
|
|
|
48
|
|
|
for (const day of days) { |
|
49
|
|
|
const event = new Event( |
|
50
|
|
|
type, |
|
51
|
|
|
user, |
|
52
|
|
|
time, |
|
53
|
|
|
this.dateUtils.format(day, 'y-MM-dd'), |
|
54
|
|
|
project, |
|
55
|
|
|
task, |
|
56
|
|
|
summary |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
if (true === (await this.isMaximumTimeSpentReached.isSatisfiedBy(event))) { |
|
60
|
|
|
errors.push(day); |
|
61
|
|
|
|
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
this.eventRepository.save(event); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new AddEventsView(errors); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|