@remit/backend 0.0.1

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 (87) hide show
  1. package/.env.test +7 -0
  2. package/dev-server/content-auth.test.ts +113 -0
  3. package/dev-server/content-auth.ts +54 -0
  4. package/dev-server/content-handler.test.ts +105 -0
  5. package/dev-server/content-handler.ts +79 -0
  6. package/dev-server/content-path.test.ts +37 -0
  7. package/dev-server/content-path.ts +27 -0
  8. package/dev-server/cors.test.ts +48 -0
  9. package/dev-server/cors.ts +25 -0
  10. package/dev-server/lambda-helpers.ts +69 -0
  11. package/dev-server/server.ts +289 -0
  12. package/package.json +82 -0
  13. package/src/auth.ts +92 -0
  14. package/src/config/msoauth.ts +60 -0
  15. package/src/data-backend.test.ts +25 -0
  16. package/src/data-backend.ts +20 -0
  17. package/src/derive/autoMoved.ts +28 -0
  18. package/src/derive/contentSignature.test.ts +153 -0
  19. package/src/derive/contentSignature.ts +139 -0
  20. package/src/derive/contentUrl.test.ts +156 -0
  21. package/src/derive/contentUrl.ts +70 -0
  22. package/src/derive/enrichThreadRows.ts +155 -0
  23. package/src/derive/filterThreadCriteria.test.ts +119 -0
  24. package/src/derive/filterThreadCriteria.ts +60 -0
  25. package/src/derive/pendingMoveCounts.ts +90 -0
  26. package/src/derive/senderTrust.test.ts +67 -0
  27. package/src/derive/senderTrust.ts +23 -0
  28. package/src/error.ts +55 -0
  29. package/src/handlers/account-guards.ts +137 -0
  30. package/src/handlers/account-oauth.test.ts +383 -0
  31. package/src/handlers/account-oauth.ts +452 -0
  32. package/src/handlers/account-overrides.test.ts +69 -0
  33. package/src/handlers/account-overrides.ts +286 -0
  34. package/src/handlers/account-ownership.ts +25 -0
  35. package/src/handlers/account-signature.ts +129 -0
  36. package/src/handlers/account.ts +524 -0
  37. package/src/handlers/address.ts +136 -0
  38. package/src/handlers/config.test.ts +184 -0
  39. package/src/handlers/config.ts +219 -0
  40. package/src/handlers/ensure-account-config.ts +30 -0
  41. package/src/handlers/filter.ts +294 -0
  42. package/src/handlers/folder-role-appointments.test.ts +250 -0
  43. package/src/handlers/folder-role-appointments.ts +272 -0
  44. package/src/handlers/folder-role.ts +76 -0
  45. package/src/handlers/index.ts +48 -0
  46. package/src/handlers/mailbox.ts +411 -0
  47. package/src/handlers/me.ts +190 -0
  48. package/src/handlers/message.ts +886 -0
  49. package/src/handlers/organize.test.ts +43 -0
  50. package/src/handlers/organize.ts +196 -0
  51. package/src/handlers/outbox.ts +218 -0
  52. package/src/handlers/search.test.ts +182 -0
  53. package/src/handlers/search.ts +105 -0
  54. package/src/handlers/sync-progress.test.ts +158 -0
  55. package/src/handlers/sync-progress.ts +64 -0
  56. package/src/handlers/sync.test.ts +146 -0
  57. package/src/handlers/sync.ts +119 -0
  58. package/src/handlers/thread.ts +361 -0
  59. package/src/handlers/unified-threads.ts +196 -0
  60. package/src/handlers/vip-suggestions.ts +21 -0
  61. package/src/index.ts +170 -0
  62. package/src/json.ts +11 -0
  63. package/src/jwt-auth.test.ts +89 -0
  64. package/src/jwt-auth.ts +95 -0
  65. package/src/request-context.test.ts +61 -0
  66. package/src/request-context.ts +29 -0
  67. package/src/request.ts +10 -0
  68. package/src/response.test.ts +107 -0
  69. package/src/response.ts +80 -0
  70. package/src/service/compose-postgres.ts +72 -0
  71. package/src/service/compose-sqlite.ts +80 -0
  72. package/src/service/create-remit-client.ts +358 -0
  73. package/src/service/dynamodb.test.ts +100 -0
  74. package/src/service/dynamodb.ts +58 -0
  75. package/src/service/filter.ts +55 -0
  76. package/src/service/fire-and-forget.test.ts +126 -0
  77. package/src/service/fire-and-forget.ts +79 -0
  78. package/src/service/localhost-env-config.test.ts +40 -0
  79. package/src/service/organize.test.ts +384 -0
  80. package/src/service/organize.ts +354 -0
  81. package/src/service/semantic-capability.test.ts +64 -0
  82. package/src/service/semantic-capability.ts +62 -0
  83. package/src/service/sqs.ts +4 -0
  84. package/src/service/trigger-sync.test.ts +211 -0
  85. package/src/service/trigger-sync.ts +102 -0
  86. package/src/types.ts +161 -0
  87. package/tsconfig.json +7 -0
