@remit/calendar-service 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/accept.test.ts +357 -0
- package/src/accept.ts +207 -0
- package/src/index.ts +15 -0
- package/src/memory-store.ts +303 -0
- package/src/parse.test.ts +11 -0
- package/src/parse.ts +7 -1
- package/src/put.test.ts +1 -210
- package/src/suggest.test.ts +329 -0
- package/src/suggest.ts +199 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CalendarCollectionItem,
|
|
3
|
+
CalendarEventIndexItem,
|
|
4
|
+
CalendarObjectItem,
|
|
5
|
+
CalendarOccurrenceInput,
|
|
6
|
+
CalendarSuggestionItem,
|
|
7
|
+
CreateCalendarCollectionInput,
|
|
8
|
+
ICalendarCollectionRepository,
|
|
9
|
+
ICalendarEventIndexRepository,
|
|
10
|
+
ICalendarObjectRepository,
|
|
11
|
+
ICalendarSuggestionRepository,
|
|
12
|
+
ICalendarUnitOfWork,
|
|
13
|
+
PutCalendarObjectInput as PutCalendarObjectRow,
|
|
14
|
+
PutCalendarSuggestionInput,
|
|
15
|
+
SettleCalendarSuggestionInput,
|
|
16
|
+
UpdateCalendarCollectionInput,
|
|
17
|
+
} from "@remit/data-ports";
|
|
18
|
+
import {
|
|
19
|
+
deriveCalendarId,
|
|
20
|
+
deriveCalendarObjectId,
|
|
21
|
+
deriveCalendarSuggestionId,
|
|
22
|
+
normalizeCalendarUrlSegment,
|
|
23
|
+
} from "@remit/data-ports/id";
|
|
24
|
+
import { CalendarSuggestionState } from "@remit/domain-enums";
|
|
25
|
+
|
|
26
|
+
export class MissingRow extends Error {}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A pass-through unit of work over plain maps — the shape the port documents
|
|
30
|
+
* for a backend with no cross-entity transaction. It proves the write path
|
|
31
|
+
* calls what it should, in the order it should; that the three writes stand or
|
|
32
|
+
* fall together is proven against a real transaction, in
|
|
33
|
+
* drizzle-service's calendar-put.sqlite.test.ts.
|
|
34
|
+
*/
|
|
35
|
+
export class MemoryCalendarStore implements ICalendarUnitOfWork {
|
|
36
|
+
readonly collections = new Map<string, CalendarCollectionItem>();
|
|
37
|
+
readonly objects = new Map<string, CalendarObjectItem>();
|
|
38
|
+
readonly occurrences = new Map<string, CalendarEventIndexItem[]>();
|
|
39
|
+
readonly suggestions = new Map<string, CalendarSuggestionItem>();
|
|
40
|
+
|
|
41
|
+
private readonly collectionRepo: ICalendarCollectionRepository = {
|
|
42
|
+
create: async (input: CreateCalendarCollectionInput) => {
|
|
43
|
+
const urlSegment = normalizeCalendarUrlSegment(input.urlSegment);
|
|
44
|
+
const calendarId = deriveCalendarId(input.accountConfigId, urlSegment);
|
|
45
|
+
const existing = this.collections.get(calendarId);
|
|
46
|
+
if (existing) return existing;
|
|
47
|
+
const now = Date.now();
|
|
48
|
+
const collection: CalendarCollectionItem = {
|
|
49
|
+
calendarId,
|
|
50
|
+
accountConfigId: input.accountConfigId,
|
|
51
|
+
urlSegment,
|
|
52
|
+
displayName: input.displayName,
|
|
53
|
+
color: input.color ?? "Cal1",
|
|
54
|
+
componentSet: input.componentSet ?? "VeventOnly",
|
|
55
|
+
source: input.source ?? "UserCreated",
|
|
56
|
+
timezone: input.timezone ?? "",
|
|
57
|
+
syncSequence: 0,
|
|
58
|
+
createdAt: now,
|
|
59
|
+
updatedAt: now,
|
|
60
|
+
};
|
|
61
|
+
this.collections.set(calendarId, collection);
|
|
62
|
+
return collection;
|
|
63
|
+
},
|
|
64
|
+
get: async (_accountConfigId: string, calendarId: string) => {
|
|
65
|
+
const collection = this.collections.get(calendarId);
|
|
66
|
+
if (!collection) throw new MissingRow(calendarId);
|
|
67
|
+
return collection;
|
|
68
|
+
},
|
|
69
|
+
update: async (
|
|
70
|
+
_accountConfigId: string,
|
|
71
|
+
calendarId: string,
|
|
72
|
+
input: UpdateCalendarCollectionInput,
|
|
73
|
+
) => {
|
|
74
|
+
const collection = this.collections.get(calendarId);
|
|
75
|
+
if (!collection) throw new MissingRow(calendarId);
|
|
76
|
+
const updated = { ...collection, ...input };
|
|
77
|
+
this.collections.set(calendarId, updated);
|
|
78
|
+
return updated;
|
|
79
|
+
},
|
|
80
|
+
delete: async (_accountConfigId: string, calendarId: string) => {
|
|
81
|
+
this.collections.delete(calendarId);
|
|
82
|
+
},
|
|
83
|
+
listByAccountConfig: async (accountConfigId: string) =>
|
|
84
|
+
[...this.collections.values()].filter(
|
|
85
|
+
(collection) => collection.accountConfigId === accountConfigId,
|
|
86
|
+
),
|
|
87
|
+
createExclusive: async (input: CreateCalendarCollectionInput) => {
|
|
88
|
+
const calendarId = deriveCalendarId(
|
|
89
|
+
input.accountConfigId,
|
|
90
|
+
input.urlSegment,
|
|
91
|
+
);
|
|
92
|
+
if (this.collections.has(calendarId)) return null;
|
|
93
|
+
return this.collectionRepo.create(input);
|
|
94
|
+
},
|
|
95
|
+
findByUrlSegment: async (accountConfigId: string, urlSegment: string) =>
|
|
96
|
+
this.collections.get(deriveCalendarId(accountConfigId, urlSegment)) ??
|
|
97
|
+
null,
|
|
98
|
+
bumpSyncSequence: async (_accountConfigId: string, calendarId: string) => {
|
|
99
|
+
const collection = this.collections.get(calendarId);
|
|
100
|
+
if (!collection) throw new MissingRow(calendarId);
|
|
101
|
+
const bumped = {
|
|
102
|
+
...collection,
|
|
103
|
+
syncSequence: collection.syncSequence + 1,
|
|
104
|
+
};
|
|
105
|
+
this.collections.set(calendarId, bumped);
|
|
106
|
+
return bumped.syncSequence;
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
private readonly objectRepo: ICalendarObjectRepository = {
|
|
111
|
+
put: async (input: PutCalendarObjectRow) => {
|
|
112
|
+
const calendarObjectId = deriveCalendarObjectId(
|
|
113
|
+
input.calendarId,
|
|
114
|
+
input.resourceName,
|
|
115
|
+
);
|
|
116
|
+
const now = Date.now();
|
|
117
|
+
const object: CalendarObjectItem = {
|
|
118
|
+
...input,
|
|
119
|
+
calendarObjectId,
|
|
120
|
+
createdAt: this.objects.get(calendarObjectId)?.createdAt ?? now,
|
|
121
|
+
updatedAt: now,
|
|
122
|
+
};
|
|
123
|
+
this.objects.set(calendarObjectId, object);
|
|
124
|
+
return object;
|
|
125
|
+
},
|
|
126
|
+
listIncompleteExpansions: async (calendarId: string, instant: string) =>
|
|
127
|
+
[...this.objects.values()].filter(
|
|
128
|
+
(object) =>
|
|
129
|
+
object.calendarId === calendarId &&
|
|
130
|
+
object.expandedThrough !== "" &&
|
|
131
|
+
object.expandedThrough < instant,
|
|
132
|
+
),
|
|
133
|
+
find: async (_calendarId: string, calendarObjectId: string) =>
|
|
134
|
+
this.objects.get(calendarObjectId) ?? null,
|
|
135
|
+
get: async (_calendarId: string, calendarObjectId: string) => {
|
|
136
|
+
const object = this.objects.get(calendarObjectId);
|
|
137
|
+
if (!object) throw new MissingRow(calendarObjectId);
|
|
138
|
+
return object;
|
|
139
|
+
},
|
|
140
|
+
delete: async (_calendarId: string, calendarObjectId: string) => {
|
|
141
|
+
this.objects.delete(calendarObjectId);
|
|
142
|
+
},
|
|
143
|
+
findByResourceName: async (calendarId: string, resourceName: string) =>
|
|
144
|
+
this.objects.get(deriveCalendarObjectId(calendarId, resourceName)) ??
|
|
145
|
+
null,
|
|
146
|
+
findByUid: async (calendarId: string, icalUid: string) =>
|
|
147
|
+
[...this.objects.values()].find(
|
|
148
|
+
(object) =>
|
|
149
|
+
object.calendarId === calendarId && object.icalUid === icalUid,
|
|
150
|
+
) ?? null,
|
|
151
|
+
listByCalendar: async (calendarId: string) =>
|
|
152
|
+
[...this.objects.values()].filter(
|
|
153
|
+
(object) => object.calendarId === calendarId,
|
|
154
|
+
),
|
|
155
|
+
listChangedSince: async (calendarId: string, syncSequence: number) =>
|
|
156
|
+
[...this.objects.values()]
|
|
157
|
+
.filter(
|
|
158
|
+
(object) =>
|
|
159
|
+
object.calendarId === calendarId &&
|
|
160
|
+
object.syncSequence > syncSequence,
|
|
161
|
+
)
|
|
162
|
+
.sort((left, right) => left.syncSequence - right.syncSequence),
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
private readonly eventIndexRepo: ICalendarEventIndexRepository = {
|
|
166
|
+
replaceForObject: async (
|
|
167
|
+
calendarId: string,
|
|
168
|
+
calendarObjectId: string,
|
|
169
|
+
occurrences: CalendarOccurrenceInput[],
|
|
170
|
+
) => {
|
|
171
|
+
const now = Date.now();
|
|
172
|
+
this.occurrences.set(
|
|
173
|
+
calendarObjectId,
|
|
174
|
+
occurrences.map((occurrence) => ({
|
|
175
|
+
...occurrence,
|
|
176
|
+
calendarId,
|
|
177
|
+
calendarObjectId,
|
|
178
|
+
createdAt: now,
|
|
179
|
+
updatedAt: now,
|
|
180
|
+
})),
|
|
181
|
+
);
|
|
182
|
+
},
|
|
183
|
+
deleteForObject: async (_calendarId: string, calendarObjectId: string) => {
|
|
184
|
+
this.occurrences.delete(calendarObjectId);
|
|
185
|
+
},
|
|
186
|
+
listForObject: async (_calendarId: string, calendarObjectId: string) =>
|
|
187
|
+
this.occurrences.get(calendarObjectId) ?? [],
|
|
188
|
+
listByStartRange: async (
|
|
189
|
+
calendarId: string,
|
|
190
|
+
startAt: string,
|
|
191
|
+
endAt: string,
|
|
192
|
+
) =>
|
|
193
|
+
[...this.occurrences.values()]
|
|
194
|
+
.flat()
|
|
195
|
+
.filter(
|
|
196
|
+
(row) =>
|
|
197
|
+
row.calendarId === calendarId &&
|
|
198
|
+
row.startAt >= startAt &&
|
|
199
|
+
row.startAt < endAt,
|
|
200
|
+
)
|
|
201
|
+
.sort((left, right) => left.startAt.localeCompare(right.startAt)),
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
private readonly suggestionRepo: ICalendarSuggestionRepository = {
|
|
205
|
+
put: async (input: PutCalendarSuggestionInput) => {
|
|
206
|
+
const suggestionId = deriveCalendarSuggestionId(
|
|
207
|
+
input.messageId,
|
|
208
|
+
input.bodyPartId,
|
|
209
|
+
input.icalUid,
|
|
210
|
+
);
|
|
211
|
+
const now = Date.now();
|
|
212
|
+
const existing = this.suggestions.get(suggestionId);
|
|
213
|
+
const suggestion: CalendarSuggestionItem = {
|
|
214
|
+
...input,
|
|
215
|
+
suggestionId,
|
|
216
|
+
state: existing?.state ?? CalendarSuggestionState.Pending,
|
|
217
|
+
acceptedCalendarObjectId: existing?.acceptedCalendarObjectId ?? "",
|
|
218
|
+
createdAt: existing?.createdAt ?? now,
|
|
219
|
+
updatedAt: now,
|
|
220
|
+
};
|
|
221
|
+
this.suggestions.set(suggestionId, suggestion);
|
|
222
|
+
return suggestion;
|
|
223
|
+
},
|
|
224
|
+
get: async (accountConfigId: string, suggestionId: string) => {
|
|
225
|
+
const suggestion = this.suggestions.get(suggestionId);
|
|
226
|
+
if (!suggestion || suggestion.accountConfigId !== accountConfigId) {
|
|
227
|
+
throw new MissingRow(suggestionId);
|
|
228
|
+
}
|
|
229
|
+
return suggestion;
|
|
230
|
+
},
|
|
231
|
+
listByMessage: async (accountConfigId: string, messageId: string) =>
|
|
232
|
+
[...this.suggestions.values()].filter(
|
|
233
|
+
(suggestion) =>
|
|
234
|
+
suggestion.accountConfigId === accountConfigId &&
|
|
235
|
+
suggestion.messageId === messageId,
|
|
236
|
+
),
|
|
237
|
+
listByState: async (
|
|
238
|
+
accountConfigId: string,
|
|
239
|
+
state: CalendarSuggestionItem["state"],
|
|
240
|
+
) => ({
|
|
241
|
+
items: [...this.suggestions.values()].filter(
|
|
242
|
+
(suggestion) =>
|
|
243
|
+
suggestion.accountConfigId === accountConfigId &&
|
|
244
|
+
suggestion.state === state,
|
|
245
|
+
),
|
|
246
|
+
continuationToken: undefined,
|
|
247
|
+
}),
|
|
248
|
+
settle: async (
|
|
249
|
+
accountConfigId: string,
|
|
250
|
+
suggestionId: string,
|
|
251
|
+
input: SettleCalendarSuggestionInput,
|
|
252
|
+
) => {
|
|
253
|
+
const suggestion = this.suggestions.get(suggestionId);
|
|
254
|
+
if (!suggestion || suggestion.accountConfigId !== accountConfigId) {
|
|
255
|
+
throw new MissingRow(suggestionId);
|
|
256
|
+
}
|
|
257
|
+
const settled = { ...suggestion, ...input, updatedAt: Date.now() };
|
|
258
|
+
this.suggestions.set(suggestionId, settled);
|
|
259
|
+
return settled;
|
|
260
|
+
},
|
|
261
|
+
supersedeIfPending: async (
|
|
262
|
+
accountConfigId: string,
|
|
263
|
+
suggestionId: string,
|
|
264
|
+
) => {
|
|
265
|
+
const suggestion = this.suggestions.get(suggestionId);
|
|
266
|
+
if (
|
|
267
|
+
!suggestion ||
|
|
268
|
+
suggestion.accountConfigId !== accountConfigId ||
|
|
269
|
+
suggestion.state !== CalendarSuggestionState.Pending
|
|
270
|
+
) {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
const retired = {
|
|
274
|
+
...suggestion,
|
|
275
|
+
state: CalendarSuggestionState.Superseded,
|
|
276
|
+
acceptedCalendarObjectId: "",
|
|
277
|
+
updatedAt: Date.now(),
|
|
278
|
+
};
|
|
279
|
+
this.suggestions.set(suggestionId, retired);
|
|
280
|
+
return retired;
|
|
281
|
+
},
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
get calendarSuggestion(): ICalendarSuggestionRepository {
|
|
285
|
+
return this.suggestionRepo;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
transaction<T>(
|
|
289
|
+
fn: (repos: {
|
|
290
|
+
calendarCollection: ICalendarCollectionRepository;
|
|
291
|
+
calendarObject: ICalendarObjectRepository;
|
|
292
|
+
calendarEventIndex: ICalendarEventIndexRepository;
|
|
293
|
+
calendarSuggestion: ICalendarSuggestionRepository;
|
|
294
|
+
}) => Promise<T>,
|
|
295
|
+
): Promise<T> {
|
|
296
|
+
return fn({
|
|
297
|
+
calendarCollection: this.collectionRepo,
|
|
298
|
+
calendarObject: this.objectRepo,
|
|
299
|
+
calendarEventIndex: this.eventIndexRepo,
|
|
300
|
+
calendarSuggestion: this.suggestionRepo,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
package/src/parse.test.ts
CHANGED
|
@@ -56,6 +56,17 @@ describe("parseCalendar", () => {
|
|
|
56
56
|
);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
+
it("refuses empty bytes as a value, naming nothing rather than crashing", async () => {
|
|
60
|
+
// A suggestion read out of prose carries no iCalendar at all, and the
|
|
61
|
+
// accept path reaches this with `""`. ical.js reads empty bytes as a
|
|
62
|
+
// component with no name, so naming what arrived would throw here — a
|
|
63
|
+
// crash on ordinary input rather than the refusal every caller renders.
|
|
64
|
+
const refusal = await refusalCode("");
|
|
65
|
+
|
|
66
|
+
assert.equal(refusal.code, "NotACalendar");
|
|
67
|
+
assert.match(refusal.message, /found nothing/);
|
|
68
|
+
});
|
|
69
|
+
|
|
59
70
|
it("refuses a document whose root is not a VCALENDAR", async () => {
|
|
60
71
|
assert.equal(
|
|
61
72
|
(
|
package/src/parse.ts
CHANGED
|
@@ -64,9 +64,15 @@ export const parseCalendar = async (
|
|
|
64
64
|
const component = read.value;
|
|
65
65
|
|
|
66
66
|
if (component.name !== "vcalendar") {
|
|
67
|
+
// A document with nothing in it names nothing: ical.js reads empty bytes
|
|
68
|
+
// as a component with no name at all, and a suggestion read out of prose
|
|
69
|
+
// carries exactly that. Naming what arrived is for a document that has a
|
|
70
|
+
// root to name.
|
|
67
71
|
return calendarFailure(
|
|
68
72
|
"NotACalendar",
|
|
69
|
-
|
|
73
|
+
component.name
|
|
74
|
+
? `expected a VCALENDAR, found ${component.name.toUpperCase()}`
|
|
75
|
+
: "expected a VCALENDAR, found nothing",
|
|
70
76
|
);
|
|
71
77
|
}
|
|
72
78
|
|
package/src/put.test.ts
CHANGED
|
@@ -1,25 +1,8 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { describe, it } from "node:test";
|
|
3
|
-
import type {
|
|
4
|
-
CalendarCollectionItem,
|
|
5
|
-
CalendarEventIndexItem,
|
|
6
|
-
CalendarObjectItem,
|
|
7
|
-
CalendarOccurrenceInput,
|
|
8
|
-
CreateCalendarCollectionInput,
|
|
9
|
-
ICalendarCollectionRepository,
|
|
10
|
-
ICalendarEventIndexRepository,
|
|
11
|
-
ICalendarObjectRepository,
|
|
12
|
-
ICalendarUnitOfWork,
|
|
13
|
-
PutCalendarObjectInput as PutCalendarObjectRow,
|
|
14
|
-
UpdateCalendarCollectionInput,
|
|
15
|
-
} from "@remit/data-ports";
|
|
16
|
-
import {
|
|
17
|
-
deriveCalendarId,
|
|
18
|
-
deriveCalendarObjectId,
|
|
19
|
-
normalizeCalendarUrlSegment,
|
|
20
|
-
} from "@remit/data-ports/id";
|
|
21
3
|
import { computeEtag } from "./etag.js";
|
|
22
4
|
import { asLf, singleEvent } from "./fixtures.js";
|
|
5
|
+
import { MemoryCalendarStore, MissingRow } from "./memory-store.js";
|
|
23
6
|
import { parseCalendar, serializeCalendar } from "./parse.js";
|
|
24
7
|
import {
|
|
25
8
|
DEFAULT_CALENDAR_URL_SEGMENT,
|
|
@@ -28,198 +11,6 @@ import {
|
|
|
28
11
|
putCalendarObject,
|
|
29
12
|
} from "./put.js";
|
|
30
13
|
|
|
31
|
-
class MissingRow extends Error {}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A pass-through unit of work over plain maps — the shape the port documents
|
|
35
|
-
* for a backend with no cross-entity transaction. It proves the write path
|
|
36
|
-
* calls what it should, in the order it should; that the three writes stand or
|
|
37
|
-
* fall together is proven against a real transaction, in
|
|
38
|
-
* drizzle-service's calendar-put.sqlite.test.ts.
|
|
39
|
-
*/
|
|
40
|
-
class MemoryCalendarStore implements ICalendarUnitOfWork {
|
|
41
|
-
readonly collections = new Map<string, CalendarCollectionItem>();
|
|
42
|
-
readonly objects = new Map<string, CalendarObjectItem>();
|
|
43
|
-
readonly occurrences = new Map<string, CalendarEventIndexItem[]>();
|
|
44
|
-
|
|
45
|
-
private readonly collectionRepo: ICalendarCollectionRepository = {
|
|
46
|
-
create: async (input: CreateCalendarCollectionInput) => {
|
|
47
|
-
const urlSegment = normalizeCalendarUrlSegment(input.urlSegment);
|
|
48
|
-
const calendarId = deriveCalendarId(input.accountConfigId, urlSegment);
|
|
49
|
-
const existing = this.collections.get(calendarId);
|
|
50
|
-
if (existing) return existing;
|
|
51
|
-
const now = Date.now();
|
|
52
|
-
const collection: CalendarCollectionItem = {
|
|
53
|
-
calendarId,
|
|
54
|
-
accountConfigId: input.accountConfigId,
|
|
55
|
-
urlSegment,
|
|
56
|
-
displayName: input.displayName,
|
|
57
|
-
color: input.color ?? "Cal1",
|
|
58
|
-
componentSet: input.componentSet ?? "VeventOnly",
|
|
59
|
-
source: input.source ?? "UserCreated",
|
|
60
|
-
timezone: input.timezone ?? "",
|
|
61
|
-
syncSequence: 0,
|
|
62
|
-
createdAt: now,
|
|
63
|
-
updatedAt: now,
|
|
64
|
-
};
|
|
65
|
-
this.collections.set(calendarId, collection);
|
|
66
|
-
return collection;
|
|
67
|
-
},
|
|
68
|
-
get: async (_accountConfigId: string, calendarId: string) => {
|
|
69
|
-
const collection = this.collections.get(calendarId);
|
|
70
|
-
if (!collection) throw new MissingRow(calendarId);
|
|
71
|
-
return collection;
|
|
72
|
-
},
|
|
73
|
-
update: async (
|
|
74
|
-
_accountConfigId: string,
|
|
75
|
-
calendarId: string,
|
|
76
|
-
input: UpdateCalendarCollectionInput,
|
|
77
|
-
) => {
|
|
78
|
-
const collection = this.collections.get(calendarId);
|
|
79
|
-
if (!collection) throw new MissingRow(calendarId);
|
|
80
|
-
const updated = { ...collection, ...input };
|
|
81
|
-
this.collections.set(calendarId, updated);
|
|
82
|
-
return updated;
|
|
83
|
-
},
|
|
84
|
-
delete: async (_accountConfigId: string, calendarId: string) => {
|
|
85
|
-
this.collections.delete(calendarId);
|
|
86
|
-
},
|
|
87
|
-
listByAccountConfig: async (accountConfigId: string) =>
|
|
88
|
-
[...this.collections.values()].filter(
|
|
89
|
-
(collection) => collection.accountConfigId === accountConfigId,
|
|
90
|
-
),
|
|
91
|
-
createExclusive: async (input: CreateCalendarCollectionInput) => {
|
|
92
|
-
const calendarId = deriveCalendarId(
|
|
93
|
-
input.accountConfigId,
|
|
94
|
-
input.urlSegment,
|
|
95
|
-
);
|
|
96
|
-
if (this.collections.has(calendarId)) return null;
|
|
97
|
-
return this.collectionRepo.create(input);
|
|
98
|
-
},
|
|
99
|
-
findByUrlSegment: async (accountConfigId: string, urlSegment: string) =>
|
|
100
|
-
this.collections.get(deriveCalendarId(accountConfigId, urlSegment)) ??
|
|
101
|
-
null,
|
|
102
|
-
bumpSyncSequence: async (_accountConfigId: string, calendarId: string) => {
|
|
103
|
-
const collection = this.collections.get(calendarId);
|
|
104
|
-
if (!collection) throw new MissingRow(calendarId);
|
|
105
|
-
const bumped = {
|
|
106
|
-
...collection,
|
|
107
|
-
syncSequence: collection.syncSequence + 1,
|
|
108
|
-
};
|
|
109
|
-
this.collections.set(calendarId, bumped);
|
|
110
|
-
return bumped.syncSequence;
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
private readonly objectRepo: ICalendarObjectRepository = {
|
|
115
|
-
put: async (input: PutCalendarObjectRow) => {
|
|
116
|
-
const calendarObjectId = deriveCalendarObjectId(
|
|
117
|
-
input.calendarId,
|
|
118
|
-
input.resourceName,
|
|
119
|
-
);
|
|
120
|
-
const now = Date.now();
|
|
121
|
-
const object: CalendarObjectItem = {
|
|
122
|
-
...input,
|
|
123
|
-
calendarObjectId,
|
|
124
|
-
createdAt: this.objects.get(calendarObjectId)?.createdAt ?? now,
|
|
125
|
-
updatedAt: now,
|
|
126
|
-
};
|
|
127
|
-
this.objects.set(calendarObjectId, object);
|
|
128
|
-
return object;
|
|
129
|
-
},
|
|
130
|
-
listIncompleteExpansions: async (calendarId: string, instant: string) =>
|
|
131
|
-
[...this.objects.values()].filter(
|
|
132
|
-
(object) =>
|
|
133
|
-
object.calendarId === calendarId &&
|
|
134
|
-
object.expandedThrough !== "" &&
|
|
135
|
-
object.expandedThrough < instant,
|
|
136
|
-
),
|
|
137
|
-
find: async (_calendarId: string, calendarObjectId: string) =>
|
|
138
|
-
this.objects.get(calendarObjectId) ?? null,
|
|
139
|
-
get: async (_calendarId: string, calendarObjectId: string) => {
|
|
140
|
-
const object = this.objects.get(calendarObjectId);
|
|
141
|
-
if (!object) throw new MissingRow(calendarObjectId);
|
|
142
|
-
return object;
|
|
143
|
-
},
|
|
144
|
-
delete: async (_calendarId: string, calendarObjectId: string) => {
|
|
145
|
-
this.objects.delete(calendarObjectId);
|
|
146
|
-
},
|
|
147
|
-
findByResourceName: async (calendarId: string, resourceName: string) =>
|
|
148
|
-
this.objects.get(deriveCalendarObjectId(calendarId, resourceName)) ??
|
|
149
|
-
null,
|
|
150
|
-
findByUid: async (calendarId: string, icalUid: string) =>
|
|
151
|
-
[...this.objects.values()].find(
|
|
152
|
-
(object) =>
|
|
153
|
-
object.calendarId === calendarId && object.icalUid === icalUid,
|
|
154
|
-
) ?? null,
|
|
155
|
-
listByCalendar: async (calendarId: string) =>
|
|
156
|
-
[...this.objects.values()].filter(
|
|
157
|
-
(object) => object.calendarId === calendarId,
|
|
158
|
-
),
|
|
159
|
-
listChangedSince: async (calendarId: string, syncSequence: number) =>
|
|
160
|
-
[...this.objects.values()]
|
|
161
|
-
.filter(
|
|
162
|
-
(object) =>
|
|
163
|
-
object.calendarId === calendarId &&
|
|
164
|
-
object.syncSequence > syncSequence,
|
|
165
|
-
)
|
|
166
|
-
.sort((left, right) => left.syncSequence - right.syncSequence),
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
private readonly eventIndexRepo: ICalendarEventIndexRepository = {
|
|
170
|
-
replaceForObject: async (
|
|
171
|
-
calendarId: string,
|
|
172
|
-
calendarObjectId: string,
|
|
173
|
-
occurrences: CalendarOccurrenceInput[],
|
|
174
|
-
) => {
|
|
175
|
-
const now = Date.now();
|
|
176
|
-
this.occurrences.set(
|
|
177
|
-
calendarObjectId,
|
|
178
|
-
occurrences.map((occurrence) => ({
|
|
179
|
-
...occurrence,
|
|
180
|
-
calendarId,
|
|
181
|
-
calendarObjectId,
|
|
182
|
-
createdAt: now,
|
|
183
|
-
updatedAt: now,
|
|
184
|
-
})),
|
|
185
|
-
);
|
|
186
|
-
},
|
|
187
|
-
deleteForObject: async (_calendarId: string, calendarObjectId: string) => {
|
|
188
|
-
this.occurrences.delete(calendarObjectId);
|
|
189
|
-
},
|
|
190
|
-
listForObject: async (_calendarId: string, calendarObjectId: string) =>
|
|
191
|
-
this.occurrences.get(calendarObjectId) ?? [],
|
|
192
|
-
listByStartRange: async (
|
|
193
|
-
calendarId: string,
|
|
194
|
-
startAt: string,
|
|
195
|
-
endAt: string,
|
|
196
|
-
) =>
|
|
197
|
-
[...this.occurrences.values()]
|
|
198
|
-
.flat()
|
|
199
|
-
.filter(
|
|
200
|
-
(row) =>
|
|
201
|
-
row.calendarId === calendarId &&
|
|
202
|
-
row.startAt >= startAt &&
|
|
203
|
-
row.startAt < endAt,
|
|
204
|
-
)
|
|
205
|
-
.sort((left, right) => left.startAt.localeCompare(right.startAt)),
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
transaction<T>(
|
|
209
|
-
fn: (repos: {
|
|
210
|
-
calendarCollection: ICalendarCollectionRepository;
|
|
211
|
-
calendarObject: ICalendarObjectRepository;
|
|
212
|
-
calendarEventIndex: ICalendarEventIndexRepository;
|
|
213
|
-
}) => Promise<T>,
|
|
214
|
-
): Promise<T> {
|
|
215
|
-
return fn({
|
|
216
|
-
calendarCollection: this.collectionRepo,
|
|
217
|
-
calendarObject: this.objectRepo,
|
|
218
|
-
calendarEventIndex: this.eventIndexRepo,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
14
|
const ACCOUNT_CONFIG_ID = "account-config-1";
|
|
224
15
|
|
|
225
16
|
const provisioned = async (): Promise<{
|