-
Notifications
You must be signed in to change notification settings - Fork 70
/
validator.service.ts
93 lines (86 loc) · 2.4 KB
/
validator.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
// Copyright (c) 2023 Sourcefuse Technologies
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import {bind, BindingScope, inject} from '@loopback/core';
import {repository} from '@loopback/repository';
import {IAuthUserWithPermissions} from '@sourceloop/core';
import {AuthenticationBindings} from 'loopback4-authentication';
import {
CalendarRepository,
EventRepository,
SubscriptionRepository,
} from '../repositories';
import {ISchedulerConfig} from '../types';
import {SchedulerBindings} from '../keys';
import {IdentifierType} from '../models/enums/identifier-type.enum';
@bind({scope: BindingScope.TRANSIENT})
export class ValidatorService {
constructor(
@repository(CalendarRepository)
public calendarRepository: CalendarRepository,
@repository(EventRepository)
public eventRepository: EventRepository,
@repository(SubscriptionRepository)
public subscriptionRepository: SubscriptionRepository,
@inject(AuthenticationBindings.CURRENT_USER)
private readonly currentUser: IAuthUserWithPermissions,
) {}
async calendarExists(calendarId?: string) {
const calendar = await this.calendarRepository.findOne({
where: {
id: calendarId,
},
});
if (calendar) {
return true;
} else {
return false;
}
}
minMaxTime(timeMin?: Date, timeMax?: Date) {
if (timeMax && timeMin && new Date(timeMax) < new Date(timeMin)) {
return false;
} else {
return true;
}
}
async eventExists(eventId?: string) {
const event = await this.eventRepository.findOne({
where: {
id: eventId,
},
});
if (event) {
return true;
} else {
return false;
}
}
@inject(SchedulerBindings.Config, {
optional: true,
})
private readonly schdulerConfig?: ISchedulerConfig;
async primaryToCalendarId(id: string) {
if (id === 'primary') {
let identifierType = this.schdulerConfig?.identifierMappedTo;
if (!identifierType) {
identifierType = IdentifierType.Id;
}
const subcription = await this.subscriptionRepository.findOne({
where: {
and: [
{identifier: this.currentUser[identifierType]},
{isPrimary: true},
],
},
});
if (subcription) {
id = subcription.calendarId;
} else {
return null;
}
}
return id;
}
}