@remit/mailbox-service 0.0.112 → 0.0.114

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 (270) hide show
  1. package/dist/account-credentials.d.ts +83 -0
  2. package/dist/account-credentials.js +104 -0
  3. package/dist/attribute-mapper.d.ts +36 -0
  4. package/dist/attribute-mapper.js +119 -0
  5. package/dist/body-parse.d.ts +28 -0
  6. package/{src/body-parse.ts → dist/body-parse.js} +18 -26
  7. package/dist/body-part-mapper.d.ts +92 -0
  8. package/dist/body-part-mapper.js +301 -0
  9. package/dist/body-sync-queue.d.ts +59 -0
  10. package/dist/body-sync-queue.js +56 -0
  11. package/dist/body-sync-terminal.d.ts +58 -0
  12. package/dist/body-sync-terminal.js +95 -0
  13. package/dist/body-sync.d.ts +351 -0
  14. package/dist/body-sync.js +1204 -0
  15. package/dist/calendar-parts.d.ts +23 -0
  16. package/dist/calendar-parts.js +18 -0
  17. package/dist/classification-backfill.d.ts +91 -0
  18. package/dist/classification-backfill.js +180 -0
  19. package/dist/classify-message.d.ts +31 -0
  20. package/dist/classify-message.js +89 -0
  21. package/dist/connection-factory.d.ts +54 -0
  22. package/dist/connection-factory.js +54 -0
  23. package/dist/connection-test.d.ts +41 -0
  24. package/dist/connection-test.js +152 -0
  25. package/dist/external-move.d.ts +94 -0
  26. package/dist/external-move.js +173 -0
  27. package/dist/filter-operations.d.ts +42 -0
  28. package/dist/filter-operations.js +211 -0
  29. package/dist/filters/filter-config.d.ts +14 -0
  30. package/dist/filters/filter-config.js +10 -0
  31. package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
  32. package/dist/filters/list-id.js +26 -0
  33. package/dist/filters/match.d.ts +69 -0
  34. package/dist/filters/match.js +165 -0
  35. package/dist/filters/pipeline.d.ts +64 -0
  36. package/dist/filters/pipeline.js +100 -0
  37. package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
  38. package/dist/flag-push-terminal.js +63 -0
  39. package/dist/flag-push.d.ts +69 -0
  40. package/dist/flag-push.js +111 -0
  41. package/dist/flag-queue.d.ts +175 -0
  42. package/dist/flag-queue.js +291 -0
  43. package/dist/folder-operations.d.ts +39 -0
  44. package/dist/folder-operations.js +112 -0
  45. package/dist/folder-placement.d.ts +13 -0
  46. package/dist/folder-placement.js +23 -0
  47. package/dist/heuristics/authenticationResults.d.ts +10 -0
  48. package/dist/heuristics/authenticationResults.js +187 -0
  49. package/dist/heuristics/classifyByHeaders.d.ts +82 -0
  50. package/dist/heuristics/classifyByHeaders.js +365 -0
  51. package/dist/heuristics/classifyPlacement.d.ts +67 -0
  52. package/dist/heuristics/classifyPlacement.js +153 -0
  53. package/dist/heuristics/machineSenders.d.ts +21 -0
  54. package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
  55. package/dist/heuristics/senderMismatch.d.ts +81 -0
  56. package/dist/heuristics/senderMismatch.js +192 -0
  57. package/dist/heuristics/socialDomains.d.ts +14 -0
  58. package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
  59. package/dist/heuristics/transactionalDomains.d.ts +18 -0
  60. package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
  61. package/dist/imapflow-connection.d.ts +377 -0
  62. package/dist/imapflow-connection.js +1147 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.js +65 -0
  65. package/dist/list-id-backfill.d.ts +83 -0
  66. package/dist/list-id-backfill.js +132 -0
  67. package/dist/mailbox-cursor-rebuild.d.ts +74 -0
  68. package/dist/mailbox-cursor-rebuild.js +71 -0
  69. package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
  70. package/dist/mailbox-cursor.js +102 -0
  71. package/dist/mailbox-intent.d.ts +41 -0
  72. package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
  73. package/dist/mailbox-management.d.ts +240 -0
  74. package/dist/mailbox-management.js +576 -0
  75. package/dist/mailbox-presence.d.ts +52 -0
  76. package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
  77. package/dist/mailbox-queue.d.ts +129 -0
  78. package/dist/mailbox-queue.js +216 -0
  79. package/dist/mailbox-sync.d.ts +93 -0
  80. package/dist/mailbox-sync.js +490 -0
  81. package/dist/mailbox-upstream.d.ts +38 -0
  82. package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
  83. package/dist/message-category.d.ts +51 -0
  84. package/{src/message-category.ts → dist/message-category.js} +28 -71
  85. package/dist/message-move.d.ts +285 -0
  86. package/dist/message-move.js +858 -0
  87. package/dist/message-parser.d.ts +14 -0
  88. package/dist/message-parser.js +17 -0
  89. package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
  90. package/dist/message-presence.js +29 -0
  91. package/dist/message-read.d.ts +51 -0
  92. package/dist/message-read.js +31 -0
  93. package/dist/message-sync.d.ts +345 -0
  94. package/dist/message-sync.js +1358 -0
  95. package/dist/mime-walker.d.ts +102 -0
  96. package/dist/mime-walker.js +198 -0
  97. package/dist/organize-job.d.ts +20 -0
  98. package/dist/organize-job.js +37 -0
  99. package/dist/outbox-attachment-content.d.ts +37 -0
  100. package/dist/outbox-attachment-content.js +50 -0
  101. package/dist/outbox-attachment-filename.d.ts +19 -0
  102. package/dist/outbox-attachment-filename.js +64 -0
  103. package/dist/outbox-attachment.d.ts +148 -0
  104. package/dist/outbox-attachment.js +273 -0
  105. package/dist/outbox-queue.d.ts +73 -0
  106. package/dist/outbox-queue.js +280 -0
  107. package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
  108. package/dist/outbox-status.js +18 -0
  109. package/dist/ownership.d.ts +24 -0
  110. package/dist/ownership.js +83 -0
  111. package/dist/placement-move-terminal.d.ts +67 -0
  112. package/dist/placement-move-terminal.js +88 -0
  113. package/dist/placement-move.d.ts +83 -0
  114. package/dist/placement-move.js +216 -0
  115. package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
  116. package/dist/placement-settled.js +75 -0
  117. package/dist/quarantine.d.ts +95 -0
  118. package/dist/quarantine.js +131 -0
  119. package/dist/refusal.d.ts +31 -0
  120. package/dist/refusal.js +3 -0
  121. package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
  122. package/dist/restore-source-placement.js +79 -0
  123. package/dist/search-text-backfill.d.ts +15 -0
  124. package/dist/search-text-backfill.js +33 -0
  125. package/dist/snippet.d.ts +61 -0
  126. package/dist/snippet.js +129 -0
  127. package/dist/spam-report.d.ts +86 -0
  128. package/dist/spam-report.js +241 -0
  129. package/dist/sync-warning.d.ts +21 -0
  130. package/dist/sync-warning.js +26 -0
  131. package/dist/sync-watermarks.d.ts +138 -0
  132. package/dist/sync-watermarks.js +205 -0
  133. package/dist/test-helpers/fake-imap.d.ts +151 -0
  134. package/dist/test-helpers/fake-imap.js +508 -0
  135. package/dist/test-helpers/folder-roles.d.ts +34 -0
  136. package/dist/test-helpers/folder-roles.js +47 -0
  137. package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
  138. package/dist/test-helpers/mime-tree-generator.js +329 -0
  139. package/dist/test-helpers/test-store.d.ts +69 -0
  140. package/dist/test-helpers/test-store.js +193 -0
  141. package/dist/text/normalizer.d.ts +20 -0
  142. package/dist/text/normalizer.js +102 -0
  143. package/dist/thread-message-rows.d.ts +69 -0
  144. package/dist/thread-message-rows.js +40 -0
  145. package/dist/thread-root.d.ts +7 -0
  146. package/dist/thread-root.js +25 -0
  147. package/dist/types.d.ts +343 -0
  148. package/dist/types.js +19 -0
  149. package/package.json +86 -88
  150. package/src/account-credentials.ts +0 -210
  151. package/src/attribute-mapper.test.ts +0 -104
  152. package/src/attribute-mapper.ts +0 -148
  153. package/src/body-parse.test.ts +0 -55
  154. package/src/body-part-mapper.property.test.ts +0 -354
  155. package/src/body-part-mapper.ts +0 -410
  156. package/src/body-sync-queue.test.ts +0 -64
  157. package/src/body-sync-queue.ts +0 -131
  158. package/src/body-sync-terminal.test.ts +0 -148
  159. package/src/body-sync-terminal.ts +0 -174
  160. package/src/body-sync.test.ts +0 -925
  161. package/src/body-sync.ts +0 -1891
  162. package/src/calendar-parts.ts +0 -41
  163. package/src/classification-backfill.test.ts +0 -285
  164. package/src/classification-backfill.ts +0 -363
  165. package/src/classify-message.test.ts +0 -142
  166. package/src/classify-message.ts +0 -116
  167. package/src/connection-factory.ts +0 -102
  168. package/src/connection-test.ts +0 -208
  169. package/src/credentials.test.ts +0 -181
  170. package/src/external-move.test.ts +0 -262
  171. package/src/external-move.ts +0 -285
  172. package/src/filter-operations.test.ts +0 -319
  173. package/src/filter-operations.ts +0 -377
  174. package/src/filter-update.test.ts +0 -313
  175. package/src/filters/filter-config.test.ts +0 -87
  176. package/src/filters/filter-config.ts +0 -34
  177. package/src/filters/list-id.test.ts +0 -50
  178. package/src/filters/match.test.ts +0 -392
  179. package/src/filters/match.ts +0 -225
  180. package/src/filters/pipeline.test.ts +0 -199
  181. package/src/filters/pipeline.ts +0 -170
  182. package/src/flag-push.test.ts +0 -112
  183. package/src/flag-push.ts +0 -198
  184. package/src/flag-queue.test.ts +0 -197
  185. package/src/flag-queue.ts +0 -509
  186. package/src/folder-operations.test.ts +0 -273
  187. package/src/folder-operations.ts +0 -217
  188. package/src/folder-placement.ts +0 -56
  189. package/src/forwarded-message.test.ts +0 -122
  190. package/src/heuristics/authenticationResults.test.ts +0 -189
  191. package/src/heuristics/authenticationResults.ts +0 -212
  192. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  193. package/src/heuristics/classifyByHeaders.ts +0 -467
  194. package/src/heuristics/classifyPlacement.test.ts +0 -366
  195. package/src/heuristics/classifyPlacement.ts +0 -211
  196. package/src/heuristics/classifyRealMail.test.ts +0 -242
  197. package/src/heuristics/machineSenders.test.ts +0 -101
  198. package/src/heuristics/senderMismatch.test.ts +0 -346
  199. package/src/heuristics/senderMismatch.ts +0 -259
  200. package/src/imapflow-connect-retry.test.ts +0 -97
  201. package/src/imapflow-connection.test.ts +0 -503
  202. package/src/imapflow-connection.ts +0 -1476
  203. package/src/index.ts +0 -397
  204. package/src/list-id-backfill.test.ts +0 -207
  205. package/src/list-id-backfill.ts +0 -274
  206. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  207. package/src/mailbox-cursor-rebuild.ts +0 -146
  208. package/src/mailbox-cursor.test.ts +0 -141
  209. package/src/mailbox-delete-intent.test.ts +0 -119
  210. package/src/mailbox-management.test.ts +0 -243
  211. package/src/mailbox-management.ts +0 -838
  212. package/src/mailbox-presence.test.ts +0 -74
  213. package/src/mailbox-queue.ts +0 -397
  214. package/src/mailbox-rename-intent.test.ts +0 -220
  215. package/src/mailbox-rename-subtree.test.ts +0 -287
  216. package/src/mailbox-sync.test.ts +0 -286
  217. package/src/mailbox-sync.ts +0 -724
  218. package/src/message-move.test.ts +0 -726
  219. package/src/message-move.ts +0 -1518
  220. package/src/message-parser.ts +0 -26
  221. package/src/message-presence.test.ts +0 -50
  222. package/src/message-read.ts +0 -87
  223. package/src/message-save-retry.test.ts +0 -90
  224. package/src/message-sync-flags.test.ts +0 -167
  225. package/src/message-sync.test.ts +0 -762
  226. package/src/message-sync.ts +0 -2133
  227. package/src/mime-walker.test.ts +0 -311
  228. package/src/mime-walker.ts +0 -306
  229. package/src/organize-job.ts +0 -93
  230. package/src/outbox-attachment-content.test.ts +0 -153
  231. package/src/outbox-attachment-content.ts +0 -104
  232. package/src/outbox-attachment-filename.test.ts +0 -132
  233. package/src/outbox-attachment-filename.ts +0 -76
  234. package/src/outbox-attachment.test.ts +0 -626
  235. package/src/outbox-attachment.ts +0 -536
  236. package/src/outbox-failed-recovery.test.ts +0 -299
  237. package/src/outbox-queue.test.ts +0 -315
  238. package/src/outbox-queue.ts +0 -525
  239. package/src/ownership.ts +0 -140
  240. package/src/placement-move-terminal.ts +0 -145
  241. package/src/placement-move.ts +0 -363
  242. package/src/placement-settled.test.ts +0 -147
  243. package/src/placement.test.ts +0 -404
  244. package/src/quarantine.test.ts +0 -133
  245. package/src/quarantine.ts +0 -229
  246. package/src/refusal.ts +0 -29
  247. package/src/snippet.test.ts +0 -213
  248. package/src/snippet.ts +0 -152
  249. package/src/spam-report.test.ts +0 -329
  250. package/src/spam-report.ts +0 -357
  251. package/src/sync-warning.test.ts +0 -213
  252. package/src/sync-warning.ts +0 -56
  253. package/src/sync-watermarks.test.ts +0 -409
  254. package/src/sync-watermarks.ts +0 -319
  255. package/src/system-flag-wire-format.test.ts +0 -77
  256. package/src/test-helpers/fake-imap.ts +0 -690
  257. package/src/test-helpers/folder-roles.ts +0 -66
  258. package/src/test-helpers/mime-tree-generator.ts +0 -461
  259. package/src/test-helpers/test-queue.ts +0 -56
  260. package/src/test-helpers/test-store.ts +0 -299
  261. package/src/text/normalizer.test.ts +0 -223
  262. package/src/text/normalizer.ts +0 -140
  263. package/src/thread-message-rows.ts +0 -77
  264. package/src/thread-root.test.ts +0 -29
  265. package/src/thread-root.ts +0 -51
  266. package/src/types/mailparser-augment.d.ts +0 -23
  267. package/src/types/natural-porter-stemmer.d.ts +0 -50
  268. package/src/types/stopword.d.ts +0 -69
  269. package/src/types.ts +0 -371
  270. package/tsconfig.json +0 -8
