-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheventClient.ts
More file actions
323 lines (277 loc) · 8.05 KB
/
eventClient.ts
File metadata and controls
323 lines (277 loc) · 8.05 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { IdempotentRequest, NitteiBaseClient } from './baseClient'
import type {
CreateBatchEventsAPIResponse,
CreateBatchEventsRequestBody,
DeleteManyEventsRequestBody,
GetEventsByExternalIdAPIResponse,
GetEventsForUsersInTimeSpanAPIResponse,
GetEventsForUsersInTimeSpanBody,
SearchEventsAPIResponse,
SearchEventsRequestBody,
} from './gen_types'
import type { CalendarEventDTO } from './gen_types/CalendarEventDTO'
import type { CalendarEventResponse } from './gen_types/CalendarEventResponse'
import type { CreateEventRequestBody } from './gen_types/CreateEventRequestBody'
import type { GetEventInstancesAPIResponse } from './gen_types/GetEventInstancesAPIResponse'
import type { ID } from './gen_types/ID'
import type { UpdateEventRequestBody } from './gen_types/UpdateEventRequestBody'
import {
replaceEventStringsToDates,
replaceInstanceStringsToDates,
} from './helpers/datesConverters'
/**
* Timespan for getting event instances
*/
export type Timespan = {
startTime: Date
endTime: Date
}
const EVENT_SEARCH_ENDPOINT = '/events/search'
/**
* Client for the events' endpoints
* This is an admin client (usually backend)
*/
export class NitteiEventClient extends NitteiBaseClient {
/**
* Update an event
* @param eventId - id of the event
* @param data - data of the event
* @returns - the updated event
*/
public async update(
eventId: ID,
data: UpdateEventRequestBody
): Promise<CalendarEventResponse> {
const res = await this.patch<CalendarEventResponse>(
`/user/events/${eventId}`,
data
)
replaceEventStringsToDates(res.event)
return res
}
/**
* Create a new calendar event
* @param userId - id of the user
* @param data - data of the event
* @returns - the created CalendarEvent
*/
public async create(
userId: ID,
data: CreateEventRequestBody
): Promise<CalendarEventResponse> {
const res = await this.post<CalendarEventResponse>(
`/user/${userId}/events`,
data
)
replaceEventStringsToDates(res.event)
return res
}
/**
* Create a batch of events for a user
* Either all events are created or none are created
* @param userId - id of the user
* @param data - data of the events
* @returns - the created events
*/
public async createMany(
userId: ID,
data: CreateBatchEventsRequestBody
): Promise<CreateBatchEventsAPIResponse> {
const res = await this.post<CreateBatchEventsAPIResponse>(
`/user/${userId}/events/batch`,
data
)
for (const event of res.events) {
replaceEventStringsToDates(event)
}
return res
}
/**
* Get an event by its id
* @param eventId - id of the event
* @returns - the event
*/
public async getById(eventId: ID): Promise<CalendarEventResponse> {
const res = await this.get<CalendarEventResponse>(`/user/events/${eventId}`)
replaceEventStringsToDates(res.event)
return res
}
/**
* Get events by external id
*
* This returns an array of events (it can match zero, one or more events)
* @param externalId - external id of the event(s) to search
* @returns - the events found
*/
public async getByExternalId(
externalId: string
): Promise<GetEventsByExternalIdAPIResponse> {
const res = await this.get<GetEventsByExternalIdAPIResponse>(
`/user/events/external_id/${externalId}`
)
for (const event of res.events) {
replaceEventStringsToDates(event)
}
return res
}
/**
* Search events given the options
* @param options - options - see {@link SearchEventsRequestBody} for more details
* @returns - the events found
*/
@IdempotentRequest(EVENT_SEARCH_ENDPOINT)
public async searchEvents(
options: SearchEventsRequestBody
): Promise<SearchEventsAPIResponse> {
const res = await this.post<SearchEventsAPIResponse>(
EVENT_SEARCH_ENDPOINT,
options
)
for (const event of res.events) {
replaceEventStringsToDates(event)
}
return res
}
/**
* Get events for users in a time range
*
* Optionally, it can generate instances of recurring events
* @param body - body for getting events for users in a time range
* @returns - the events found
*/
public async getEventsOfUsersDuringTimespan(
body: GetEventsForUsersInTimeSpanBody
): Promise<GetEventsForUsersInTimeSpanAPIResponse> {
const res = await this.post<GetEventsForUsersInTimeSpanAPIResponse>(
'/events/timespan',
body
)
for (const event of res.events) {
replaceEventStringsToDates(event.event)
for (const instance of event.instances) {
replaceInstanceStringsToDates(instance)
}
}
return res
}
public async findByMeta(
meta: {
key: string
value: string
},
skip: number,
limit: number
): Promise<{ events: CalendarEventDTO[] }> {
const res = await this.get<{ events: CalendarEventDTO[] }>('/events/meta', {
skip,
limit,
key: meta.key,
value: meta.value,
})
for (const event of res.events) {
replaceEventStringsToDates(event)
}
return res
}
public async remove(eventId: ID) {
return await this.delete<CalendarEventResponse>(`/user/events/${eventId}`)
}
/**
* Remove many events (either by id and/or by external id)
* @param body.eventIds - ids of the events to remove
* @param body.externalIds - external ids of the events to remove
* @returns Nothing
*/
public async removeMany(body: DeleteManyEventsRequestBody): Promise<void> {
await this.post<CalendarEventResponse>('/user/events/delete_many', body)
}
public async getInstances(
eventId: ID,
timespan: Timespan
): Promise<GetEventInstancesAPIResponse> {
const res = await this.get<GetEventInstancesAPIResponse>(
`/user/events/${eventId}/instances`,
{
startTime: timespan.startTime.toISOString(),
endTime: timespan.endTime.toISOString(),
}
)
replaceEventStringsToDates(res.event)
for (const instance of res.instances) {
replaceInstanceStringsToDates(instance)
}
return res
}
}
/**
* Client for the event endpoints
* This is an end user client (usually frontend)
*/
export class NitteiEventUserClient extends NitteiBaseClient {
/**
* Update an event
* @param eventId - id of the event
* @param data - data of the event
* @returns - the updated event
*/
public async update(
eventId: ID,
data: UpdateEventRequestBody
): Promise<CalendarEventResponse> {
const res = await this.patch<CalendarEventResponse>(
`/events/${eventId}`,
data
)
replaceEventStringsToDates(res.event)
return res
}
/**
* Update an event (V2)
* @param eventId - id of the event
* @param data - data of the event
* @returns - the updated event
*/
public async updateV2(
eventId: ID,
data: UpdateEventRequestBody
): Promise<CalendarEventResponse> {
const res = await this.put<CalendarEventResponse>(
`/events_v2/${eventId}`,
data
)
replaceEventStringsToDates(res.event)
return res
}
public async create(
data: CreateEventRequestBody
): Promise<CalendarEventResponse> {
const res = await this.post<CalendarEventResponse>('/events', data)
replaceEventStringsToDates(res.event)
return res
}
public async findById(eventId: ID): Promise<CalendarEventResponse> {
const res = await this.get<CalendarEventResponse>(`/events/${eventId}`)
replaceEventStringsToDates(res.event)
return res
}
public async remove(eventId: ID) {
return await this.delete<CalendarEventResponse>(`/events/${eventId}`)
}
public async getInstances(
eventId: ID,
timespan: Timespan
): Promise<GetEventInstancesAPIResponse> {
const res = await this.get<GetEventInstancesAPIResponse>(
`/events/${eventId}/instances`,
{
startTime: timespan.startTime.toISOString(),
endTime: timespan.endTime.toISOString(),
}
)
replaceEventStringsToDates(res.event)
for (const instance of res.instances) {
replaceInstanceStringsToDates(instance)
}
return res
}
}