@remit/backend 0.0.143 → 0.0.145

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 (279) 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 +5 -0
  4. package/dist/src/api.js +73 -0
  5. package/dist/src/auth.d.ts +24 -0
  6. package/dist/src/auth.js +81 -0
  7. package/dist/src/config/api-config.d.ts +20 -0
  8. package/dist/src/config/api-config.js +1 -0
  9. package/dist/src/config/from-env.d.ts +2 -0
  10. package/dist/src/config/from-env.js +65 -0
  11. package/dist/src/config/msoauth-secrets-manager.d.ts +2 -0
  12. package/dist/src/config/msoauth-secrets-manager.js +28 -0
  13. package/dist/src/config/msoauth.d.ts +19 -0
  14. package/dist/src/config/msoauth.js +6 -0
  15. package/{src/derive/autoMoved.ts → dist/src/derive/autoMoved.d.ts} +1 -29
  16. package/dist/src/derive/autoMoved.js +49 -0
  17. package/dist/src/derive/contentSignature.d.ts +40 -0
  18. package/{src/derive/contentSignature.ts → dist/src/derive/contentSignature.js} +23 -77
  19. package/dist/src/derive/contentUrl.d.ts +33 -0
  20. package/dist/src/derive/contentUrl.js +27 -0
  21. package/{src/derive/deriveMuted.ts → dist/src/derive/deriveMuted.d.ts} +1 -3
  22. package/dist/src/derive/deriveMuted.js +7 -0
  23. package/dist/src/derive/enrichThreadRows.d.ts +118 -0
  24. package/dist/src/derive/enrichThreadRows.js +151 -0
  25. package/dist/src/derive/filterThreadCriteria.d.ts +23 -0
  26. package/dist/src/derive/filterThreadCriteria.js +24 -0
  27. package/{src/derive/pendingMoveCounts.ts → dist/src/derive/pendingMoveCounts.d.ts} +5 -53
  28. package/dist/src/derive/pendingMoveCounts.js +53 -0
  29. package/{src/derive/senderTrust.ts → dist/src/derive/senderTrust.d.ts} +1 -9
  30. package/dist/src/derive/senderTrust.js +19 -0
  31. package/dist/src/error.d.ts +21 -0
  32. package/dist/src/error.js +85 -0
  33. package/dist/src/handlers/account-guards.d.ts +36 -0
  34. package/dist/src/handlers/account-guards.js +107 -0
  35. package/dist/src/handlers/account-oauth.d.ts +15 -0
  36. package/dist/src/handlers/account-oauth.js +334 -0
  37. package/dist/src/handlers/account-overrides.d.ts +53 -0
  38. package/dist/src/handlers/account-overrides.js +233 -0
  39. package/dist/src/handlers/account-signature.d.ts +28 -0
  40. package/dist/src/handlers/account-signature.js +85 -0
  41. package/dist/src/handlers/account.d.ts +7 -0
  42. package/dist/src/handlers/account.js +337 -0
  43. package/dist/src/handlers/address.d.ts +35 -0
  44. package/dist/src/handlers/address.js +133 -0
  45. package/dist/src/handlers/calendar-event.d.ts +70 -0
  46. package/dist/src/handlers/calendar-event.js +417 -0
  47. package/dist/src/handlers/calendar-feed.d.ts +47 -0
  48. package/dist/src/handlers/calendar-feed.js +184 -0
  49. package/dist/src/handlers/calendar-suggestion.d.ts +47 -0
  50. package/dist/src/handlers/calendar-suggestion.js +204 -0
  51. package/dist/src/handlers/calendar.d.ts +116 -0
  52. package/dist/src/handlers/calendar.js +323 -0
  53. package/dist/src/handlers/config.d.ts +43 -0
  54. package/dist/src/handlers/config.js +164 -0
  55. package/dist/src/handlers/ensure-account-config.d.ts +9 -0
  56. package/dist/src/handlers/ensure-account-config.js +27 -0
  57. package/dist/src/handlers/filter.d.ts +3 -0
  58. package/dist/src/handlers/filter.js +88 -0
  59. package/dist/src/handlers/folder-role-appointments.d.ts +68 -0
  60. package/dist/src/handlers/folder-role-appointments.js +168 -0
  61. package/dist/src/handlers/folder-role-labels.d.ts +26 -0
  62. package/dist/src/handlers/folder-role-labels.js +40 -0
  63. package/dist/src/handlers/folder-role.d.ts +9 -0
  64. package/dist/src/handlers/folder-role.js +37 -0
  65. package/dist/src/handlers/index.d.ts +88 -0
  66. package/dist/src/handlers/index.js +62 -0
  67. package/dist/src/handlers/label.d.ts +43 -0
  68. package/dist/src/handlers/label.js +107 -0
  69. package/dist/src/handlers/mailbox-bindings.d.ts +41 -0
  70. package/dist/src/handlers/mailbox-bindings.js +61 -0
  71. package/dist/src/handlers/mailbox.d.ts +23 -0
  72. package/dist/src/handlers/mailbox.js +211 -0
  73. package/dist/src/handlers/me.d.ts +2 -0
  74. package/dist/src/handlers/me.js +117 -0
  75. package/dist/src/handlers/message.d.ts +82 -0
  76. package/dist/src/handlers/message.js +595 -0
  77. package/dist/src/handlers/organize.d.ts +10 -0
  78. package/dist/src/handlers/organize.js +103 -0
  79. package/dist/src/handlers/outbox-attachment.d.ts +25 -0
  80. package/dist/src/handlers/outbox-attachment.js +77 -0
  81. package/dist/src/handlers/outbox.d.ts +4 -0
  82. package/dist/src/handlers/outbox.js +136 -0
  83. package/dist/src/handlers/sync-progress.d.ts +46 -0
  84. package/dist/src/handlers/sync-progress.js +63 -0
  85. package/dist/src/handlers/sync.d.ts +28 -0
  86. package/dist/src/handlers/sync.js +73 -0
  87. package/dist/src/handlers/system-update.d.ts +2 -0
  88. package/dist/src/handlers/system-update.js +159 -0
  89. package/dist/src/handlers/thread.d.ts +148 -0
  90. package/dist/src/handlers/thread.js +228 -0
  91. package/dist/src/handlers/unified-threads.d.ts +247 -0
  92. package/dist/src/handlers/unified-threads.js +406 -0
  93. package/{src/handlers/vip-suggestions.ts → dist/src/handlers/vip-suggestions.d.ts} +1 -13
  94. package/dist/src/handlers/vip-suggestions.js +16 -0
  95. package/dist/src/index.d.ts +5 -0
  96. package/dist/src/index.js +53 -0
  97. package/{src/json.ts → dist/src/json.d.ts} +1 -4
  98. package/dist/src/json.js +10 -0
  99. package/dist/src/jwt-auth.d.ts +15 -0
  100. package/dist/src/jwt-auth.js +45 -0
  101. package/dist/src/lambda.d.ts +13 -0
  102. package/dist/src/lambda.js +33 -0
  103. package/dist/src/request.d.ts +9 -0
  104. package/dist/src/request.js +17 -0
  105. package/dist/src/response.d.ts +44 -0
  106. package/dist/src/response.js +80 -0
  107. package/dist/src/serve.d.ts +31 -0
  108. package/dist/src/serve.js +117 -0
  109. package/dist/src/service/account-deletion.d.ts +20 -0
  110. package/dist/src/service/account-deletion.js +33 -0
  111. package/dist/src/service/compose-sqlite.d.ts +3 -0
  112. package/dist/src/service/compose-sqlite.js +13 -0
  113. package/dist/src/service/create-remit-client.d.ts +109 -0
  114. package/dist/src/service/create-remit-client.js +188 -0
  115. package/dist/src/service/data-client.d.ts +12 -0
  116. package/dist/src/service/data-client.js +42 -0
  117. package/dist/src/service/export-identity.d.ts +4 -0
  118. package/dist/src/service/export-identity.js +33 -0
  119. package/dist/src/service/fire-and-forget.d.ts +54 -0
  120. package/dist/src/service/fire-and-forget.js +37 -0
  121. package/dist/src/service/organize.d.ts +181 -0
  122. package/dist/src/service/organize.js +367 -0
  123. package/dist/src/service/queues.d.ts +12 -0
  124. package/dist/src/service/queues.js +1 -0
  125. package/dist/src/service/refusal.d.ts +4 -0
  126. package/dist/src/service/refusal.js +20 -0
  127. package/dist/src/service/sender-category.d.ts +51 -0
  128. package/{src/service/sender-category.ts → dist/src/service/sender-category.js} +40 -81
  129. package/dist/src/service/shared-deps.d.ts +6 -0
  130. package/dist/src/service/shared-deps.js +24 -0
  131. package/dist/src/service/trigger-sync.d.ts +57 -0
  132. package/dist/src/service/trigger-sync.js +31 -0
  133. package/dist/src/types.d.ts +72 -0
  134. package/dist/src/types.js +1 -0
  135. package/package.json +125 -98
  136. package/.env.test +0 -4
  137. package/dev-server/content-auth.test.ts +0 -113
  138. package/dev-server/content-auth.ts +0 -54
  139. package/dev-server/content-handler.test.ts +0 -105
  140. package/dev-server/content-handler.ts +0 -79
  141. package/dev-server/content-path.test.ts +0 -37
  142. package/dev-server/content-path.ts +0 -27
  143. package/dev-server/cors.test.ts +0 -48
  144. package/dev-server/cors.ts +0 -25
  145. package/dev-server/keep-alive.test.ts +0 -24
  146. package/dev-server/lambda-helpers.ts +0 -69
  147. package/dev-server/log-output.test.ts +0 -173
  148. package/dev-server/oauth-callback-route.test.ts +0 -66
  149. package/dev-server/relational-health.test.ts +0 -83
  150. package/dev-server/relational-health.ts +0 -39
  151. package/dev-server/server.ts +0 -384
  152. package/dev-server/sync-age.test.ts +0 -91
  153. package/dev-server/sync-age.ts +0 -55
  154. package/dev-server/upload-handler.ts +0 -132
  155. package/dev-server/upload-route.test.ts +0 -196
  156. package/scripts/backfill-classification.ts +0 -91
  157. package/scripts/backfill-list-id.ts +0 -83
  158. package/scripts/config-save.ts +0 -128
  159. package/src/auth.test.ts +0 -12
  160. package/src/auth.ts +0 -92
  161. package/src/config/msoauth.test.ts +0 -48
  162. package/src/config/msoauth.ts +0 -48
  163. package/src/data-backend.test.ts +0 -23
  164. package/src/data-backend.ts +0 -14
  165. package/src/derive/autoMoved.test.ts +0 -104
  166. package/src/derive/contentSignature.test.ts +0 -161
  167. package/src/derive/contentUrl.test.ts +0 -172
  168. package/src/derive/contentUrl.ts +0 -86
  169. package/src/derive/deriveMuted.test.ts +0 -34
  170. package/src/derive/enrichThreadRows.test.ts +0 -360
  171. package/src/derive/enrichThreadRows.ts +0 -234
  172. package/src/derive/filterThreadCriteria.test.ts +0 -126
  173. package/src/derive/filterThreadCriteria.ts +0 -51
  174. package/src/derive/senderTrust.test.ts +0 -67
  175. package/src/error.test.ts +0 -201
  176. package/src/error.ts +0 -123
  177. package/src/handlers/account-guards.test.ts +0 -67
  178. package/src/handlers/account-guards.ts +0 -169
  179. package/src/handlers/account-oauth-callback.test.ts +0 -155
  180. package/src/handlers/account-oauth-consent.test.ts +0 -336
  181. package/src/handlers/account-oauth-start.test.ts +0 -191
  182. package/src/handlers/account-oauth.test.ts +0 -439
  183. package/src/handlers/account-oauth.ts +0 -635
  184. package/src/handlers/account-overrides.test.ts +0 -88
  185. package/src/handlers/account-overrides.ts +0 -323
  186. package/src/handlers/account-ownership.ts +0 -25
  187. package/src/handlers/account-signature.ts +0 -117
  188. package/src/handlers/account-synced-services.test.ts +0 -245
  189. package/src/handlers/account.ts +0 -552
  190. package/src/handlers/address.test.ts +0 -381
  191. package/src/handlers/address.ts +0 -200
  192. package/src/handlers/calendar-event.test.ts +0 -685
  193. package/src/handlers/calendar-event.ts +0 -738
  194. package/src/handlers/calendar-feed-validators.test.ts +0 -228
  195. package/src/handlers/calendar-feed.test.ts +0 -439
  196. package/src/handlers/calendar-feed.ts +0 -291
  197. package/src/handlers/calendar-sqlite-fixture.ts +0 -36
  198. package/src/handlers/calendar-suggestion.test.ts +0 -1269
  199. package/src/handlers/calendar-suggestion.ts +0 -425
  200. package/src/handlers/calendar.test.ts +0 -1080
  201. package/src/handlers/calendar.ts +0 -517
  202. package/src/handlers/config.test.ts +0 -245
  203. package/src/handlers/config.ts +0 -332
  204. package/src/handlers/ensure-account-config.ts +0 -30
  205. package/src/handlers/filter.test.ts +0 -644
  206. package/src/handlers/filter.ts +0 -518
  207. package/src/handlers/folder-role-appointments.test.ts +0 -438
  208. package/src/handlers/folder-role-appointments.ts +0 -249
  209. package/src/handlers/folder-role-labels.ts +0 -63
  210. package/src/handlers/folder-role.test.ts +0 -209
  211. package/src/handlers/folder-role.ts +0 -80
  212. package/src/handlers/index.ts +0 -83
  213. package/src/handlers/label.test.ts +0 -112
  214. package/src/handlers/label.ts +0 -210
  215. package/src/handlers/mailbox-bindings.ts +0 -111
  216. package/src/handlers/mailbox-rename-refusal.test.ts +0 -247
  217. package/src/handlers/mailbox-settled-target.test.ts +0 -251
  218. package/src/handlers/mailbox.test.ts +0 -280
  219. package/src/handlers/mailbox.ts +0 -581
  220. package/src/handlers/me.test.ts +0 -81
  221. package/src/handlers/me.ts +0 -203
  222. package/src/handlers/message-read-backfill.test.ts +0 -103
  223. package/src/handlers/message.test.ts +0 -131
  224. package/src/handlers/message.ts +0 -1131
  225. package/src/handlers/organize.test.ts +0 -277
  226. package/src/handlers/organize.ts +0 -197
  227. package/src/handlers/outbox-attachment.test.ts +0 -765
  228. package/src/handlers/outbox-attachment.ts +0 -126
  229. package/src/handlers/outbox-list.test.ts +0 -179
  230. package/src/handlers/outbox.test.ts +0 -772
  231. package/src/handlers/outbox.ts +0 -258
  232. package/src/handlers/search.test.ts +0 -161
  233. package/src/handlers/search.ts +0 -105
  234. package/src/handlers/sync-progress.test.ts +0 -205
  235. package/src/handlers/sync-progress.ts +0 -99
  236. package/src/handlers/sync.test.ts +0 -168
  237. package/src/handlers/sync.ts +0 -118
  238. package/src/handlers/system-update.test.ts +0 -497
  239. package/src/handlers/system-update.ts +0 -230
  240. package/src/handlers/thread.test.ts +0 -396
  241. package/src/handlers/thread.ts +0 -427
  242. package/src/handlers/unified-threads-mail-sources.sqlite.test.ts +0 -197
  243. package/src/handlers/unified-threads.test.ts +0 -785
  244. package/src/handlers/unified-threads.ts +0 -669
  245. package/src/index.test.ts +0 -83
  246. package/src/index.ts +0 -228
  247. package/src/jwt-auth.test.ts +0 -152
  248. package/src/jwt-auth.ts +0 -132
  249. package/src/log-redaction.test.ts +0 -144
  250. package/src/openapi-spec.test.ts +0 -118
  251. package/src/request-context.test.ts +0 -61
  252. package/src/request-context.ts +0 -33
  253. package/src/request-pipeline.test.ts +0 -88
  254. package/src/request.ts +0 -10
  255. package/src/response.test.ts +0 -130
  256. package/src/response.ts +0 -144
  257. package/src/service/compose-sqlite.ts +0 -98
  258. package/src/service/config-import.ts +0 -39
  259. package/src/service/create-remit-client.ts +0 -522
  260. package/src/service/data-client.test.ts +0 -97
  261. package/src/service/data-client.ts +0 -58
  262. package/src/service/export-identity.ts +0 -39
  263. package/src/service/filter.ts +0 -40
  264. package/src/service/fire-and-forget.test.ts +0 -126
  265. package/src/service/fire-and-forget.ts +0 -79
  266. package/src/service/localhost-env-config.test.ts +0 -36
  267. package/src/service/organize-queue.ts +0 -98
  268. package/src/service/organize.test.ts +0 -1451
  269. package/src/service/organize.ts +0 -1025
  270. package/src/service/read-path-filter-config.test.ts +0 -243
  271. package/src/service/semantic-capability.test.ts +0 -78
  272. package/src/service/semantic-capability.ts +0 -95
  273. package/src/service/sender-category.test.ts +0 -235
  274. package/src/service/sqs.ts +0 -4
  275. package/src/service/trigger-sync.test.ts +0 -205
  276. package/src/service/trigger-sync.ts +0 -100
  277. package/src/stdout-capture-fixture.ts +0 -40
  278. package/src/types.ts +0 -271
  279. package/tsconfig.json +0 -7