@@ -0,0 +1,286 @@
1
+ import type { MutedFlag } from "@remit/api-openapi-types";
2
+ import type {
3
+ AccountSettingItem,
4
+ IAccountSettingRepository,
5
+ } from "@remit/data-ports";
6
+ import {
7
+ baseSettingName,
8
+ composeSettingName,
9
+ SETTING_NAME_SEPARATOR,
10
+ } from "@remit/data-ports/account-settings";
11
+
12
+ /**
13
+ * RFC 032 Tier 1: the per-account display name and mute flag, and the
14
+ * per-mailbox display-name override and mute flag, live in AccountSetting
15
+ * rows — not on the Account/Mailbox entities. They are stored as composite-named
16
+ * rows keyed by the target id (`AccountDisplayName#<accountId>`,
17
+ * `MailboxMuted#<mailboxId>`, …), so one `listByAccountConfig` query yields every
18
+ * target's overrides; readers group them by the `#<targetId>` suffix. This mirrors
19
+ * the signature helper (`account-signature.ts`). The per-folder role override
20
+ * this module used to carry is superseded by the per-account role map — see
21
+ * `folder-role-appointments.ts` (RFC 032 exclusive-folder-appointment, #976).
22
+ */
23
+
24
+ const ACCOUNT_OVERRIDE_NAMES = {
25
+ displayName: "AccountDisplayName",
26
+ muted: "AccountMuted",
27
+ } as const;
28
+
29
+ const MAILBOX_OVERRIDE_NAMES = {
30
+ displayName: "MailboxDisplayName",
31
+ muted: "MailboxMuted",
32
+ } as const;
33
+
34
+ export interface AccountOverrides {
35
+ displayName?: string;
36
+ muted?: MutedFlag;
37
+ }
38
+
39
+ export interface MailboxOverrides {
40
+ displayNameOverride?: string;
41
+ muted?: MutedFlag;
42
+ }
43
+
44
+ const stringValueOf = (item: AccountSettingItem): string | undefined => {
45
+ const { value } = item;
46
+ if (value.kind === "String") return value.value;
47
+ return undefined;
48
+ };
49
+
50
+ const mutedValueOf = (item: AccountSettingItem): MutedFlag | undefined => {
51
+ const { value } = item;
52
+ if (value.kind === "MutedFlag") return value.value;
53
+ return undefined;
54
+ };
55
+
56
+ const targetIdOf = (name: string): string | undefined => {
57
+ const idx = name.indexOf(SETTING_NAME_SEPARATOR);
58
+ if (idx === -1) return undefined;
59
+ return name.slice(idx + SETTING_NAME_SEPARATOR.length) || undefined;
60
+ };
61
+
62
+ // ============================================
63
+ // Account overrides (displayName + muted)
64
+ // ============================================
65
+
66
+ export const groupAccountOverrides = (
67
+ settings: AccountSettingItem[],
68
+ ): Map<string, AccountOverrides> => {
69
+ const byAccount = new Map<string, AccountOverrides>();
70
+ for (const setting of settings) {
71
+ const accountId = targetIdOf(setting.name);
72
+ if (!accountId) continue;
73
+ const base = baseSettingName(setting.name);
74
+ if (base === ACCOUNT_OVERRIDE_NAMES.displayName) {
75
+ const displayName = stringValueOf(setting);
76
+ if (displayName === undefined) continue;
77
+ const current = byAccount.get(accountId) ?? {};
78
+ current.displayName = displayName;
79
+ byAccount.set(accountId, current);
80
+ continue;
81
+ }
82
+ if (base === ACCOUNT_OVERRIDE_NAMES.muted) {
83
+ const muted = mutedValueOf(setting);
84
+ if (muted === undefined) continue;
85
+ const current = byAccount.get(accountId) ?? {};
86
+ current.muted = muted;
87
+ byAccount.set(accountId, current);
88
+ }
89
+ }
90
+ return byAccount;
91
+ };
92
+
93
+ /**
94
+ * Load every account's display-name/mute overrides for an account configuration
95
+ * in one query, grouped by accountId. Callers reading multiple accounts
96
+ * (GET /config) use this once.
97
+ */
98
+ export const loadAccountOverridesForConfig = async (
99
+ accountSetting: IAccountSettingRepository,
100
+ accountConfigId: string,
101
+ ): Promise<Map<string, AccountOverrides>> => {
102
+ const settings = await accountSetting.listByAccountConfig(accountConfigId);
103
+ return groupAccountOverrides(settings);
104
+ };
105
+
106
+ /**
107
+ * Resolve one account's display-name/mute overrides by reading just its two
108
+ * composite rows. Used by the account create/update handlers.
109
+ */
110
+ export const loadAccountOverrides = async (
111
+ accountSetting: IAccountSettingRepository,
112
+ accountConfigId: string,
113
+ accountId: string,
114
+ ): Promise<AccountOverrides> => {
115
+ const [displayName, muted] = await Promise.all([
116
+ accountSetting.get(
117
+ accountConfigId,
118
+ composeSettingName(ACCOUNT_OVERRIDE_NAMES.displayName, accountId),
119
+ ),
120
+ accountSetting.get(
121
+ accountConfigId,
122
+ composeSettingName(ACCOUNT_OVERRIDE_NAMES.muted, accountId),
123
+ ),
124
+ ]);
125
+ const overrides: AccountOverrides = {};
126
+ const displayNameValue = displayName ? stringValueOf(displayName) : undefined;
127
+ const mutedValue = muted ? mutedValueOf(muted) : undefined;
128
+ if (displayNameValue !== undefined) overrides.displayName = displayNameValue;
129
+ if (mutedValue !== undefined) overrides.muted = mutedValue;
130
+ return overrides;
131
+ };
132
+
133
+ export const upsertAccountDisplayName = (
134
+ accountSetting: IAccountSettingRepository,
135
+ accountConfigId: string,
136
+ accountId: string,
137
+ displayName: string,
138
+ ): Promise<unknown> =>
139
+ accountSetting.upsert({
140
+ accountConfigId,
141
+ name: composeSettingName(ACCOUNT_OVERRIDE_NAMES.displayName, accountId),
142
+ value: { kind: "String", value: displayName },
143
+ });
144
+
145
+ /**
146
+ * Set or clear the account mute flag. A `MutedFlag` upserts the row; `null`
147
+ * deletes it (RFC 032: absence is "unset"). Mirrors the address-flag
148
+ * null→remove, object→set semantics.
149
+ */
150
+ export const writeAccountMuted = (
151
+ accountSetting: IAccountSettingRepository,
152
+ accountConfigId: string,
153
+ accountId: string,
154
+ muted: MutedFlag | null,
155
+ ): Promise<unknown> => {
156
+ const name = composeSettingName(ACCOUNT_OVERRIDE_NAMES.muted, accountId);
157
+ if (muted === null) {
158
+ return accountSetting.delete(accountConfigId, name);
159
+ }
160
+ return accountSetting.upsert({
161
+ accountConfigId,
162
+ name,
163
+ value: { kind: "MutedFlag", value: muted },
164
+ });
165
+ };
166
+
167
+ // ============================================
168
+ // Mailbox overrides (displayNameOverride + muted)
169
+ // ============================================
170
+
171
+ export const groupMailboxOverrides = (
172
+ settings: AccountSettingItem[],
173
+ ): Map<string, MailboxOverrides> => {
174
+ const byMailbox = new Map<string, MailboxOverrides>();
175
+ for (const setting of settings) {
176
+ const mailboxId = targetIdOf(setting.name);
177
+ if (!mailboxId) continue;
178
+ const base = baseSettingName(setting.name);
179
+ if (base === MAILBOX_OVERRIDE_NAMES.displayName) {
180
+ const displayName = stringValueOf(setting);
181
+ if (displayName === undefined) continue;
182
+ const current = byMailbox.get(mailboxId) ?? {};
183
+ current.displayNameOverride = displayName;
184
+ byMailbox.set(mailboxId, current);
185
+ continue;
186
+ }
187
+ if (base === MAILBOX_OVERRIDE_NAMES.muted) {
188
+ const muted = mutedValueOf(setting);
189
+ if (muted === undefined) continue;
190
+ const current = byMailbox.get(mailboxId) ?? {};
191
+ current.muted = muted;
192
+ byMailbox.set(mailboxId, current);
193
+ }
194
+ }
195
+ return byMailbox;
196
+ };
197
+
198
+ /**
199
+ * Load every mailbox's overrides for an account configuration in one query,
200
+ * grouped by mailboxId. listMailboxes uses this once to avoid an N+1 per mailbox.
201
+ */
202
+ export const loadMailboxOverridesForConfig = async (
203
+ accountSetting: IAccountSettingRepository,
204
+ accountConfigId: string,
205
+ ): Promise<Map<string, MailboxOverrides>> => {
206
+ const settings = await accountSetting.listByAccountConfig(accountConfigId);
207
+ return groupMailboxOverrides(settings);
208
+ };
209
+
210
+ /**
211
+ * Resolve one mailbox's overrides by reading just its two composite rows.
212
+ * Used by getMailbox and the rename/PATCH handler.
213
+ */
214
+ export const loadMailboxOverrides = async (
215
+ accountSetting: IAccountSettingRepository,
216
+ accountConfigId: string,
217
+ mailboxId: string,
218
+ ): Promise<MailboxOverrides> => {
219
+ const [displayName, muted] = await Promise.all([
220
+ accountSetting.get(
221
+ accountConfigId,
222
+ composeSettingName(MAILBOX_OVERRIDE_NAMES.displayName, mailboxId),
223
+ ),
224
+ accountSetting.get(
225
+ accountConfigId,
226
+ composeSettingName(MAILBOX_OVERRIDE_NAMES.muted, mailboxId),
227
+ ),
228
+ ]);
229
+ const overrides: MailboxOverrides = {};
230
+ const displayNameValue = displayName ? stringValueOf(displayName) : undefined;
231
+ const mutedValue = muted ? mutedValueOf(muted) : undefined;
232
+ if (displayNameValue !== undefined)
233
+ overrides.displayNameOverride = displayNameValue;
234
+ if (mutedValue !== undefined) overrides.muted = mutedValue;
235
+ return overrides;
236
+ };
237
+
238
+ /**
239
+ * Apply a mailbox PATCH's override changes (displayNameOverride / muted) to
240
+ * AccountSetting rows. For each field: `null` deletes the row, a value upserts
241
+ * it, `undefined`/absent is a no-op — the same semantics the entity-backed
242
+ * version used. The canonical role a folder fills is written separately via
243
+ * `writeFolderRoleAppointment` (RFC 032 exclusive-folder-appointment, #976).
244
+ */
245
+ export const applyMailboxOverrideChanges = async (
246
+ accountSetting: Pick<IAccountSettingRepository, "upsert" | "delete">,
247
+ accountConfigId: string,
248
+ mailboxId: string,
249
+ changes: {
250
+ displayNameOverride?: string | null;
251
+ muted?: MutedFlag | null;
252
+ },
253
+ ): Promise<void> => {
254
+ const writes: Promise<unknown>[] = [];
255
+
256
+ if (changes.displayNameOverride !== undefined) {
257
+ const name = composeSettingName(
258
+ MAILBOX_OVERRIDE_NAMES.displayName,
259
+ mailboxId,
260
+ );
261
+ writes.push(
262
+ changes.displayNameOverride === null
263
+ ? accountSetting.delete(accountConfigId, name)
264
+ : accountSetting.upsert({
265
+ accountConfigId,
266
+ name,
267
+ value: { kind: "String", value: changes.displayNameOverride },
268
+ }),
269
+ );
270
+ }
271
+
272
+ if (changes.muted !== undefined) {
273
+ const name = composeSettingName(MAILBOX_OVERRIDE_NAMES.muted, mailboxId);
274
+ writes.push(
275
+ changes.muted === null
276
+ ? accountSetting.delete(accountConfigId, name)
277
+ : accountSetting.upsert({
278
+ accountConfigId,
279
+ name,
280
+ value: { kind: "MutedFlag", value: changes.muted },
281
+ }),
282
+ );
283
+ }
284
+
285
+ await Promise.all(writes);
286
+ };
@@ -0,0 +1,25 @@
1
+ import type { AccountItem } from "@remit/data-ports";
2
+ import { ForbiddenError, NotFoundError } from "@remit/data-ports/errors";
3
+
4
+ /**
5
+ * Cross-tenant ownership guard for accounts.
6
+ *
7
+ * `mode: "read"` throws NotFoundError on mismatch (404) so we don't leak the
8
+ * existence of another tenant's account on a GET. `mode: "act"` throws
9
+ * ForbiddenError on mismatch (403) for action verbs (PATCH/POST/DELETE) where
10
+ * the caller has already named the resource and the API contract says we
11
+ * explicitly deny rather than feign 404.
12
+ */
13
+ export const assertAccountOwnership = (
14
+ account: Pick<AccountItem, "accountId" | "accountConfigId">,
15
+ callerAccountConfigId: string,
16
+ mode: "read" | "act",
17
+ ): void => {
18
+ if (account.accountConfigId === callerAccountConfigId) return;
19
+ if (mode === "read") {
20
+ throw new NotFoundError(`Account not found: ${account.accountId}`);
21
+ }
22
+ throw new ForbiddenError(
23
+ `Account ${account.accountId} not in account config`,
24
+ );
25
+ };
@@ -0,0 +1,129 @@
1
+ import type {
2
+ AccountSettingItem,
3
+ IAccountSettingRepository,
4
+ } from "@remit/data-ports";
5
+ import {
6
+ baseSettingName,
7
+ composeSettingName,
8
+ SETTING_NAME_SEPARATOR,
9
+ } from "@remit/data-ports/account-settings";
10
+
11
+ export interface AccountSignature {
12
+ plainText?: string;
13
+ html?: string;
14
+ }
15
+
16
+ const SIGNATURE_NAMES = {
17
+ plainText: "AccountSignaturePlainText",
18
+ html: "AccountSignatureHtml",
19
+ } as const;
20
+
21
+ const stringValueOf = (item: AccountSettingItem): string | undefined => {
22
+ const { value } = item;
23
+ if (value.kind === "String") return value.value;
24
+ return undefined;
25
+ };
26
+
27
+ const accountIdOf = (name: string): string | undefined => {
28
+ const idx = name.indexOf(SETTING_NAME_SEPARATOR);
29
+ if (idx === -1) return undefined;
30
+ return name.slice(idx + SETTING_NAME_SEPARATOR.length) || undefined;
31
+ };
32
+
33
+ /**
34
+ * Build a per-account signature lookup from the whole settings set of one
35
+ * account configuration. Signatures are stored as composite-named rows
36
+ * (`AccountSignaturePlainText#<accountId>` / `AccountSignatureHtml#<accountId>`),
37
+ * so one `listByAccountConfig` query yields every account's signature; this
38
+ * groups them by the `#<accountId>` suffix.
39
+ */
40
+ export const groupSignaturesByAccount = (
41
+ settings: AccountSettingItem[],
42
+ ): Map<string, AccountSignature> => {
43
+ const byAccount = new Map<string, AccountSignature>();
44
+ for (const setting of settings) {
45
+ const accountId = accountIdOf(setting.name);
46
+ if (!accountId) continue;
47
+ const base = baseSettingName(setting.name);
48
+ const text = stringValueOf(setting);
49
+ if (text === undefined) continue;
50
+ const current = byAccount.get(accountId) ?? {};
51
+ if (base === SIGNATURE_NAMES.plainText) current.plainText = text;
52
+ if (base === SIGNATURE_NAMES.html) current.html = text;
53
+ byAccount.set(accountId, current);
54
+ }
55
+ return byAccount;
56
+ };
57
+
58
+ /**
59
+ * Load every signature in an account configuration in one query, grouped by
60
+ * account. Callers reading multiple accounts (GET /config) use this once.
61
+ */
62
+ export const loadSignaturesForConfig = async (
63
+ accountSetting: IAccountSettingRepository,
64
+ accountConfigId: string,
65
+ ): Promise<Map<string, AccountSignature>> => {
66
+ const settings = await accountSetting.listByAccountConfig(accountConfigId);
67
+ return groupSignaturesByAccount(settings);
68
+ };
69
+
70
+ /**
71
+ * Resolve the signature for a single account by reading just its two composite
72
+ * rows. Used by the account create/update handlers, which act on one account.
73
+ */
74
+ export const loadSignatureForAccount = async (
75
+ accountSetting: IAccountSettingRepository,
76
+ accountConfigId: string,
77
+ accountId: string,
78
+ ): Promise<AccountSignature> => {
79
+ const [plain, html] = await Promise.all([
80
+ accountSetting.get(
81
+ accountConfigId,
82
+ composeSettingName(SIGNATURE_NAMES.plainText, accountId),
83
+ ),
84
+ accountSetting.get(
85
+ accountConfigId,
86
+ composeSettingName(SIGNATURE_NAMES.html, accountId),
87
+ ),
88
+ ]);
89
+ const signature: AccountSignature = {};
90
+ const plainText = plain ? stringValueOf(plain) : undefined;
91
+ const htmlText = html ? stringValueOf(html) : undefined;
92
+ if (plainText !== undefined) signature.plainText = plainText;
93
+ if (htmlText !== undefined) signature.html = htmlText;
94
+ return signature;
95
+ };
96
+
97
+ /**
98
+ * Idempotent upsert of an account's signature fields. Only writes the parts the
99
+ * caller supplied — an empty string is a valid stored value (it clears the
100
+ * displayed signature without removing the row), matching the prior PATCH
101
+ * semantics where setting `""` stored `""`.
102
+ */
103
+ export const upsertAccountSignature = async (
104
+ accountSetting: IAccountSettingRepository,
105
+ accountConfigId: string,
106
+ accountId: string,
107
+ signature: { plainText?: string; html?: string },
108
+ ): Promise<void> => {
109
+ const writes: Promise<unknown>[] = [];
110
+ if (signature.plainText !== undefined) {
111
+ writes.push(
112
+ accountSetting.upsert({
113
+ accountConfigId,
114
+ name: composeSettingName(SIGNATURE_NAMES.plainText, accountId),
115
+ value: { kind: "String", value: signature.plainText },
116
+ }),
117
+ );
118
+ }
119
+ if (signature.html !== undefined) {
120
+ writes.push(
121
+ accountSetting.upsert({
122
+ accountConfigId,
123
+ name: composeSettingName(SIGNATURE_NAMES.html, accountId),
124
+ value: { kind: "String", value: signature.html },
125
+ }),
126
+ );
127
+ }
128
+ await Promise.all(writes);
129
+ };