@remit/backend 0.0.142 → 0.0.144

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.
Files changed (269) hide show
  1. package/dist/dev-server/content-handler.d.ts +38 -0
  2. package/dist/dev-server/content-handler.js +46 -0
  3. package/dist/src/api.d.ts +7 -0
  4. package/dist/src/api.js +144 -0
  5. package/dist/src/auth.d.ts +30 -0
  6. package/dist/src/auth.js +73 -0
  7. package/dist/src/config/msoauth.d.ts +30 -0
  8. package/dist/src/config/msoauth.js +30 -0
  9. package/{src/data-backend.ts → dist/src/data-backend.d.ts} +2 -4
  10. package/dist/src/data-backend.js +12 -0
  11. package/{src/derive/autoMoved.ts → dist/src/derive/autoMoved.d.ts} +1 -29
  12. package/dist/src/derive/autoMoved.js +49 -0
  13. package/dist/src/derive/contentSignature.d.ts +41 -0
  14. package/{src/derive/contentSignature.ts → dist/src/derive/contentSignature.js} +27 -72
  15. package/dist/src/derive/contentUrl.d.ts +46 -0
  16. package/dist/src/derive/contentUrl.js +46 -0
  17. package/{src/derive/deriveMuted.ts → dist/src/derive/deriveMuted.d.ts} +1 -3
  18. package/dist/src/derive/deriveMuted.js +7 -0
  19. package/dist/src/derive/enrichThreadRows.d.ts +118 -0
  20. package/dist/src/derive/enrichThreadRows.js +151 -0
  21. package/dist/src/derive/filterThreadCriteria.d.ts +23 -0
  22. package/dist/src/derive/filterThreadCriteria.js +24 -0
  23. package/{src/derive/pendingMoveCounts.ts → dist/src/derive/pendingMoveCounts.d.ts} +5 -53
  24. package/dist/src/derive/pendingMoveCounts.js +53 -0
  25. package/{src/derive/senderTrust.ts → dist/src/derive/senderTrust.d.ts} +1 -9
  26. package/dist/src/derive/senderTrust.js +19 -0
  27. package/dist/src/error.d.ts +21 -0
  28. package/dist/src/error.js +85 -0
  29. package/dist/src/handlers/account-guards.d.ts +36 -0
  30. package/dist/src/handlers/account-guards.js +107 -0
  31. package/dist/src/handlers/account-oauth.d.ts +14 -0
  32. package/dist/src/handlers/account-oauth.js +387 -0
  33. package/dist/src/handlers/account-overrides.d.ts +53 -0
  34. package/dist/src/handlers/account-overrides.js +233 -0
  35. package/dist/src/handlers/account-signature.d.ts +28 -0
  36. package/dist/src/handlers/account-signature.js +85 -0
  37. package/dist/src/handlers/account.d.ts +6 -0
  38. package/dist/src/handlers/account.js +350 -0
  39. package/dist/src/handlers/address.d.ts +35 -0
  40. package/dist/src/handlers/address.js +141 -0
  41. package/dist/src/handlers/calendar-event.d.ts +70 -0
  42. package/dist/src/handlers/calendar-event.js +425 -0
  43. package/dist/src/handlers/calendar-feed.d.ts +47 -0
  44. package/dist/src/handlers/calendar-feed.js +192 -0
  45. package/dist/src/handlers/calendar-suggestion.d.ts +47 -0
  46. package/dist/src/handlers/calendar-suggestion.js +212 -0
  47. package/dist/src/handlers/calendar.d.ts +116 -0
  48. package/dist/src/handlers/calendar.js +330 -0
  49. package/dist/src/handlers/config.d.ts +43 -0
  50. package/dist/src/handlers/config.js +176 -0
  51. package/dist/src/handlers/ensure-account-config.d.ts +9 -0
  52. package/dist/src/handlers/ensure-account-config.js +27 -0
  53. package/dist/src/handlers/filter.d.ts +3 -0
  54. package/dist/src/handlers/filter.js +103 -0
  55. package/dist/src/handlers/folder-role-appointments.d.ts +68 -0
  56. package/dist/src/handlers/folder-role-appointments.js +168 -0
  57. package/dist/src/handlers/folder-role-labels.d.ts +26 -0
  58. package/dist/src/handlers/folder-role-labels.js +40 -0
  59. package/dist/src/handlers/folder-role.d.ts +9 -0
  60. package/dist/src/handlers/folder-role.js +40 -0
  61. package/dist/src/handlers/index.d.ts +2 -0
  62. package/dist/src/handlers/index.js +60 -0
  63. package/dist/src/handlers/label.d.ts +43 -0
  64. package/dist/src/handlers/label.js +114 -0
  65. package/dist/src/handlers/mailbox-bindings.d.ts +41 -0
  66. package/dist/src/handlers/mailbox-bindings.js +61 -0
  67. package/dist/src/handlers/mailbox.d.ts +23 -0
  68. package/dist/src/handlers/mailbox.js +219 -0
  69. package/dist/src/handlers/me.d.ts +2 -0
  70. package/dist/src/handlers/me.js +138 -0
  71. package/dist/src/handlers/message.d.ts +84 -0
  72. package/dist/src/handlers/message.js +607 -0
  73. package/dist/src/handlers/organize.d.ts +10 -0
  74. package/dist/src/handlers/organize.js +112 -0
  75. package/dist/src/handlers/outbox-attachment.d.ts +23 -0
  76. package/dist/src/handlers/outbox-attachment.js +81 -0
  77. package/dist/src/handlers/outbox.d.ts +4 -0
  78. package/dist/src/handlers/outbox.js +144 -0
  79. package/dist/src/handlers/sync-progress.d.ts +46 -0
  80. package/dist/src/handlers/sync-progress.js +63 -0
  81. package/dist/src/handlers/sync.d.ts +28 -0
  82. package/dist/src/handlers/sync.js +82 -0
  83. package/dist/src/handlers/system-update.d.ts +2 -0
  84. package/dist/src/handlers/system-update.js +172 -0
  85. package/dist/src/handlers/thread.d.ts +148 -0
  86. package/dist/src/handlers/thread.js +233 -0
  87. package/dist/src/handlers/unified-threads.d.ts +247 -0
  88. package/dist/src/handlers/unified-threads.js +409 -0
  89. package/{src/handlers/vip-suggestions.ts → dist/src/handlers/vip-suggestions.d.ts} +1 -13
  90. package/dist/src/handlers/vip-suggestions.js +16 -0
  91. package/dist/src/index.d.ts +2 -0
  92. package/dist/src/index.js +37 -0
  93. package/{src/json.ts → dist/src/json.d.ts} +1 -4
  94. package/dist/src/json.js +10 -0
  95. package/dist/src/jwt-auth.d.ts +18 -0
  96. package/dist/src/jwt-auth.js +97 -0
  97. package/dist/src/request-context.d.ts +9 -0
  98. package/dist/src/request-context.js +22 -0
  99. package/dist/src/request.d.ts +3 -0
  100. package/dist/src/request.js +7 -0
  101. package/dist/src/response.d.ts +27 -0
  102. package/dist/src/response.js +87 -0
  103. package/dist/src/service/account-deletion.d.ts +22 -0
  104. package/dist/src/service/account-deletion.js +39 -0
  105. package/dist/src/service/compose-sqlite.d.ts +2 -0
  106. package/dist/src/service/compose-sqlite.js +11 -0
  107. package/dist/src/service/create-remit-client.d.ts +95 -0
  108. package/dist/src/service/create-remit-client.js +207 -0
  109. package/dist/src/service/data-client.d.ts +12 -0
  110. package/dist/src/service/data-client.js +44 -0
  111. package/dist/src/service/export-identity.d.ts +3 -0
  112. package/dist/src/service/export-identity.js +35 -0
  113. package/dist/src/service/fire-and-forget.d.ts +54 -0
  114. package/dist/src/service/fire-and-forget.js +38 -0
  115. package/dist/src/service/organize.d.ts +184 -0
  116. package/dist/src/service/organize.js +384 -0
  117. package/dist/src/service/refusal.d.ts +4 -0
  118. package/dist/src/service/refusal.js +20 -0
  119. package/dist/src/service/sender-category.d.ts +51 -0
  120. package/{src/service/sender-category.ts → dist/src/service/sender-category.js} +40 -81
  121. package/dist/src/service/sqs.d.ts +1 -0
  122. package/{src/service/sqs.ts → dist/src/service/sqs.js} +0 -1
  123. package/dist/src/service/trigger-sync.d.ts +51 -0
  124. package/dist/src/service/trigger-sync.js +39 -0
  125. package/dist/src/types.d.ts +49 -0
  126. package/dist/src/types.js +1 -0
  127. package/package.json +113 -98
  128. package/.env.test +0 -4
  129. package/dev-server/content-auth.test.ts +0 -113
  130. package/dev-server/content-auth.ts +0 -54
  131. package/dev-server/content-handler.test.ts +0 -105
  132. package/dev-server/content-handler.ts +0 -79
  133. package/dev-server/content-path.test.ts +0 -37
  134. package/dev-server/content-path.ts +0 -27
  135. package/dev-server/cors.test.ts +0 -48
  136. package/dev-server/cors.ts +0 -25
  137. package/dev-server/keep-alive.test.ts +0 -24
  138. package/dev-server/lambda-helpers.ts +0 -69
  139. package/dev-server/log-output.test.ts +0 -173
  140. package/dev-server/oauth-callback-route.test.ts +0 -66
  141. package/dev-server/relational-health.test.ts +0 -83
  142. package/dev-server/relational-health.ts +0 -39
  143. package/dev-server/server.ts +0 -384
  144. package/dev-server/sync-age.test.ts +0 -91
  145. package/dev-server/sync-age.ts +0 -55
  146. package/dev-server/upload-handler.ts +0 -132
  147. package/dev-server/upload-route.test.ts +0 -196
  148. package/scripts/backfill-classification.ts +0 -91
  149. package/scripts/backfill-list-id.ts +0 -83
  150. package/scripts/config-save.ts +0 -128
  151. package/src/auth.test.ts +0 -12
  152. package/src/auth.ts +0 -92
  153. package/src/config/msoauth.test.ts +0 -48
  154. package/src/config/msoauth.ts +0 -48
  155. package/src/data-backend.test.ts +0 -23
  156. package/src/derive/autoMoved.test.ts +0 -104
  157. package/src/derive/contentSignature.test.ts +0 -161
  158. package/src/derive/contentUrl.test.ts +0 -172
  159. package/src/derive/contentUrl.ts +0 -86
  160. package/src/derive/deriveMuted.test.ts +0 -34
  161. package/src/derive/enrichThreadRows.test.ts +0 -360
  162. package/src/derive/enrichThreadRows.ts +0 -234
  163. package/src/derive/filterThreadCriteria.test.ts +0 -126
  164. package/src/derive/filterThreadCriteria.ts +0 -51
  165. package/src/derive/senderTrust.test.ts +0 -67
  166. package/src/error.test.ts +0 -201
  167. package/src/error.ts +0 -123
  168. package/src/handlers/account-guards.test.ts +0 -67
  169. package/src/handlers/account-guards.ts +0 -169
  170. package/src/handlers/account-oauth-callback.test.ts +0 -155
  171. package/src/handlers/account-oauth-consent.test.ts +0 -336
  172. package/src/handlers/account-oauth-start.test.ts +0 -191
  173. package/src/handlers/account-oauth.test.ts +0 -439
  174. package/src/handlers/account-oauth.ts +0 -635
  175. package/src/handlers/account-overrides.test.ts +0 -88
  176. package/src/handlers/account-overrides.ts +0 -323
  177. package/src/handlers/account-ownership.ts +0 -25
  178. package/src/handlers/account-signature.ts +0 -117
  179. package/src/handlers/account-synced-services.test.ts +0 -245
  180. package/src/handlers/account.ts +0 -552
  181. package/src/handlers/address.test.ts +0 -381
  182. package/src/handlers/address.ts +0 -200
  183. package/src/handlers/calendar-event.test.ts +0 -685
  184. package/src/handlers/calendar-event.ts +0 -738
  185. package/src/handlers/calendar-feed-validators.test.ts +0 -228
  186. package/src/handlers/calendar-feed.test.ts +0 -439
  187. package/src/handlers/calendar-feed.ts +0 -291
  188. package/src/handlers/calendar-sqlite-fixture.ts +0 -36
  189. package/src/handlers/calendar-suggestion.test.ts +0 -1269
  190. package/src/handlers/calendar-suggestion.ts +0 -425
  191. package/src/handlers/calendar.test.ts +0 -1080
  192. package/src/handlers/calendar.ts +0 -517
  193. package/src/handlers/config.test.ts +0 -245
  194. package/src/handlers/config.ts +0 -332
  195. package/src/handlers/ensure-account-config.ts +0 -30
  196. package/src/handlers/filter.test.ts +0 -644
  197. package/src/handlers/filter.ts +0 -518
  198. package/src/handlers/folder-role-appointments.test.ts +0 -438
  199. package/src/handlers/folder-role-appointments.ts +0 -249
  200. package/src/handlers/folder-role-labels.ts +0 -63
  201. package/src/handlers/folder-role.test.ts +0 -209
  202. package/src/handlers/folder-role.ts +0 -80
  203. package/src/handlers/index.ts +0 -83
  204. package/src/handlers/label.test.ts +0 -112
  205. package/src/handlers/label.ts +0 -210
  206. package/src/handlers/mailbox-bindings.ts +0 -111
  207. package/src/handlers/mailbox-rename-refusal.test.ts +0 -247
  208. package/src/handlers/mailbox-settled-target.test.ts +0 -251
  209. package/src/handlers/mailbox.test.ts +0 -280
  210. package/src/handlers/mailbox.ts +0 -581
  211. package/src/handlers/me.test.ts +0 -81
  212. package/src/handlers/me.ts +0 -203
  213. package/src/handlers/message-read-backfill.test.ts +0 -103
  214. package/src/handlers/message.test.ts +0 -131
  215. package/src/handlers/message.ts +0 -1131
  216. package/src/handlers/organize.test.ts +0 -277
  217. package/src/handlers/organize.ts +0 -197
  218. package/src/handlers/outbox-attachment.test.ts +0 -765
  219. package/src/handlers/outbox-attachment.ts +0 -126
  220. package/src/handlers/outbox-list.test.ts +0 -179
  221. package/src/handlers/outbox.test.ts +0 -772
  222. package/src/handlers/outbox.ts +0 -258
  223. package/src/handlers/search.test.ts +0 -161
  224. package/src/handlers/search.ts +0 -105
  225. package/src/handlers/sync-progress.test.ts +0 -205
  226. package/src/handlers/sync-progress.ts +0 -99
  227. package/src/handlers/sync.test.ts +0 -168
  228. package/src/handlers/sync.ts +0 -118
  229. package/src/handlers/system-update.test.ts +0 -497
  230. package/src/handlers/system-update.ts +0 -230
  231. package/src/handlers/thread.test.ts +0 -396
  232. package/src/handlers/thread.ts +0 -427
  233. package/src/handlers/unified-threads-mail-sources.sqlite.test.ts +0 -197
  234. package/src/handlers/unified-threads.test.ts +0 -785
  235. package/src/handlers/unified-threads.ts +0 -669
  236. package/src/index.test.ts +0 -83
  237. package/src/index.ts +0 -228
  238. package/src/jwt-auth.test.ts +0 -152
  239. package/src/jwt-auth.ts +0 -132
  240. package/src/log-redaction.test.ts +0 -144
  241. package/src/openapi-spec.test.ts +0 -118
  242. package/src/request-context.test.ts +0 -61
  243. package/src/request-context.ts +0 -33
  244. package/src/request-pipeline.test.ts +0 -88
  245. package/src/request.ts +0 -10
  246. package/src/response.test.ts +0 -130
  247. package/src/response.ts +0 -144
  248. package/src/service/compose-sqlite.ts +0 -98
  249. package/src/service/config-import.ts +0 -39
  250. package/src/service/create-remit-client.ts +0 -521
  251. package/src/service/data-client.test.ts +0 -97
  252. package/src/service/data-client.ts +0 -58
  253. package/src/service/export-identity.ts +0 -39
  254. package/src/service/filter.ts +0 -40
  255. package/src/service/fire-and-forget.test.ts +0 -126
  256. package/src/service/fire-and-forget.ts +0 -79
  257. package/src/service/localhost-env-config.test.ts +0 -36
  258. package/src/service/organize-queue.ts +0 -98
  259. package/src/service/organize.test.ts +0 -1451
  260. package/src/service/organize.ts +0 -1025
  261. package/src/service/read-path-filter-config.test.ts +0 -243
  262. package/src/service/semantic-capability.test.ts +0 -78
  263. package/src/service/semantic-capability.ts +0 -95
  264. package/src/service/sender-category.test.ts +0 -235
  265. package/src/service/trigger-sync.test.ts +0 -205
  266. package/src/service/trigger-sync.ts +0 -100
  267. package/src/stdout-capture-fixture.ts +0 -40
  268. package/src/types.ts +0 -271
  269. package/tsconfig.json +0 -7