@@ -1,381 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { afterEach, describe, it, mock } from "node:test";
3
- import type { SendMessageCommand } from "@aws-sdk/client-sqs";
4
- import type {
5
- AddressResponse,
6
- UpdateAddressInput,
7
- } from "@remit/api-openapi-types";
8
- import type {
9
- AddressItem,
10
- FlagsMergePatch,
11
- ResultList,
12
- } from "@remit/data-ports";
13
- import type { APIGatewayProxyEvent } from "aws-lambda";
14
- import type { Context } from "openapi-backend";
15
- import { deriveAccountConfigId } from "../auth.js";
16
- import {
17
- _resetForTest,
18
- type RemitClient,
19
- setClient,
20
- } from "../service/data-client.js";
21
- import { sqsClient } from "../service/sqs.js";
22
- import { AddressDetailOperations, AddressOperations } from "./address.js";
23
-
24
- const searchAddresses =
25
- AddressOperations.AddressOperations_searchAddresses as unknown as (
26
- context: Context,
27
- event: APIGatewayProxyEvent,
28
- ) => Promise<ResultList<AddressItem>>;
29
-
30
- const updateAddress =
31
- AddressDetailOperations.AddressDetailOperations_updateAddress as unknown as (
32
- context: Context,
33
- event: APIGatewayProxyEvent,
34
- ) => Promise<AddressResponse>;
35
-
36
- const SUB = "cognito-sub-704";
37
- const ACCOUNT_CONFIG_ID = deriveAccountConfigId(SUB);
38
-
39
- const eventFor = (sub: string): APIGatewayProxyEvent =>
40
- ({
41
- requestContext: { authorizer: { claims: { sub } } },
42
- }) as unknown as APIGatewayProxyEvent;
43
-
44
- const contextFor = (query: { q: string; limit?: number }): Context =>
45
- ({ request: { query } }) as unknown as Context;
46
-
47
- const address = (over: Partial<AddressItem>): AddressItem =>
48
- ({
49
- addressId: "addr-1",
50
- accountConfigId: ACCOUNT_CONFIG_ID,
51
- localPart: "amsterdam",
52
- domain: "pocahondas.nl",
53
- normalizedEmail: "amsterdam@pocahondas.nl",
54
- normalizedCompound: "pocahondas locatie amsterdam amsterdam@pocahondas.nl",
55
- displayName: "Pocahondas locatie amsterdam",
56
- flags: {},
57
- inboundCount: 4,
58
- outboundCount: 1,
59
- replyCount: 2,
60
- lastInboundAt: 1_000,
61
- lastReplyAt: 900,
62
- createdAt: 100,
63
- updatedAt: 200,
64
- ...over,
65
- }) as AddressItem;
66
-
67
- interface Listing {
68
- accountConfigId: string;
69
- search?: string;
70
- limit?: number;
71
- }
72
-
73
- const clientReturning = (items: AddressItem[], seen: Listing[]): RemitClient =>
74
- ({
75
- address: {
76
- listByAccountConfig: async (
77
- input: Listing,
78
- ): Promise<ResultList<AddressItem>> => {
79
- seen.push(input);
80
- return { items, continuationToken: undefined };
81
- },
82
- },
83
- }) as unknown as RemitClient;
84
-
85
- afterEach(() => {
86
- mock.restoreAll();
87
- _resetForTest();
88
- });
89
-
90
- describe("AddressOperations_searchAddresses", () => {
91
- it("looks a partial term up against the caller's own addresses (#704)", async () => {
92
- const seen: Listing[] = [];
93
- setClient(clientReturning([address({})], seen));
94
-
95
- const response = await searchAddresses(
96
- contextFor({ q: "Po", limit: 8 }),
97
- eventFor(SUB),
98
- );
99
-
100
- assert.deepEqual(seen, [
101
- { accountConfigId: ACCOUNT_CONFIG_ID, search: "po", limit: 8 },
102
- ]);
103
- assert.deepEqual(
104
- response.items.map((item) => item.normalizedEmail),
105
- ["amsterdam@pocahondas.nl"],
106
- );
107
- assert.equal(response.items[0].displayName, "Pocahondas locatie amsterdam");
108
- });
109
-
110
- it("hands the suggestion list back in the order it was ranked", async () => {
111
- const seen: Listing[] = [];
112
- setClient(
113
- clientReturning(
114
- [
115
- address({
116
- addressId: "addr-frequent",
117
- normalizedEmail: "zoe@pocahondas.nl",
118
- }),
119
- address({
120
- addressId: "addr-stranger",
121
- normalizedEmail: "aaron@pocahondas.nl",
122
- inboundCount: 0,
123
- replyCount: 0,
124
- }),
125
- ],
126
- seen,
127
- ),
128
- );
129
-
130
- const response = await searchAddresses(
131
- contextFor({ q: "po" }),
132
- eventFor(SUB),
133
- );
134
-
135
- assert.deepEqual(
136
- response.items.map((item) => item.addressId),
137
- ["addr-frequent", "addr-stranger"],
138
- );
139
- });
140
-
141
- it("asks for a suggestion-sized window when the caller names no limit", async () => {
142
- const seen: Listing[] = [];
143
- setClient(clientReturning([], seen));
144
-
145
- await searchAddresses(contextFor({ q: "po" }), eventFor(SUB));
146
-
147
- assert.equal(seen[0].limit, 10);
148
- });
149
- });
150
-
151
- const updateContextFor = (body: UpdateAddressInput): Context =>
152
- ({
153
- request: { params: { addressId: "addr-1" }, requestBody: body },
154
- }) as unknown as Context;
155
-
156
- /**
157
- * Fake data client that applies the same merge semantics the repo does:
158
- * a `null` in the patch deletes the key, anything else writes it.
159
- */
160
- const clientHolding = (
161
- stored: AddressItem,
162
- seen: FlagsMergePatch[],
163
- ): RemitClient =>
164
- ({
165
- address: {
166
- getAddress: async (): Promise<AddressItem> => stored,
167
- mergeFlags: async (
168
- _accountConfigId: string,
169
- _addressId: string,
170
- patch: FlagsMergePatch,
171
- ): Promise<AddressItem> => {
172
- seen.push(patch);
173
- const flags: Record<string, unknown> = { ...(stored.flags ?? {}) };
174
- for (const [key, value] of Object.entries(patch)) {
175
- if (value === undefined) continue;
176
- if (value === null) delete flags[key];
177
- else flags[key] = value;
178
- }
179
- return { ...stored, flags } as AddressItem;
180
- },
181
- },
182
- }) as unknown as RemitClient;
183
-
184
- /**
185
- * The never-spam grant (#605) travels the same route every other flag does. The
186
- * exclusivity against `blocked` is not asserted here: it lives in the repo's
187
- * merge fold, the only place that sees both keys at once.
188
- */
189
- describe("AddressDetailOperations_updateAddress never-spam (#605)", () => {
190
- it("forwards the grant to the merge patch and hands it back on the address", async () => {
191
- const seen: FlagsMergePatch[] = [];
192
- setClient(clientHolding(address({ flags: {} }), seen));
193
-
194
- const response = await updateAddress(
195
- updateContextFor({ flags: { neverSpam: { value: true, setAt: 20 } } }),
196
- eventFor(SUB),
197
- );
198
-
199
- assert.deepEqual(seen, [{ neverSpam: { value: true, setAt: 20 } }]);
200
- assert.deepEqual(response.flags.neverSpam, { value: true, setAt: 20 });
201
- });
202
-
203
- it("clears the grant by naming the key, the removal form every flag shares", async () => {
204
- const seen: FlagsMergePatch[] = [];
205
- setClient(
206
- clientHolding(
207
- address({ flags: { neverSpam: { value: true, setAt: 10 } } }),
208
- seen,
209
- ),
210
- );
211
-
212
- const response = await updateAddress(
213
- updateContextFor({ clearFlags: ["neverSpam"] }),
214
- eventFor(SUB),
215
- );
216
-
217
- assert.deepEqual(seen, [{ neverSpam: null }]);
218
- assert.equal(response.flags.neverSpam, undefined);
219
- });
220
- });
221
-
222
- describe("AddressDetailOperations_updateAddress removing a flag (#615)", () => {
223
- it("clears the category override, which has no false-equivalent value to send", async () => {
224
- const seen: FlagsMergePatch[] = [];
225
- setClient(
226
- clientHolding(
227
- address({
228
- flags: { category: { value: "newsletter", setAt: 10 } },
229
- }),
230
- seen,
231
- ),
232
- );
233
-
234
- const response = await updateAddress(
235
- updateContextFor({ clearFlags: ["category"] }),
236
- eventFor(SUB),
237
- );
238
-
239
- assert.deepEqual(seen, [{ category: null }]);
240
- assert.equal(response.flags.category, undefined);
241
- });
242
-
243
- it("clears a boolean flag by the same route, leaving the untouched ones alone", async () => {
244
- const seen: FlagsMergePatch[] = [];
245
- setClient(
246
- clientHolding(
247
- address({
248
- flags: {
249
- muted: { value: true, setAt: 10 },
250
- vip: { value: true, setAt: 11 },
251
- },
252
- }),
253
- seen,
254
- ),
255
- );
256
-
257
- const response = await updateAddress(
258
- updateContextFor({ clearFlags: ["muted"] }),
259
- eventFor(SUB),
260
- );
261
-
262
- assert.equal(response.flags.muted, undefined);
263
- assert.deepEqual(response.flags.vip, { value: true, setAt: 11 });
264
- });
265
-
266
- it("removes a key named in both halves, because clearFlags is applied last", async () => {
267
- const seen: FlagsMergePatch[] = [];
268
- setClient(
269
- clientHolding(
270
- address({ flags: { muted: { value: true, setAt: 10 } } }),
271
- seen,
272
- ),
273
- );
274
-
275
- const response = await updateAddress(
276
- updateContextFor({
277
- flags: { muted: { value: true, setAt: 20 } },
278
- clearFlags: ["muted"],
279
- }),
280
- eventFor(SUB),
281
- );
282
-
283
- assert.deepEqual(seen, [{ muted: null }]);
284
- assert.equal(response.flags.muted, undefined);
285
- });
286
- });
287
-
288
- /**
289
- * The retroactive half of a sender-category override (#415). Setting the flag
290
- * enqueues one back-apply carrying the category it was set to; clearing it —
291
- * the revert-to-auto route — enqueues nothing, because the classifier's own
292
- * answer for mail already filed cannot be re-derived without every body.
293
- */
294
- describe("AddressDetailOperations_updateAddress category back-apply (#415)", () => {
295
- const withStubbedQueue = (enqueued: SendMessageCommand[]): void => {
296
- process.env.SQS_QUEUE_URL_ACCOUNT_FANOUT =
297
- "http://localhost:9324/queue/account-fanout-test";
298
- mock.method(sqsClient, "send", async (command: SendMessageCommand) => {
299
- enqueued.push(command);
300
- return {};
301
- });
302
- };
303
-
304
- it("enqueues a back-apply naming the sender and the set it was fired for", async () => {
305
- const enqueued: SendMessageCommand[] = [];
306
- withStubbedQueue(enqueued);
307
- setClient(clientHolding(address({ flags: {} }), []));
308
-
309
- await updateAddress(
310
- updateContextFor({
311
- flags: { category: { value: "newsletter", setAt: 20 } },
312
- }),
313
- eventFor(SUB),
314
- );
315
-
316
- assert.equal(enqueued.length, 1);
317
- assert.deepEqual(JSON.parse(String(enqueued[0]?.input.MessageBody)), {
318
- type: "SenderCategoryBackApply",
319
- accountConfigId: ACCOUNT_CONFIG_ID,
320
- addressId: "addr-1",
321
- normalizedEmail: "amsterdam@pocahondas.nl",
322
- category: "newsletter",
323
- categorySetAt: 20,
324
- });
325
- });
326
-
327
- it("says the override is saved and names the retry when the queue refuses", async () => {
328
- process.env.SQS_QUEUE_URL_ACCOUNT_FANOUT =
329
- "http://localhost:9324/queue/account-fanout-test";
330
- mock.method(sqsClient, "send", async () => {
331
- throw new Error("AWS.SimpleQueueService.NonExistentQueue");
332
- });
333
- setClient(clientHolding(address({ flags: {} }), []));
334
-
335
- await assert.rejects(
336
- updateAddress(
337
- updateContextFor({
338
- flags: { category: { value: "newsletter", setAt: 20 } },
339
- }),
340
- eventFor(SUB),
341
- ),
342
- (error: Error) => {
343
- assert.match(error.message, /saved/);
344
- assert.match(error.message, /same category again/);
345
- return true;
346
- },
347
- );
348
- });
349
-
350
- it("enqueues nothing when the override is cleared back to auto", async () => {
351
- const enqueued: SendMessageCommand[] = [];
352
- withStubbedQueue(enqueued);
353
- setClient(
354
- clientHolding(
355
- address({ flags: { category: { value: "newsletter", setAt: 10 } } }),
356
- [],
357
- ),
358
- );
359
-
360
- const response = await updateAddress(
361
- updateContextFor({ clearFlags: ["category"] }),
362
- eventFor(SUB),
363
- );
364
-
365
- assert.equal(response.flags.category, undefined);
366
- assert.deepEqual(enqueued, []);
367
- });
368
-
369
- it("enqueues nothing for a flag that has no bearing on classification", async () => {
370
- const enqueued: SendMessageCommand[] = [];
371
- withStubbedQueue(enqueued);
372
- setClient(clientHolding(address({ flags: {} }), []));
373
-
374
- await updateAddress(
375
- updateContextFor({ flags: { muted: { value: true, setAt: 20 } } }),
376
- eventFor(SUB),
377
- );
378
-
379
- assert.deepEqual(enqueued, []);
380
- });
381
- });
@@ -1,200 +0,0 @@
1
- import { SendMessageCommand } from "@aws-sdk/client-sqs";
2
- import type {
3
- AddressResponse,
4
- UpdateAddressInput,
5
- } from "@remit/api-openapi-types";
6
- import type {
7
- AddressFlags,
8
- AddressItem,
9
- FlagsMergePatch,
10
- } from "@remit/data-ports";
11
- import { ForbiddenError } from "@remit/data-ports/errors";
12
- import { AddressFlagKey } from "@remit/domain-enums";
13
- import type { APIGatewayProxyEvent } from "aws-lambda";
14
- import { env } from "expect-env";
15
- import type { Context } from "openapi-backend";
16
- import { getAccountConfigIdFromEvent } from "../auth.js";
17
- import { getClient } from "../service/data-client.js";
18
- import { sqsClient } from "../service/sqs.js";
19
- import type {
20
- AddressDetailOperationIds,
21
- AddressOperationIds,
22
- OperationHandler,
23
- } from "../types.js";
24
-
25
- export const toAddressResponse = (item: AddressItem): AddressResponse => ({
26
- addressId: item.addressId,
27
- accountConfigId: item.accountConfigId,
28
- displayName: item.displayName,
29
- localPart: item.localPart,
30
- domain: item.domain,
31
- normalizedEmail: item.normalizedEmail,
32
- flags: item.flags ?? {},
33
- inboundCount: item.inboundCount ?? 0,
34
- outboundCount: item.outboundCount ?? 0,
35
- replyCount: item.replyCount ?? 0,
36
- lastInboundAt: item.lastInboundAt ?? 0,
37
- lastReplyAt: item.lastReplyAt ?? 0,
38
- createdAt: item.createdAt,
39
- updatedAt: item.updatedAt,
40
- });
41
-
42
- type FlagKey = (typeof AddressFlagKey)[keyof typeof AddressFlagKey];
43
-
44
- const FLAG_KEYS = Object.values(AddressFlagKey) as readonly FlagKey[];
45
-
46
- /**
47
- * Translate the wire-format `UpdateAddressInput` into a service-level
48
- * `FlagsMergePatch`. Only known flag keys are forwarded; unknown keys are
49
- * silently dropped (a TypeSpec-only schema means unknown keys are a client
50
- * bug, not a security risk).
51
- *
52
- * `clearFlags` is the removal form for every flag whatever its value type, and
53
- * is applied after `flags`, so a key named in both ends up removed. The `null`
54
- * alternative on a flag key stays honoured for callers that can express it,
55
- * but it is unreachable over OAS 3.0: TypeSpec emits a nullable `$ref` as
56
- * `allOf`, and the request validator drops the `nullable` there.
57
- */
58
- export const buildFlagsPatch = (
59
- input: UpdateAddressInput | undefined,
60
- ): FlagsMergePatch => {
61
- if (!input) return {};
62
- const patch = {} as Record<FlagKey, unknown>;
63
- const flags = input.flags;
64
- if (flags) {
65
- for (const key of FLAG_KEYS) {
66
- if (!(key in flags)) continue;
67
- const value = (flags as Record<FlagKey, unknown>)[key];
68
- if (value === null) {
69
- patch[key] = null;
70
- continue;
71
- }
72
- if (value === undefined) continue;
73
- patch[key] = value;
74
- }
75
- }
76
- for (const key of input.clearFlags ?? []) {
77
- if (!FLAG_KEYS.includes(key)) continue;
78
- patch[key] = null;
79
- }
80
- return patch as FlagsMergePatch;
81
- };
82
-
83
- /**
84
- * The category flag a back-apply should be enqueued for, or `undefined` when
85
- * this patch asks for none (issue #415).
86
- *
87
- * A SET fires one; a CLEAR — `null` in the patch, whether it came from
88
- * `clearFlags` or from the nullable flag value — never does. Reverting a sender
89
- * to auto-classification says nothing about what the classifier would have
90
- * answered on mail already filed, and re-deriving that would need every
91
- * message's body back; the revert takes effect on the sender's next message,
92
- * exactly as the override itself did before this.
93
- *
94
- * The whole flag, not just its value: `setAt` identifies WHICH set the job was
95
- * fired for, and the worker refuses to apply a job whose set is no longer the
96
- * one standing on the Address.
97
- */
98
- export const backApplyCategoryFlag = (
99
- patch: FlagsMergePatch,
100
- ): NonNullable<AddressFlags["category"]> | undefined => {
101
- const flag = patch.category;
102
- if (flag === null || flag === undefined) return undefined;
103
- return flag;
104
- };
105
-
106
- export const AddressOperations: Record<
107
- AddressOperationIds,
108
- OperationHandler<AddressOperationIds>
109
- > = {
110
- AddressOperations_searchAddresses: async (
111
- context: Context,
112
- ...args: unknown[]
113
- ) => {
114
- const event = args[0] as APIGatewayProxyEvent;
115
- const accountConfigId = getAccountConfigIdFromEvent(event);
116
- const { q, limit } = context.request.query as {
117
- q: string;
118
- limit?: number;
119
- };
120
-
121
- const client = await getClient();
122
-
123
- const result = await client.address.listByAccountConfig({
124
- accountConfigId,
125
- search: q.toLowerCase(),
126
- limit: limit ?? 10,
127
- });
128
-
129
- return {
130
- items: result.items.map(toAddressResponse),
131
- continuationToken: result.continuationToken,
132
- };
133
- },
134
- };
135
-
136
- export const AddressDetailOperations: Record<
137
- AddressDetailOperationIds,
138
- OperationHandler<AddressDetailOperationIds>
139
- > = {
140
- AddressDetailOperations_updateAddress: async (
141
- context: Context,
142
- ...args: unknown[]
143
- ) => {
144
- const event = args[0] as APIGatewayProxyEvent;
145
- const accountConfigId = getAccountConfigIdFromEvent(event);
146
- const { addressId } = context.request.params as { addressId: string };
147
- const body = (context.request.requestBody ?? {}) as UpdateAddressInput;
148
-
149
- const client = await getClient();
150
-
151
- // Authorize: address must belong to the caller's accountConfig
152
- const existing = await client.address.getAddress(
153
- accountConfigId,
154
- addressId,
155
- );
156
- if (existing.accountConfigId !== accountConfigId) {
157
- throw new ForbiddenError(`Address ${addressId} not in account config`);
158
- }
159
-
160
- const patch = buildFlagsPatch(body);
161
- if (Object.keys(patch).length === 0) {
162
- return toAddressResponse(existing);
163
- }
164
-
165
- const updated = await client.address.mergeFlags(
166
- accountConfigId,
167
- addressId,
168
- patch,
169
- );
170
-
171
- const flag = backApplyCategoryFlag(patch);
172
- if (flag) {
173
- // The flag itself is already durable. A queue failure here costs the
174
- // user the retroactive pass and nothing else, so the 500 says so and
175
- // names the retry — re-sending the same category enqueues a fresh job.
176
- await sqsClient
177
- .send(
178
- new SendMessageCommand({
179
- QueueUrl: env.SQS_QUEUE_URL_ACCOUNT_FANOUT,
180
- MessageBody: JSON.stringify({
181
- type: "SenderCategoryBackApply",
182
- accountConfigId,
183
- addressId,
184
- normalizedEmail: updated.normalizedEmail,
185
- category: flag.value,
186
- categorySetAt: flag.setAt,
187
- }),
188
- }),
189
- )
190
- .catch((cause: unknown) => {
191
- throw new Error(
192
- `The ${flag.value} override is saved and applies to this sender's next message, but the pass over their existing mail could not be started. Setting the same category again retries it.`,
193
- { cause },
194
- );
195
- });
196
- }
197
-
198
- return toAddressResponse(updated);
199
- },
200
- };