-
Notifications
You must be signed in to change notification settings - Fork 70
/
event.service.ts
134 lines (123 loc) · 3.6 KB
/
event.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2023 Sourcefuse Technologies
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import {bind, BindingScope} from '@loopback/core';
import {repository, Filter} from '@loopback/repository';
import {ResponseStatusType} from '../models/enums/response-status.enum';
import {IStartEndTime, EventAttendeeView} from '../models';
import {EventAttendeeViewRepository} from '../repositories/event-attendee-view.repository';
import {EventAttendeeViewItemDTO} from '../models/event-attendee-view-item.dto';
import {HttpErrors} from '@loopback/rest';
import {ErrorKeys, StatusType} from '../models/enums';
@bind({scope: BindingScope.TRANSIENT})
export class EventService {
constructor(
@repository(EventAttendeeViewRepository)
public eventAttendeeViewRepository: EventAttendeeViewRepository,
) {}
async getBusyDetails(
item: EventAttendeeViewItemDTO,
timeMax: Date,
timeMin: Date,
) {
const where = [];
where.push({
status: {neq: StatusType.Cancelled},
});
where.push({
or: [
{responseStatus: {neq: ResponseStatusType.Declined}},
{responseStatus: null as unknown as ResponseStatusType},
],
});
where.push({
and: [{startDateTime: {lt: timeMax}}, {endDateTime: {gt: timeMin}}],
});
where.push({
or: [
{
and: [
{identifier: item.id},
{attendeeIdentifier: null as unknown as string},
],
},
{attendeeIdentifier: item.id},
],
});
let key: keyof EventAttendeeViewItemDTO;
for (key in item) {
if (key !== 'id') {
where.push({[key]: item[key]});
}
}
const eventAttendeeFilter = {
where: {
and: where,
},
};
let eventAttendeeResponse;
try {
eventAttendeeResponse = await this.eventAttendeeViewRepository.find(
eventAttendeeFilter as Filter<EventAttendeeView>,
);
} catch (e) {
throw new HttpErrors.UnprocessableEntity(ErrorKeys.ItemInvalid);
}
const timesObj: IStartEndTime[] = [];
const timesObj2 = Object.assign(timesObj, eventAttendeeResponse);
const eventAttendeeList = await this.limitTimeToBoundaryValues(
timesObj2,
timeMin,
timeMax,
);
let busyDetails: IStartEndTime[] = [];
busyDetails = this.addToBusyArray(busyDetails, eventAttendeeList);
return {
busy: busyDetails,
};
}
validateDateForTimeZone(date: Date): boolean {
if (new Date(date).getTime() > 0) {
const dateInString = date.toString().toLowerCase();
if (dateInString.includes('+') || dateInString.includes('z')) {
return true;
}
}
return false;
}
addToBusyArray(
busy: IStartEndTime[],
entityList: IStartEndTime[],
): IStartEndTime[] {
for (const entity of entityList) {
const {startDateTime, endDateTime} = entity;
if (startDateTime && endDateTime) {
const startEndTime: IStartEndTime = {
startDateTime,
endDateTime,
};
busy.push(startEndTime);
}
}
return busy;
}
async limitTimeToBoundaryValues(
timesObj: IStartEndTime[],
startTime: Date,
endTime: Date,
): Promise<IStartEndTime[]> {
timesObj.forEach(function (times) {
const {startDateTime, endDateTime} = times;
if (startDateTime && endDateTime) {
if (startDateTime < new Date(startTime)) {
times.startDateTime = new Date(startTime);
}
if (endDateTime > new Date(endTime)) {
times.endDateTime = new Date(endTime);
}
}
});
return timesObj;
}
}