@@ -1,377 +0,0 @@
1
- import {
2
- deriveFilterTtl,
3
- FILTER_NO_ACTION,
4
- type FilterItem,
5
- type IAccountRepository,
6
- type IFilterRepository,
7
- type IMailboxRepository,
8
- type MailboxItem,
9
- type OrganizeJobRequestItem,
10
- type UpdateFilterInput,
11
- } from "@remit/data-ports";
12
- import {
13
- FilterDisabledReason,
14
- FilterScope,
15
- FilterState,
16
- } from "@remit/domain-enums";
17
- import {
18
- type OrganizeJobDeps,
19
- type OrganizeJobOwner,
20
- queueOrganizeJob,
21
- } from "./organize-job.js";
22
- import { folderInAccount, ownedAccount, settledFolder } from "./ownership.js";
23
- import { accept, invalid, type Refusable } from "./refusal.js";
24
-
25
- export type NewFilter = Pick<
26
- FilterItem,
27
- | "name"
28
- | "scope"
29
- | "expiresAt"
30
- | "matchOperator"
31
- | "literalClauses"
32
- | "actionLabelId"
33
- | "actionMailboxId"
34
- >;
35
-
36
- export type FilterPatch = Pick<
37
- UpdateFilterInput,
38
- | "name"
39
- | "scope"
40
- | "expiresAt"
41
- | "matchOperator"
42
- | "literalClauses"
43
- | "actionLabelId"
44
- | "actionMailboxId"
45
- | "state"
46
- >;
47
-
48
- interface ResolvedScopeExpiry {
49
- scope: FilterItem["scope"];
50
- expiresAt?: string;
51
- ttl?: number;
52
- state: FilterItem["state"];
53
- }
54
-
55
- export interface FilterPage {
56
- items: FilterItem[];
57
- continuationToken?: string;
58
- }
59
-
60
- export interface CreatedFilter {
61
- filter: FilterItem;
62
- organizeJob: OrganizeJobRequestItem;
63
- }
64
-
65
- interface AccountDeps {
66
- account: Pick<IAccountRepository, "get">;
67
- }
68
-
69
- interface ActionFolderDeps {
70
- mailbox: Pick<IMailboxRepository, "resolveAccountId"> & {
71
- get(accountId: string, mailboxId: string): Promise<MailboxItem>;
72
- };
73
- }
74
-
75
- // Only these keys reach the repo, and a key absent from the body stays absent,
76
- // so a rename never bumps ruleChangedAt and a smuggled ttl is dropped.
77
- const pickFilterUpdate = (body: FilterPatch): FilterPatch => {
78
- const patch: FilterPatch = {};
79
- if (Object.hasOwn(body, "name")) patch.name = body.name;
80
- if (Object.hasOwn(body, "scope")) patch.scope = body.scope;
81
- if (Object.hasOwn(body, "expiresAt")) patch.expiresAt = body.expiresAt;
82
- if (Object.hasOwn(body, "matchOperator")) {
83
- patch.matchOperator = body.matchOperator;
84
- }
85
- if (Object.hasOwn(body, "literalClauses")) {
86
- patch.literalClauses = body.literalClauses;
87
- }
88
- if (Object.hasOwn(body, "actionLabelId")) {
89
- patch.actionLabelId = body.actionLabelId;
90
- }
91
- if (Object.hasOwn(body, "actionMailboxId")) {
92
- patch.actionMailboxId = body.actionMailboxId;
93
- }
94
- if (Object.hasOwn(body, "state")) patch.state = body.state;
95
- return patch;
96
- };
97
-
98
- // A Temporary filter always carries expiresAt; outside Temporary both
99
- // expiresAt and the table-wide ttl stay absent, or the row would be swept.
100
- const resolveFilterScopeExpiry = (
101
- current: Pick<FilterItem, "scope" | "expiresAt">,
102
- patch: FilterPatch,
103
- ): Refusable<ResolvedScopeExpiry> => {
104
- const scope = patch.scope ?? current.scope;
105
-
106
- if (scope !== FilterScope.Temporary) {
107
- if (Object.hasOwn(patch, "expiresAt") && patch.expiresAt) {
108
- return invalid(
109
- "expiresAt only applies to a Temporary filter — switch scope to Temporary to set one.",
110
- );
111
- }
112
- return accept({
113
- scope,
114
- expiresAt: undefined,
115
- ttl: undefined,
116
- state: FilterState.Active,
117
- });
118
- }
119
-
120
- const expiresAt = Object.hasOwn(patch, "expiresAt")
121
- ? patch.expiresAt
122
- : current.expiresAt;
123
- if (!expiresAt) {
124
- return invalid(
125
- "A Temporary filter needs expiresAt — pick a date, or switch scope to Standing.",
126
- );
127
- }
128
-
129
- const ttl = deriveFilterTtl(scope, expiresAt);
130
- const state =
131
- new Date(expiresAt).getTime() > Date.now()
132
- ? FilterState.Active
133
- : FilterState.Expired;
134
-
135
- return accept({ scope, expiresAt, ttl, state });
136
- };
137
-
138
- const hasLapsed = (
139
- filter: Pick<FilterItem, "scope" | "expiresAt">,
140
- now: number,
141
- ): boolean =>
142
- filter.scope === FilterScope.Temporary &&
143
- filter.expiresAt !== undefined &&
144
- new Date(filter.expiresAt).getTime() <= now;
145
-
146
- const FOLDERLESS_REASONS: ReadonlySet<FilterItem["disabledReason"]> = new Set([
147
- FilterDisabledReason.AwaitingFolder,
148
- FilterDisabledReason.FolderCreateFailed,
149
- ]);
150
-
151
- const folderlessMessage = (reason: FilterItem["disabledReason"]): string =>
152
- reason === FilterDisabledReason.AwaitingFolder
153
- ? "This filter has no folder to move mail into yet. Wait for its folder to be created on the mail server, or pick a folder in the rule, then turn it on."
154
- : "The mail server refused to create this filter's folder. Pick a folder in the rule, then turn it on.";
155
-
156
- export const resolveFilterUpdate = (
157
- current: Pick<
158
- FilterItem,
159
- "scope" | "expiresAt" | "state" | "disabledReason" | "actionMailboxId"
160
- >,
161
- patch: FilterPatch,
162
- now: number = Date.now(),
163
- ): Refusable<UpdateFilterInput> => {
164
- if (patch.state === FilterState.Expired) {
165
- return invalid(
166
- "A filter expires through its date, not its state. Set it Active or Disabled, or change its expiry.",
167
- );
168
- }
169
- const folderless =
170
- current.state === FilterState.Disabled &&
171
- FOLDERLESS_REASONS.has(current.disabledReason);
172
- const folder = patch.actionMailboxId ?? current.actionMailboxId;
173
- const picked = folderless && folder !== FILTER_NO_ACTION;
174
- const pickedReason = picked
175
- ? { disabledReason: FilterDisabledReason.UserDisabled }
176
- : {};
177
-
178
- const touchesTiming =
179
- Object.hasOwn(patch, "scope") || Object.hasOwn(patch, "expiresAt");
180
- if (!touchesTiming && patch.state === undefined) {
181
- return accept({ ...patch, ...pickedReason });
182
- }
183
-
184
- let timing: ResolvedScopeExpiry | undefined;
185
- if (touchesTiming) {
186
- const resolved = resolveFilterScopeExpiry(current, patch);
187
- if (!resolved.ok) return resolved;
188
- timing = resolved.value;
189
- }
190
- const lapsed = timing
191
- ? timing.state === FilterState.Expired
192
- : hasLapsed(current, now);
193
-
194
- if (patch.state === FilterState.Active && lapsed) {
195
- return invalid(
196
- "This filter has expired. Move its date forward or make it Standing to turn it back on.",
197
- );
198
- }
199
-
200
- const wanted = patch.state ?? current.state;
201
- const merged = { ...patch, ...timing };
202
- if (wanted === FilterState.Disabled) {
203
- return accept({
204
- ...merged,
205
- state: FilterState.Disabled,
206
- ...(folderless
207
- ? pickedReason
208
- : patch.state === FilterState.Disabled
209
- ? { disabledReason: FilterDisabledReason.UserDisabled }
210
- : {}),
211
- });
212
- }
213
- if (lapsed) {
214
- return accept({
215
- ...merged,
216
- state: FilterState.Expired,
217
- disabledReason: FilterDisabledReason.None,
218
- });
219
- }
220
- if (folderless && folder === FILTER_NO_ACTION) {
221
- return invalid(folderlessMessage(current.disabledReason));
222
- }
223
- return accept({
224
- ...merged,
225
- state: FilterState.Active,
226
- disabledReason: FilterDisabledReason.None,
227
- });
228
- };
229
-
230
- // The sentinel means "no move action" and binds to nothing, so it is not
231
- // gated, and neither is a patch that leaves the field alone.
232
- const settledActionFolder = async (
233
- deps: Pick<ActionFolderDeps, "mailbox">,
234
- accountId: string,
235
- actionMailboxId: string | undefined,
236
- ): Promise<Refusable<string | undefined>> => {
237
- if (actionMailboxId === undefined || actionMailboxId === FILTER_NO_ACTION) {
238
- return accept(actionMailboxId);
239
- }
240
- const target = await deps.mailbox.get(accountId, actionMailboxId);
241
- const inAccount = folderInAccount(target, accountId, "act");
242
- if (!inAccount.ok) return inAccount;
243
- const settled = settledFolder(target);
244
- if (!settled.ok) return settled;
245
- return accept(actionMailboxId);
246
- };
247
-
248
- const existingActionFolder = async (
249
- deps: ActionFolderDeps,
250
- accountId: string,
251
- actionMailboxId: string,
252
- ): Promise<Refusable<string | undefined>> => {
253
- if (actionMailboxId === FILTER_NO_ACTION) return accept(actionMailboxId);
254
- if ((await deps.mailbox.resolveAccountId(actionMailboxId)) === null) {
255
- return invalid(
256
- "The folder this filter moves mail into no longer exists. Pick another folder, then turn the filter on.",
257
- );
258
- }
259
- return settledActionFolder(deps, accountId, actionMailboxId);
260
- };
261
-
262
- export const listFilters = async (
263
- deps: AccountDeps & {
264
- filter: Pick<
265
- IFilterRepository,
266
- "listPageByAccountConfig" | "refreshExpiry"
267
- >;
268
- },
269
- accountConfigId: string,
270
- query: { accountId: string; continuationToken: string | undefined },
271
- ): Promise<Refusable<FilterPage>> => {
272
- const owned = ownedAccount(
273
- await deps.account.get(query.accountId),
274
- accountConfigId,
275
- "read",
276
- );
277
- if (!owned.ok) return owned;
278
-
279
- const page = await deps.filter.listPageByAccountConfig(accountConfigId, {
280
- continuationToken: query.continuationToken,
281
- });
282
- // Reading the list lazily expires a lapsed Temporary filter.
283
- const items = await Promise.all(
284
- page.items.map((item) => deps.filter.refreshExpiry(item)),
285
- );
286
- return accept({ items, continuationToken: page.continuationToken });
287
- };
288
-
289
- export const createFilter = async (
290
- deps: AccountDeps &
291
- ActionFolderDeps &
292
- OrganizeJobDeps & { filter: Pick<IFilterRepository, "create"> },
293
- owner: OrganizeJobOwner,
294
- input: { accountId: string; filter: NewFilter },
295
- ): Promise<Refusable<CreatedFilter>> => {
296
- const { accountConfigId, userId } = owner;
297
- const { accountId, filter: rule } = input;
298
- const owned = ownedAccount(
299
- await deps.account.get(accountId),
300
- accountConfigId,
301
- "act",
302
- );
303
- if (!owned.ok) return owned;
304
- const scoped = resolveFilterScopeExpiry(
305
- { scope: rule.scope, expiresAt: undefined },
306
- { scope: rule.scope, expiresAt: rule.expiresAt },
307
- );
308
- if (!scoped.ok) return scoped;
309
- const target = await settledActionFolder(
310
- deps,
311
- accountId,
312
- rule.actionMailboxId,
313
- );
314
- if (!target.ok) return target;
315
-
316
- const filter = await deps.filter.create({
317
- accountConfigId,
318
- name: rule.name,
319
- scope: scoped.value.scope,
320
- expiresAt: scoped.value.expiresAt,
321
- ttl: scoped.value.ttl,
322
- matchOperator: rule.matchOperator,
323
- literalClauses: rule.literalClauses,
324
- actionLabelId: rule.actionLabelId,
325
- actionMailboxId: rule.actionMailboxId,
326
- });
327
- // The new filter's first pass over existing mail.
328
- const organizeJob = await queueOrganizeJob(deps, {
329
- accountConfigId,
330
- userId,
331
- filterId: filter.filterId,
332
- });
333
- return accept({ filter, organizeJob });
334
- };
335
-
336
- export const updateFilter = async (
337
- deps: AccountDeps &
338
- ActionFolderDeps & {
339
- filter: Pick<IFilterRepository, "get" | "update">;
340
- },
341
- accountConfigId: string,
342
- input: { accountId: string; filterId: string; patch: FilterPatch },
343
- ): Promise<Refusable<FilterItem>> => {
344
- const { accountId, filterId } = input;
345
- const owned = ownedAccount(
346
- await deps.account.get(accountId),
347
- accountConfigId,
348
- "act",
349
- );
350
- if (!owned.ok) return owned;
351
-
352
- const patch = pickFilterUpdate(input.patch);
353
- const target = await settledActionFolder(
354
- deps,
355
- accountId,
356
- patch.actionMailboxId,
357
- );
358
- if (!target.ok) return target;
359
- const current = await deps.filter.get(accountConfigId, filterId);
360
- const resolved = resolveFilterUpdate(current, patch);
361
- if (!resolved.ok) return resolved;
362
- if (
363
- patch.state === FilterState.Active &&
364
- patch.actionMailboxId === undefined
365
- ) {
366
- const existing = await existingActionFolder(
367
- deps,
368
- accountId,
369
- current.actionMailboxId,
370
- );
371
- if (!existing.ok) return existing;
372
- }
373
-
374
- return accept(
375
- await deps.filter.update(accountConfigId, filterId, resolved.value),
376
- );
377
- };
@@ -1,313 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { describe, it } from "node:test";
3
- import {
4
- FilterDisabledReason,
5
- FilterScope,
6
- FilterState,
7
- } from "@remit/domain-enums";
8
- import { resolveFilterUpdate } from "./filter-operations.js";
9
- import type { Refusable } from "./refusal.js";
10
-
11
- const NOW = Date.parse("2026-09-25T12:00:00Z");
12
-
13
- const standing = {
14
- scope: FilterScope.Standing,
15
- expiresAt: undefined,
16
- state: FilterState.Active,
17
- disabledReason: FilterDisabledReason.None,
18
- actionMailboxId: "mbx-invoices",
19
- };
20
-
21
- const awaiting = {
22
- ...standing,
23
- state: FilterState.Disabled,
24
- disabledReason: FilterDisabledReason.AwaitingFolder,
25
- actionMailboxId: "None",
26
- };
27
-
28
- const acceptedValue = <T>(outcome: Refusable<T>): T => {
29
- if (!outcome.ok) assert.fail(`refused: ${outcome.refusal.message}`);
30
- return outcome.value;
31
- };
32
-
33
- const refusedWith = <T>(outcome: Refusable<T>, message: RegExp): void => {
34
- assert.equal(outcome.ok, false, "expected a refusal");
35
- if (outcome.ok) return;
36
- assert.equal(outcome.refusal.kind, "Invalid");
37
- assert.match(outcome.refusal.message, message);
38
- };
39
-
40
- describe("resolveFilterUpdate: state (#1103)", () => {
41
- it("records UserDisabled when the user turns a filter off", () => {
42
- assert.deepEqual(
43
- resolveFilterUpdate(standing, { state: FilterState.Disabled }, NOW),
44
- {
45
- ok: true,
46
- value: {
47
- state: FilterState.Disabled,
48
- disabledReason: FilterDisabledReason.UserDisabled,
49
- },
50
- },
51
- );
52
- });
53
-
54
- for (const reason of [
55
- FilterDisabledReason.UserDisabled,
56
- FilterDisabledReason.AwaitingFolder,
57
- FilterDisabledReason.FolderCreateFailed,
58
- FilterDisabledReason.FolderMissing,
59
- ]) {
60
- it(`clears ${reason} when the user turns the filter back on`, () => {
61
- assert.deepEqual(
62
- resolveFilterUpdate(
63
- { ...standing, state: FilterState.Disabled, disabledReason: reason },
64
- { state: FilterState.Active },
65
- NOW,
66
- ),
67
- {
68
- ok: true,
69
- value: {
70
- state: FilterState.Active,
71
- disabledReason: FilterDisabledReason.None,
72
- },
73
- },
74
- );
75
- });
76
- }
77
-
78
- it("keeps a disabled filter disabled, with its reason, through an expiry edit", () => {
79
- const resolved = acceptedValue(
80
- resolveFilterUpdate(
81
- { ...standing, state: FilterState.Disabled },
82
- {
83
- scope: FilterScope.Temporary,
84
- expiresAt: "2027-01-01T00:00:00+00:00",
85
- },
86
- NOW,
87
- ),
88
- );
89
- assert.equal(resolved.state, FilterState.Disabled);
90
- assert.equal("disabledReason" in resolved, false);
91
- });
92
-
93
- for (const reason of [
94
- FilterDisabledReason.AwaitingFolder,
95
- FilterDisabledReason.FolderCreateFailed,
96
- ]) {
97
- it(`refuses to turn on a ${reason} filter that has no folder`, () => {
98
- refusedWith(
99
- resolveFilterUpdate(
100
- { ...awaiting, disabledReason: reason },
101
- { state: FilterState.Active },
102
- NOW,
103
- ),
104
- /pick a folder in the rule/i,
105
- );
106
- });
107
-
108
- it(`turns on a ${reason} filter once the patch names a folder`, () => {
109
- const resolved = acceptedValue(
110
- resolveFilterUpdate(
111
- { ...awaiting, disabledReason: reason },
112
- { state: FilterState.Active, actionMailboxId: "mbx-picked" },
113
- NOW,
114
- ),
115
- );
116
- assert.equal(resolved.state, FilterState.Active);
117
- });
118
- }
119
-
120
- it("keeps a folderless reason when the user turns an awaiting filter off again", () => {
121
- const resolved = acceptedValue(
122
- resolveFilterUpdate(awaiting, { state: FilterState.Disabled }, NOW),
123
- );
124
- assert.equal(resolved.state, FilterState.Disabled);
125
- assert.equal("disabledReason" in resolved, false);
126
- refusedWith(
127
- resolveFilterUpdate(awaiting, { state: FilterState.Active }, NOW),
128
- /pick a folder in the rule/i,
129
- );
130
- });
131
-
132
- it("keeps a lapsed folderless filter off when its date moves forward", () => {
133
- const lapsed = {
134
- ...awaiting,
135
- scope: FilterScope.Temporary,
136
- expiresAt: "2026-01-01T00:00:00+00:00",
137
- disabledReason: FilterDisabledReason.FolderCreateFailed,
138
- };
139
- const resolved = acceptedValue(
140
- resolveFilterUpdate(
141
- lapsed,
142
- {
143
- scope: FilterScope.Temporary,
144
- expiresAt: "2027-01-01T00:00:00+00:00",
145
- },
146
- NOW,
147
- ),
148
- );
149
- assert.equal(resolved.state, FilterState.Disabled);
150
- assert.equal("disabledReason" in resolved, false);
151
- refusedWith(
152
- resolveFilterUpdate(
153
- { ...lapsed, expiresAt: "2027-01-01T00:00:00+00:00" },
154
- { state: FilterState.Active },
155
- NOW,
156
- ),
157
- /pick a folder in the rule/i,
158
- );
159
- });
160
-
161
- it("marks a folderless filter UserDisabled once the user picks a folder", () => {
162
- assert.deepEqual(
163
- resolveFilterUpdate(awaiting, { actionMailboxId: "mbx-picked" }, NOW),
164
- {
165
- ok: true,
166
- value: {
167
- actionMailboxId: "mbx-picked",
168
- disabledReason: FilterDisabledReason.UserDisabled,
169
- },
170
- },
171
- );
172
- });
173
-
174
- it("refuses Expired as a state the user sets", () => {
175
- refusedWith(
176
- resolveFilterUpdate(standing, { state: FilterState.Expired }, NOW),
177
- /expires through its date/,
178
- );
179
- });
180
-
181
- it("refuses to turn on a temporary filter whose date has passed", () => {
182
- refusedWith(
183
- resolveFilterUpdate(
184
- {
185
- ...standing,
186
- scope: FilterScope.Temporary,
187
- expiresAt: "2026-01-01T00:00:00+00:00",
188
- state: FilterState.Disabled,
189
- },
190
- { state: FilterState.Active },
191
- NOW,
192
- ),
193
- /has expired/,
194
- );
195
- });
196
-
197
- it("leaves a patch that names neither state nor timing untouched", () => {
198
- assert.deepEqual(resolveFilterUpdate(standing, { name: "Receipts" }, NOW), {
199
- ok: true,
200
- value: { name: "Receipts" },
201
- });
202
- });
203
- });
204
-
205
- describe("resolveFilterUpdate: scope and expiry (reader #266)", () => {
206
- it("moves Standing to Temporary given a future expiresAt", () => {
207
- const resolved = acceptedValue(
208
- resolveFilterUpdate(standing, {
209
- scope: FilterScope.Temporary,
210
- expiresAt: "2099-01-01T00:00:00+00:00",
211
- }),
212
- );
213
- assert.equal(resolved.scope, FilterScope.Temporary);
214
- assert.equal(resolved.expiresAt, "2099-01-01T00:00:00+00:00");
215
- assert.equal(resolved.state, FilterState.Active);
216
- assert.ok(resolved.ttl && resolved.ttl > 0);
217
- });
218
-
219
- it("refuses moving to Temporary without an expiresAt", () => {
220
- refusedWith(
221
- resolveFilterUpdate(standing, { scope: FilterScope.Temporary }),
222
- /needs expiresAt/,
223
- );
224
- });
225
-
226
- it("refuses an expiresAt that isn't paired with a Temporary scope", () => {
227
- refusedWith(
228
- resolveFilterUpdate(standing, { expiresAt: "2099-01-01T00:00:00+00:00" }),
229
- /only applies to a Temporary/,
230
- );
231
- });
232
-
233
- it("clears expiresAt/ttl and reports Active when moving Temporary to Standing", () => {
234
- const resolved = acceptedValue(
235
- resolveFilterUpdate(
236
- {
237
- ...standing,
238
- scope: FilterScope.Temporary,
239
- expiresAt: "2020-01-01T00:00:00+00:00",
240
- state: FilterState.Expired,
241
- },
242
- { scope: FilterScope.Standing },
243
- ),
244
- );
245
- assert.equal(resolved.scope, FilterScope.Standing);
246
- assert.equal(resolved.expiresAt, undefined);
247
- assert.equal(resolved.ttl, undefined);
248
- assert.equal(resolved.state, FilterState.Active);
249
- });
250
-
251
- it("recomputes ttl for an expiresAt-only change on an already-Temporary filter", () => {
252
- const resolved = acceptedValue(
253
- resolveFilterUpdate(
254
- {
255
- ...standing,
256
- scope: FilterScope.Temporary,
257
- expiresAt: "2099-01-01T00:00:00+00:00",
258
- },
259
- { expiresAt: "2099-06-01T00:00:00+00:00" },
260
- ),
261
- );
262
- assert.equal(resolved.scope, FilterScope.Temporary);
263
- assert.equal(resolved.expiresAt, "2099-06-01T00:00:00+00:00");
264
- assert.equal(
265
- resolved.ttl,
266
- Math.floor(new Date("2099-06-01T00:00:00+00:00").getTime() / 1000),
267
- );
268
- assert.equal(resolved.state, FilterState.Active);
269
- });
270
-
271
- it("reactivates a lapsed filter extended into the future", () => {
272
- const resolved = acceptedValue(
273
- resolveFilterUpdate(
274
- {
275
- ...standing,
276
- scope: FilterScope.Temporary,
277
- expiresAt: "2020-01-01T00:00:00+00:00",
278
- state: FilterState.Expired,
279
- },
280
- { expiresAt: "2099-01-01T00:00:00+00:00" },
281
- ),
282
- );
283
- assert.equal(resolved.state, FilterState.Active);
284
- });
285
-
286
- it("reads Expired immediately when the new expiresAt is already in the past", () => {
287
- const resolved = acceptedValue(
288
- resolveFilterUpdate(
289
- {
290
- ...standing,
291
- scope: FilterScope.Temporary,
292
- expiresAt: "2099-01-01T00:00:00+00:00",
293
- },
294
- { expiresAt: "2020-01-01T00:00:00+00:00" },
295
- ),
296
- );
297
- assert.equal(resolved.state, FilterState.Expired);
298
- });
299
-
300
- it("rejects an unparseable expiresAt", () => {
301
- assert.throws(
302
- () =>
303
- resolveFilterUpdate(standing, {
304
- scope: FilterScope.Temporary,
305
- expiresAt: "not-a-date",
306
- }),
307
- (error: unknown) => {
308
- assert.equal((error as { statusCode?: number }).statusCode, 400);
309
- return true;
310
- },
311
- );
312
- });
313
- });