@@ -1,517 +0,0 @@
1
- import type {
2
- CalendarResponse,
3
- CreateCalendarInput,
4
- UpdateCalendarInput,
5
- } from "@remit/api-openapi-types";
6
- import {
7
- type CalendarFeedFetcher,
8
- DEFAULT_CALENDAR_URL_SEGMENT,
9
- isResolvableZone,
10
- provisionDefaultCalendar,
11
- putCalendarObject,
12
- readCalendarFeed,
13
- readSubscriptionUrl,
14
- writeCalendarFeed,
15
- } from "@remit/calendar-service";
16
- import type {
17
- CalendarCollectionItem,
18
- ICalendarCollectionRepository,
19
- ICalendarEventIndexRepository,
20
- ICalendarFeedTokenRepository,
21
- ICalendarObjectRepository,
22
- ICalendarUnitOfWork,
23
- UpdateCalendarCollectionInput,
24
- } from "@remit/data-ports";
25
- import { normalizeCalendarUrlSegment } from "@remit/data-ports/id";
26
- import { CalendarSource } from "@remit/domain-enums";
27
- import type { APIGatewayProxyEvent } from "aws-lambda";
28
- import { getAccountConfigIdFromEvent } from "../auth.js";
29
- import { defaultErrorCode } from "../error.js";
30
- import type { RemitClient } from "../service/data-client.js";
31
- import { getClient } from "../service/data-client.js";
32
- import type {
33
- CalendarCollectionDetailOperationIds,
34
- CalendarDetailOperationIds,
35
- CalendarOperationIds,
36
- OperationHandler,
37
- } from "../types.js";
38
-
39
- /** The calendar store, as the handlers reach it. */
40
- export interface CalendarDeps {
41
- calendarCollection: ICalendarCollectionRepository;
42
- calendarObject: ICalendarObjectRepository;
43
- calendarEventIndex: ICalendarEventIndexRepository;
44
- calendarFeedToken: ICalendarFeedTokenRepository;
45
- calendarUnitOfWork: ICalendarUnitOfWork;
46
- }
47
-
48
- /** Why a calendar request was refused, in a form a client can branch on. */
49
- export interface CalendarRefusal {
50
- code: string;
51
- message: string;
52
- }
53
-
54
- /**
55
- * A refusal is a value here, not a throw. Every one of these is something the
56
- * caller sent — a window that runs backwards, a segment already taken, a
57
- * RECURRENCE-ID naming no occurrence — so it is an ordinary outcome of the
58
- * request rather than a fault of the server's.
59
- */
60
- export type CalendarOutcome<T> =
61
- | { ok: true; value: T }
62
- | { ok: false; error: CalendarRefusal };
63
-
64
- export const refuseCalendar = <T>(
65
- code: string,
66
- message: string,
67
- ): CalendarOutcome<T> => ({ ok: false, error: { code, message } });
68
-
69
- /**
70
- * `CalendarRefusal.code` is this module's and `@remit/calendar-service`'s own
71
- * routing token, in PascalCase; the API's `code` vocabulary is snake_case
72
- * (issue #371). Derived rather than mapped by hand, so a refusal added to
73
- * either producer reaches the wire under its own name instead of silently
74
- * collapsing into the status-class code.
75
- */
76
- const wireCode = (code: string): string =>
77
- code.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
78
-
79
- /**
80
- * A 400 keeps the refusal's own code — `unknown_time_zone` and
81
- * `url_segment_taken` are what a client branches on, and the status class alone
82
- * would not distinguish them. A 404 and a 412 have nothing to add to their
83
- * status, so they take the shared token.
84
- */
85
- export const badRequest = (error: CalendarRefusal) => ({
86
- statusCode: 400,
87
- body: { code: wireCode(error.code), message: error.message },
88
- });
89
-
90
- export const notFound = (message: string) => ({
91
- statusCode: 404,
92
- body: { code: defaultErrorCode(404), message },
93
- });
94
-
95
- export const preconditionFailed = (message: string) => ({
96
- statusCode: 412,
97
- body: { code: defaultErrorCode(412), message },
98
- });
99
-
100
- /** The calendar half of the client, named so a handler takes only what it uses. */
101
- export const calendarDepsOf = (client: RemitClient): CalendarDeps => ({
102
- calendarCollection: client.calendarCollection,
103
- calendarObject: client.calendarObject,
104
- calendarEventIndex: client.calendarEventIndex,
105
- calendarFeedToken: client.calendarFeedToken,
106
- calendarUnitOfWork: client.calendarUnitOfWork,
107
- });
108
-
109
- const toCalendarResponse = (
110
- item: CalendarCollectionItem,
111
- ): CalendarResponse => ({
112
- calendarId: item.calendarId,
113
- accountConfigId: item.accountConfigId,
114
- urlSegment: item.urlSegment,
115
- displayName: item.displayName,
116
- color: item.color,
117
- componentSet: item.componentSet,
118
- source: item.source,
119
- timezone: item.timezone,
120
- syncSequence: item.syncSequence,
121
- subscriptionUrl: item.subscriptionUrl,
122
- subscriptionEnabled: item.subscriptionEnabled,
123
- subscriptionCheckedAt: item.subscriptionCheckedAt,
124
- subscriptionFetchedAt: item.subscriptionFetchedAt,
125
- subscriptionError: item.subscriptionError,
126
- createdAt: item.createdAt,
127
- updatedAt: item.updatedAt,
128
- });
129
-
130
- /**
131
- * Every collection the account config holds, provisioning the default one when
132
- * it holds none.
133
- *
134
- * Safe to race with itself: `calendarId` is derived from the account config and
135
- * the URL segment, so two first reads arriving together write the same row
136
- * rather than two, and the loser's write is a no-op instead of a second
137
- * calendar nobody asked for.
138
- */
139
- export const listCalendarsFor = async (
140
- deps: CalendarDeps,
141
- accountConfigId: string,
142
- ): Promise<CalendarCollectionItem[]> => {
143
- const existing =
144
- await deps.calendarCollection.listByAccountConfig(accountConfigId);
145
- if (existing.length > 0) return existing;
146
-
147
- await provisionDefaultCalendar(deps.calendarUnitOfWork, accountConfigId);
148
- return deps.calendarCollection.listByAccountConfig(accountConfigId);
149
- };
150
-
151
- /** One of the caller's collections, or a refusal naming the one they asked for. */
152
- export const findCalendarFor = async (
153
- deps: CalendarDeps,
154
- accountConfigId: string,
155
- calendarId: string,
156
- ): Promise<CalendarOutcome<CalendarCollectionItem>> => {
157
- const collections =
158
- await deps.calendarCollection.listByAccountConfig(accountConfigId);
159
- const found = collections.find(
160
- (collection) => collection.calendarId === calendarId,
161
- );
162
- if (!found) {
163
- return refuseCalendar(
164
- "NotFound",
165
- `no calendar ${calendarId} on this account`,
166
- );
167
- }
168
- return { ok: true, value: found };
169
- };
170
-
171
- /**
172
- * One of the caller's collections that a person may write events into. A
173
- * subscribed collection holds what its feed holds: an edit made here would be
174
- * overwritten by the next refresh, so it is refused rather than accepted and
175
- * silently lost.
176
- */
177
- export const findWritableCalendarFor = async (
178
- deps: CalendarDeps,
179
- accountConfigId: string,
180
- calendarId: string,
181
- ): Promise<CalendarOutcome<CalendarCollectionItem>> => {
182
- const found = await findCalendarFor(deps, accountConfigId, calendarId);
183
- if (!found.ok) return found;
184
- if (found.value.source === CalendarSource.Subscribed) {
185
- return refuseCalendar(
186
- "ReadOnlyCalendar",
187
- `"${found.value.displayName}" is subscribed to a feed and is read-only — change the event where the feed comes from`,
188
- );
189
- }
190
- return found;
191
- };
192
-
193
- /**
194
- * A collection's timezone is what every floating time in it is read in, so a
195
- * name this server cannot resolve is not a cosmetic setting — it silently moves
196
- * every all-day and unzoned event in the calendar. A Windows zone name, which
197
- * is what a client that has not normalised its input sends, is refused here
198
- * rather than stored and quietly read as UTC.
199
- */
200
- export const readCollectionTimezone = (
201
- timezone: string | undefined,
202
- ): CalendarOutcome<string> => {
203
- if (timezone === undefined || timezone === "") return { ok: true, value: "" };
204
- if (!isResolvableZone(timezone)) {
205
- return refuseCalendar(
206
- "UnknownTimeZone",
207
- `"${timezone}" is not a time zone this server can resolve — use an IANA name such as "Europe/Amsterdam"`,
208
- );
209
- }
210
- return { ok: true, value: timezone };
211
- };
212
-
213
- /**
214
- * Creates a collection subscribed to a feed. The feed is read before anything
215
- * is written, so an address that cannot be fetched leaves no empty calendar
216
- * behind; its events then land through the one write path, in the same unit as
217
- * the collection itself.
218
- */
219
- const createSubscribedCalendarFor = async (
220
- deps: CalendarDeps,
221
- accountConfigId: string,
222
- input: CreateCalendarInput & { subscriptionUrl: string },
223
- collection: { urlSegment: string; timezone: string },
224
- fetcher: CalendarFeedFetcher,
225
- now: number,
226
- ): Promise<CalendarOutcome<CalendarCollectionItem>> => {
227
- const url = readSubscriptionUrl(input.subscriptionUrl);
228
- if (!url.ok) return url;
229
-
230
- const feed = await readCalendarFeed(url.value, fetcher);
231
- if (!feed.ok) {
232
- return refuseCalendar(
233
- "SubscriptionUnreachable",
234
- `the calendar was not created: ${feed.reason}`,
235
- );
236
- }
237
-
238
- const created = await deps.calendarUnitOfWork.transaction(async (repos) => {
239
- const made = await repos.calendarCollection.createExclusive({
240
- accountConfigId,
241
- urlSegment: collection.urlSegment,
242
- displayName: input.displayName,
243
- color: input.color,
244
- timezone: collection.timezone,
245
- source: CalendarSource.Subscribed,
246
- subscriptionUrl: url.value,
247
- subscriptionEnabled: true,
248
- });
249
- if (!made) return null;
250
- await writeCalendarFeed(
251
- deps.calendarUnitOfWork,
252
- { accountConfigId, calendarId: made.calendarId },
253
- feed.value,
254
- now,
255
- );
256
- return repos.calendarCollection.get(accountConfigId, made.calendarId);
257
- });
258
- if (!created) {
259
- return refuseCalendar(
260
- "UrlSegmentTaken",
261
- `"${collection.urlSegment}" already addresses a calendar on this account — pick another`,
262
- );
263
- }
264
- return { ok: true, value: created };
265
- };
266
-
267
- export const createCalendarFor = async (
268
- deps: CalendarDeps,
269
- accountConfigId: string,
270
- input: CreateCalendarInput,
271
- fetcher: CalendarFeedFetcher = fetch,
272
- now: number = Date.now(),
273
- ): Promise<CalendarOutcome<CalendarCollectionItem>> => {
274
- const urlSegment = normalizeCalendarUrlSegment(input.urlSegment);
275
- if (urlSegment === "") {
276
- return refuseCalendar(
277
- "InvalidUrlSegment",
278
- "a calendar needs a url segment to be addressed by",
279
- );
280
- }
281
-
282
- const timezone = readCollectionTimezone(input.timezone);
283
- if (!timezone.ok) return timezone;
284
-
285
- const { subscriptionUrl } = input;
286
- if (subscriptionUrl !== undefined) {
287
- return createSubscribedCalendarFor(
288
- deps,
289
- accountConfigId,
290
- { ...input, subscriptionUrl },
291
- { urlSegment, timezone: timezone.value },
292
- fetcher,
293
- now,
294
- );
295
- }
296
-
297
- // The write decides, not a prior read: two creates of one segment arriving
298
- // together would both find it free, and the loser would silently be handed
299
- // the winner's calendar to write into.
300
- const created = await deps.calendarUnitOfWork.transaction((repos) =>
301
- repos.calendarCollection.createExclusive({
302
- accountConfigId,
303
- urlSegment,
304
- displayName: input.displayName,
305
- color: input.color,
306
- timezone: timezone.value,
307
- source: CalendarSource.UserCreated,
308
- }),
309
- );
310
- if (!created) {
311
- return refuseCalendar(
312
- "UrlSegmentTaken",
313
- `"${urlSegment}" already addresses a calendar on this account — pick another`,
314
- );
315
- }
316
- return { ok: true, value: created };
317
- };
318
-
319
- /**
320
- * Removes a collection with everything in it, in one unit.
321
- *
322
- * The default collection stays: it is where an accepted invitation and a first
323
- * event land, and an account config with no calendar has nowhere to put one.
324
- */
325
- export const deleteCalendarFor = async (
326
- deps: CalendarDeps,
327
- accountConfigId: string,
328
- calendarId: string,
329
- ): Promise<CalendarOutcome<null>> => {
330
- const collection = await findCalendarFor(deps, accountConfigId, calendarId);
331
- if (!collection.ok) return collection;
332
- if (collection.value.source === CalendarSource.Default) {
333
- return refuseCalendar(
334
- "DefaultCalendarUndeletable",
335
- `"${DEFAULT_CALENDAR_URL_SEGMENT}" is the calendar this account files events into and cannot be removed`,
336
- );
337
- }
338
-
339
- await deps.calendarUnitOfWork.transaction(async (repos) => {
340
- const objects = await repos.calendarObject.listByCalendar(calendarId);
341
- for (const object of objects) {
342
- await repos.calendarEventIndex.deleteForObject(
343
- calendarId,
344
- object.calendarObjectId,
345
- );
346
- await repos.calendarObject.delete(calendarId, object.calendarObjectId);
347
- }
348
- await repos.calendarFeedToken.delete(accountConfigId, calendarId);
349
- await repos.calendarCollection.delete(accountConfigId, calendarId);
350
- });
351
- return { ok: true, value: null };
352
- };
353
-
354
- /**
355
- * Reduce a PATCH body to the fields a collection update may set. `urlSegment`
356
- * is deliberately absent: it is the collection's identity and the path a client
357
- * has bookmarked, so moving it is making a different calendar.
358
- */
359
- export const pickCalendarUpdate = (
360
- body: Partial<UpdateCalendarInput>,
361
- ): UpdateCalendarCollectionInput => {
362
- const patch: UpdateCalendarCollectionInput = {};
363
- if (Object.hasOwn(body, "displayName")) patch.displayName = body.displayName;
364
- if (Object.hasOwn(body, "color")) patch.color = body.color;
365
- if (Object.hasOwn(body, "timezone")) patch.timezone = body.timezone;
366
- if (Object.hasOwn(body, "subscriptionEnabled")) {
367
- patch.subscriptionEnabled = body.subscriptionEnabled;
368
- }
369
- return patch;
370
- };
371
-
372
- /**
373
- * Applies a collection patch, rewriting what the collection's timezone decides.
374
- *
375
- * The timezone is not a label. Every floating and all-day time in the
376
- * collection is read in it, so changing it moves every occurrence row those
377
- * resources produced — and a row left at the old zone is an event drawn hours
378
- * from where the calendar now says it is. Each resource is therefore written
379
- * again from its own stored bytes, which re-projects and re-expands it and
380
- * bumps the collection's sequence so a syncing client sees the change. The
381
- * whole set is one unit: a half-converted calendar is worse than either zone.
382
- */
383
- export const updateCalendarFor = async (
384
- deps: CalendarDeps,
385
- accountConfigId: string,
386
- calendarId: string,
387
- body: Partial<UpdateCalendarInput>,
388
- ): Promise<CalendarOutcome<CalendarCollectionItem>> => {
389
- const current = await findCalendarFor(deps, accountConfigId, calendarId);
390
- if (!current.ok) return current;
391
-
392
- const patch = pickCalendarUpdate(body);
393
- if (
394
- patch.subscriptionEnabled !== undefined &&
395
- current.value.source !== CalendarSource.Subscribed
396
- ) {
397
- return refuseCalendar(
398
- "NotSubscribed",
399
- `"${current.value.displayName}" is not subscribed to a feed, so there is no refresh to turn on or off`,
400
- );
401
- }
402
- if (patch.timezone !== undefined) {
403
- const timezone = readCollectionTimezone(patch.timezone);
404
- if (!timezone.ok) return timezone;
405
- patch.timezone = timezone.value;
406
- }
407
- const rezone =
408
- patch.timezone !== undefined && patch.timezone !== current.value.timezone;
409
-
410
- const updated = await deps.calendarUnitOfWork.transaction(async (repos) => {
411
- const collection = await repos.calendarCollection.update(
412
- accountConfigId,
413
- calendarId,
414
- patch,
415
- );
416
- if (!rezone) return collection;
417
-
418
- const objects = await repos.calendarObject.listByCalendar(calendarId);
419
- for (const object of objects) {
420
- const rewritten = await putCalendarObject(deps.calendarUnitOfWork, {
421
- accountConfigId,
422
- calendarId,
423
- resourceName: object.resourceName,
424
- icalData: object.icalData,
425
- });
426
- if (!rewritten.ok) {
427
- throw new Error(
428
- `stored calendar object ${object.calendarObjectId} was refused on re-expansion: ${rewritten.error.code}`,
429
- );
430
- }
431
- }
432
- return repos.calendarCollection.get(accountConfigId, calendarId);
433
- });
434
- return { ok: true, value: updated };
435
- };
436
-
437
- export const CalendarOperations: Record<
438
- CalendarOperationIds,
439
- OperationHandler<CalendarOperationIds>
440
- > = {
441
- CalendarOperations_listCalendars: async (_context, ...args: unknown[]) => {
442
- const event = args[0] as APIGatewayProxyEvent;
443
- const accountConfigId = getAccountConfigIdFromEvent(event);
444
- const deps = calendarDepsOf(await getClient());
445
- const items = await listCalendarsFor(deps, accountConfigId);
446
- return { items: items.map(toCalendarResponse) };
447
- },
448
-
449
- CalendarOperations_createCalendar: async (context, ...args: unknown[]) => {
450
- const event = args[0] as APIGatewayProxyEvent;
451
- const accountConfigId = getAccountConfigIdFromEvent(event);
452
- const input = context.request.requestBody as CreateCalendarInput;
453
- const deps = calendarDepsOf(await getClient());
454
-
455
- const created = await createCalendarFor(deps, accountConfigId, input);
456
- if (!created.ok) return badRequest(created.error);
457
- return toCalendarResponse(created.value);
458
- },
459
- };
460
-
461
- export const CalendarDetailOperations: Record<
462
- CalendarCollectionDetailOperationIds,
463
- OperationHandler<CalendarDetailOperationIds>
464
- > = {
465
- CalendarDetailOperations_getCalendar: async (context, ...args: unknown[]) => {
466
- const event = args[0] as APIGatewayProxyEvent;
467
- const accountConfigId = getAccountConfigIdFromEvent(event);
468
- const { calendarId } = context.request.params as { calendarId: string };
469
- const deps = calendarDepsOf(await getClient());
470
-
471
- const found = await findCalendarFor(deps, accountConfigId, calendarId);
472
- if (!found.ok) return notFound(found.error.message);
473
- return toCalendarResponse(found.value);
474
- },
475
-
476
- CalendarDetailOperations_updateCalendar: async (
477
- context,
478
- ...args: unknown[]
479
- ) => {
480
- const event = args[0] as APIGatewayProxyEvent;
481
- const accountConfigId = getAccountConfigIdFromEvent(event);
482
- const { calendarId } = context.request.params as { calendarId: string };
483
- const body = context.request.requestBody as Partial<UpdateCalendarInput>;
484
- const deps = calendarDepsOf(await getClient());
485
-
486
- const updated = await updateCalendarFor(
487
- deps,
488
- accountConfigId,
489
- calendarId,
490
- body,
491
- );
492
- if (!updated.ok) {
493
- return updated.error.code === "NotFound"
494
- ? notFound(updated.error.message)
495
- : badRequest(updated.error);
496
- }
497
- return toCalendarResponse(updated.value);
498
- },
499
-
500
- CalendarDetailOperations_deleteCalendar: async (
501
- context,
502
- ...args: unknown[]
503
- ) => {
504
- const event = args[0] as APIGatewayProxyEvent;
505
- const accountConfigId = getAccountConfigIdFromEvent(event);
506
- const { calendarId } = context.request.params as { calendarId: string };
507
- const deps = calendarDepsOf(await getClient());
508
-
509
- const removed = await deleteCalendarFor(deps, accountConfigId, calendarId);
510
- if (!removed.ok) {
511
- return removed.error.code === "NotFound"
512
- ? notFound(removed.error.message)
513
- : badRequest(removed.error);
514
- }
515
- return { statusCode: 204 };
516
- },
517
- };