@swan-io/shared-business 18.2.0 → 18.4.0

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.
@@ -0,0 +1,659 @@
1
+ import { Option } from "@bloodyowl/boxed";
2
+ import { describe, expect, expectTypeOf, test } from "vitest";
3
+ import { createFakeFraudReportApi, } from "../fraudReport/fakeApi";
4
+ const NOW = new Date("2026-11-20T10:00:00.000Z");
5
+ const card1 = {
6
+ id: "card-1",
7
+ accountId: "account-1",
8
+ rail: "Card",
9
+ cardholderAccountMembershipId: "member-1",
10
+ };
11
+ const card2 = { ...card1, id: "card-2" };
12
+ const cardOtherHolder = {
13
+ ...card1,
14
+ id: "card-3",
15
+ cardholderAccountMembershipId: "member-2",
16
+ };
17
+ const transfer1 = { id: "transfer-1", accountId: "account-1", rail: "Transfer" };
18
+ const transferOtherAccount = {
19
+ ...transfer1,
20
+ id: "transfer-2",
21
+ accountId: "account-2",
22
+ };
23
+ const notReportable = {
24
+ ...transfer1,
25
+ id: "transfer-3",
26
+ notReportableReason: "TransactionStatusNotReportable",
27
+ };
28
+ const outsideWindow = {
29
+ ...transfer1,
30
+ id: "transfer-4",
31
+ reportingWindowEndsAt: "2026-10-01T00:00:00.000Z",
32
+ };
33
+ const fraudDetails = {
34
+ reportedReason: "Manipulated",
35
+ reportedTypology: "FakeAdvisor",
36
+ description: "A fake advisor called me",
37
+ };
38
+ const createApi = (config = {}) => createFakeFraudReportApi({
39
+ transactions: [
40
+ card1,
41
+ card2,
42
+ cardOtherHolder,
43
+ transfer1,
44
+ transferOtherAccount,
45
+ notReportable,
46
+ outsideWindow,
47
+ ],
48
+ accountMemberships: [
49
+ { id: "member-1", accountId: "account-1" },
50
+ { id: "member-2", accountId: "account-1" },
51
+ { id: "member-other", accountId: "account-2" },
52
+ ],
53
+ now: () => NOW,
54
+ ...config,
55
+ });
56
+ const getRejection = (result) => result.match({
57
+ Ok: () => undefined,
58
+ Error: error => (error.kind === "rejection" ? error.rejection : undefined),
59
+ });
60
+ const getOk = (result) => result.match({
61
+ Ok: value => value,
62
+ Error: error => {
63
+ throw new Error(`Expected Ok, got ${JSON.stringify(error)}`);
64
+ },
65
+ });
66
+ const draft = async (api, input) => getOk(await api.draft(input));
67
+ describe("contract types", () => {
68
+ test("the fake adapter implements FraudReportApi", () => {
69
+ expectTypeOf(createApi()).toExtend();
70
+ });
71
+ test("update: leaving a field out keeps it, null clears it, transactionIds can't be null", () => {
72
+ expectTypeOf().toExtend();
73
+ expectTypeOf().toExtend();
74
+ expectTypeOf().not.toExtend();
75
+ });
76
+ test("draft requires transactionIds only", () => {
77
+ expectTypeOf().toExtend();
78
+ expectTypeOf().not.toExtend();
79
+ });
80
+ });
81
+ describe("happy path", () => {
82
+ test("draft → update → submit", async () => {
83
+ var _a, _b;
84
+ const api = createApi();
85
+ const created = await draft(api, { transactionIds: ["transfer-1"] });
86
+ expect(created.statusInfo).toEqual({ status: "Draft", expiresAt: "2026-12-20T10:00:00.000Z" });
87
+ expect(created.accountId).toBe("account-1");
88
+ expect(created.hasVictim).toBe(false);
89
+ expect(created.supportingDocumentCollection.status).toBe("WaitingForDocument");
90
+ const updated = getOk(await api.update({
91
+ disputeCaseId: created.id,
92
+ fraudDetails,
93
+ fraudTransferDetails: { hasContactedBankOrBeneficiary: true },
94
+ victimAccountMembershipId: "member-2",
95
+ }));
96
+ expect((_a = updated.fraudDetails) === null || _a === void 0 ? void 0 : _a.description).toBe("A fake advisor called me");
97
+ expect((_b = updated.fraudDetails) === null || _b === void 0 ? void 0 : _b.fraudsterChannels).toEqual([]);
98
+ expect(updated.victimAccountMembershipId).toBe("member-2");
99
+ expect(updated.hasVictim).toBe(true);
100
+ const submitted = getOk(await api.submit({ disputeCaseId: created.id }));
101
+ expect(submitted.statusInfo).toEqual({ status: "Submitted", submittedAt: NOW.toISOString() });
102
+ expect(submitted.supportingDocumentCollection.status).toBe("PendingReview");
103
+ });
104
+ test("the victim of a case with card transactions is the cardholder", async () => {
105
+ const created = await draft(createApi(), { transactionIds: ["card-1", "transfer-1"] });
106
+ expect(created.victimAccountMembershipId).toBe("member-1");
107
+ expect(created.hasVictim).toBe(true);
108
+ });
109
+ test("update: left-out fields are kept, null clears", async () => {
110
+ var _a;
111
+ const api = createApi();
112
+ const created = await draft(api, {
113
+ transactionIds: ["transfer-1"],
114
+ fraudDetails,
115
+ externalReference: "ref-1",
116
+ });
117
+ const kept = getOk(await api.update({ disputeCaseId: created.id, externalReference: "ref-2" }));
118
+ expect((_a = kept.fraudDetails) === null || _a === void 0 ? void 0 : _a.reportedTypology).toBe("FakeAdvisor");
119
+ expect(kept.externalReference).toBe("ref-2");
120
+ const cleared = getOk(await api.update({ disputeCaseId: created.id, fraudDetails: null }));
121
+ expect(cleared.fraudDetails).toBeNull();
122
+ expect(cleared.externalReference).toBe("ref-2");
123
+ });
124
+ test("changing transactions removes details and victim that no longer apply", async () => {
125
+ const api = createApi();
126
+ const created = await draft(api, {
127
+ transactionIds: ["transfer-1"],
128
+ fraudTransferDetails: { hasContactedBankOrBeneficiary: false },
129
+ victimAccountMembershipId: "member-2",
130
+ });
131
+ const updated = getOk(await api.update({ disputeCaseId: created.id, transactionIds: ["card-1"] }));
132
+ expect(updated.fraudTransferDetails).toBeNull();
133
+ expect(updated.victimAccountMembershipId).toBe("member-1");
134
+ });
135
+ test("submitting twice returns the submitted case", async () => {
136
+ const api = createApi();
137
+ const created = await draft(api, { transactionIds: ["card-1"], fraudDetails });
138
+ const first = getOk(await api.submit({ disputeCaseId: created.id }));
139
+ const second = getOk(await api.submit({ disputeCaseId: created.id }));
140
+ expect(second).toEqual(first);
141
+ });
142
+ test("cancel, getCase and findDrafts", async () => {
143
+ const api = createApi();
144
+ const a = await draft(api, { transactionIds: ["card-1"] });
145
+ const b = await draft(api, { transactionIds: ["card-1", "card-2"] });
146
+ await draft(api, { transactionIds: ["transfer-2"] });
147
+ const drafts = getOk(await api.findDrafts({ accountId: "account-1", transactionIds: ["card-1"] }));
148
+ expect(drafts.map(item => item.id)).toEqual([b.id, a.id]);
149
+ const canceled = getOk(await api.cancel({ disputeCaseId: a.id }));
150
+ expect(canceled.statusInfo).toEqual({ status: "Canceled", canceledAt: NOW.toISOString() });
151
+ expect(getOk(await api.cancel({ disputeCaseId: a.id }))).toEqual(canceled);
152
+ const remaining = getOk(await api.findDrafts({ accountId: "account-1", transactionIds: ["card-1"] }));
153
+ expect(remaining.map(item => item.id)).toEqual([b.id]);
154
+ expect(getOk(await api.getCase({ disputeCaseId: b.id }))).toEqual(Option.Some(b));
155
+ expect(getOk(await api.getCase({ disputeCaseId: "unknown" }))).toEqual(Option.None());
156
+ });
157
+ test("starts from seeded cases", async () => {
158
+ const seeded = await draft(createApi(), { transactionIds: ["card-1"] });
159
+ const api = createApi({ cases: [seeded] });
160
+ expect(getOk(await api.getCase({ disputeCaseId: seeded.id }))).toEqual(Option.Some(seeded));
161
+ });
162
+ test("uploads resolve", async () => {
163
+ const api = createApi();
164
+ const upload = getOk(await api.generateUpload({
165
+ supportingDocumentCollectionId: "collection-1",
166
+ fileName: "police-report.pdf",
167
+ purpose: "PoliceReport",
168
+ }));
169
+ expect(upload.id).toEqual(expect.any(String));
170
+ expect((await api.removeFile({
171
+ id: upload.id,
172
+ name: "police-report.pdf",
173
+ statusInfo: { status: "Uploaded" },
174
+ })).isOk()).toBe(true);
175
+ });
176
+ });
177
+ describe("draft and update rejections", () => {
178
+ test("malformed transactionIds are errors, not rejections", async () => {
179
+ const api = createApi();
180
+ for (const transactionIds of [[], ["card-1", "card-1"]]) {
181
+ const result = await api.draft({ transactionIds });
182
+ expect(result.match({ Ok: () => undefined, Error: error => error.kind })).toBe("error");
183
+ }
184
+ });
185
+ test("DisputeCaseNotFoundRejection", async () => {
186
+ const result = await createApi().update({ disputeCaseId: "unknown", fraudDetails });
187
+ expect(getRejection(result)).toMatchObject({
188
+ __typename: "DisputeCaseNotFoundRejection",
189
+ disputeCaseId: "unknown",
190
+ });
191
+ });
192
+ test("DisputeCaseWrongStatusRejection", async () => {
193
+ const api = createApi();
194
+ const created = await draft(api, { transactionIds: ["card-1"] });
195
+ await api.cancel({ disputeCaseId: created.id });
196
+ expect(getRejection(await api.update({ disputeCaseId: created.id, fraudDetails }))).toMatchObject({
197
+ __typename: "DisputeCaseWrongStatusRejection",
198
+ currentStatus: "Canceled",
199
+ expectedStatuses: ["Draft"],
200
+ });
201
+ });
202
+ test("DisputeCaseTransactionsLimitExceededRejection", async () => {
203
+ const transactions = Array.from({ length: 101 }, (_, index) => ({
204
+ ...card1,
205
+ id: `card-${index}`,
206
+ }));
207
+ const result = await createApi({ transactions }).draft({
208
+ transactionIds: transactions.map(item => item.id),
209
+ });
210
+ expect(getRejection(result)).toMatchObject({
211
+ __typename: "DisputeCaseTransactionsLimitExceededRejection",
212
+ transactionCount: 101,
213
+ maxTransactionCount: 100,
214
+ });
215
+ });
216
+ test("DisputeCaseTransactionsNotFoundRejection", async () => {
217
+ const result = await createApi().draft({ transactionIds: ["card-1", "unknown"] });
218
+ expect(getRejection(result)).toMatchObject({
219
+ __typename: "DisputeCaseTransactionsNotFoundRejection",
220
+ transactionIds: ["unknown"],
221
+ });
222
+ });
223
+ test("DisputeCaseTransactionsNotReportableRejection", async () => {
224
+ const result = await createApi().draft({ transactionIds: ["transfer-3"] });
225
+ expect(getRejection(result)).toMatchObject({
226
+ __typename: "DisputeCaseTransactionsNotReportableRejection",
227
+ transactions: [{ transactionId: "transfer-3", reason: "TransactionStatusNotReportable" }],
228
+ });
229
+ });
230
+ test("DisputeCaseMultipleAccountsRejection", async () => {
231
+ const result = await createApi().draft({ transactionIds: ["transfer-1", "transfer-2"] });
232
+ expect(getRejection(result)).toMatchObject({
233
+ __typename: "DisputeCaseMultipleAccountsRejection",
234
+ accountIds: ["account-1", "account-2"],
235
+ });
236
+ });
237
+ test("DisputeCaseMultipleCardHoldersRejection", async () => {
238
+ const result = await createApi().draft({ transactionIds: ["card-1", "card-3"] });
239
+ expect(getRejection(result)).toMatchObject({
240
+ __typename: "DisputeCaseMultipleCardHoldersRejection",
241
+ accountMembershipIds: ["member-1", "member-2"],
242
+ });
243
+ });
244
+ test("DisputeCaseTransactionsOutsideReportingWindowRejection", async () => {
245
+ const result = await createApi().draft({ transactionIds: ["transfer-4"] });
246
+ expect(getRejection(result)).toMatchObject({
247
+ __typename: "DisputeCaseTransactionsOutsideReportingWindowRejection",
248
+ transactions: [
249
+ { transactionId: "transfer-4", reportingWindowEndedAt: "2026-10-01T00:00:00.000Z" },
250
+ ],
251
+ });
252
+ });
253
+ test("DisputeCaseDetailsNotApplicableRejection", async () => {
254
+ const api = createApi();
255
+ expect(getRejection(await api.draft({
256
+ transactionIds: ["card-1"],
257
+ fraudTransferDetails: { hasContactedBankOrBeneficiary: true },
258
+ }))).toMatchObject({
259
+ __typename: "DisputeCaseDetailsNotApplicableRejection",
260
+ fields: [{ path: ["fraudTransferDetails"] }],
261
+ });
262
+ expect(getRejection(await api.draft({
263
+ transactionIds: ["transfer-1"],
264
+ fraudCardDetails: { isLostOrStolen: true },
265
+ }))).toMatchObject({ fields: [{ path: ["fraudCardDetails"] }] });
266
+ expect(getRejection(await api.draft({ transactionIds: ["card-1"], victimAccountMembershipId: "member-1" }))).toMatchObject({ fields: [{ path: ["victimAccountMembershipId"] }] });
267
+ });
268
+ test("AccountMembershipNotFoundRejection", async () => {
269
+ const result = await createApi().draft({
270
+ transactionIds: ["transfer-1"],
271
+ victimAccountMembershipId: "member-other",
272
+ });
273
+ expect(getRejection(result)).toMatchObject({
274
+ __typename: "AccountMembershipNotFoundRejection",
275
+ id: "member-other",
276
+ });
277
+ });
278
+ test("a rejected call changes nothing", async () => {
279
+ const api = createApi();
280
+ const created = await draft(api, { transactionIds: ["transfer-1"], fraudDetails });
281
+ await api.update({
282
+ disputeCaseId: created.id,
283
+ fraudDetails: null,
284
+ transactionIds: ["unknown"],
285
+ });
286
+ expect(getOk(await api.getCase({ disputeCaseId: created.id }))).toEqual(Option.Some(created));
287
+ });
288
+ });
289
+ describe("submit and cancel rejections", () => {
290
+ test("DisputeCaseIncompleteRejection: fraud details", async () => {
291
+ const api = createApi();
292
+ const created = await draft(api, { transactionIds: ["card-1"] });
293
+ expect(getRejection(await api.submit({ disputeCaseId: created.id }))).toMatchObject({
294
+ __typename: "DisputeCaseIncompleteRejection",
295
+ missingFields: [{ path: ["fraudDetails"] }],
296
+ });
297
+ });
298
+ test("DisputeCaseIncompleteRejection: victim on a transfer-only case", async () => {
299
+ const api = createApi();
300
+ const created = await draft(api, { transactionIds: ["transfer-1"], fraudDetails });
301
+ expect(getRejection(await api.submit({ disputeCaseId: created.id }))).toMatchObject({
302
+ __typename: "DisputeCaseIncompleteRejection",
303
+ missingFields: [{ path: ["victimAccountMembershipId"] }],
304
+ });
305
+ });
306
+ test("DisputeCaseTransactionsAlreadyReportedRejection", async () => {
307
+ const api = createApi({ alreadyReported: [{ transactionId: "card-2", disputeCaseId: null }] });
308
+ const first = await draft(api, { transactionIds: ["card-1"], fraudDetails });
309
+ const second = await draft(api, { transactionIds: ["card-1", "card-2"], fraudDetails });
310
+ getOk(await api.submit({ disputeCaseId: first.id }));
311
+ expect(getRejection(await api.submit({ disputeCaseId: second.id }))).toMatchObject({
312
+ __typename: "DisputeCaseTransactionsAlreadyReportedRejection",
313
+ transactions: [
314
+ { transactionId: "card-1", disputeCaseId: first.id },
315
+ { transactionId: "card-2", disputeCaseId: null },
316
+ ],
317
+ });
318
+ });
319
+ test("DisputeCaseTransactionsNotReportableRejection and OutsideReportingWindow on seeded drafts", async () => {
320
+ const seeded = await draft(createApi(), { transactionIds: ["transfer-1"], fraudDetails });
321
+ const notReportableCase = {
322
+ ...seeded,
323
+ id: "case-1",
324
+ transactions: [{ id: "transfer-3", rail: "Transfer" }],
325
+ };
326
+ const outsideCase = {
327
+ ...seeded,
328
+ id: "case-2",
329
+ transactions: [{ id: "transfer-4", rail: "Transfer" }],
330
+ };
331
+ const api = createApi({ cases: [notReportableCase, outsideCase] });
332
+ expect(getRejection(await api.submit({ disputeCaseId: "case-1" }))).toMatchObject({
333
+ __typename: "DisputeCaseTransactionsNotReportableRejection",
334
+ });
335
+ expect(getRejection(await api.submit({ disputeCaseId: "case-2" }))).toMatchObject({
336
+ __typename: "DisputeCaseTransactionsOutsideReportingWindowRejection",
337
+ });
338
+ });
339
+ test("DisputeCaseNotFoundRejection and DisputeCaseWrongStatusRejection", async () => {
340
+ const api = createApi();
341
+ expect(getRejection(await api.submit({ disputeCaseId: "unknown" }))).toMatchObject({
342
+ __typename: "DisputeCaseNotFoundRejection",
343
+ });
344
+ expect(getRejection(await api.cancel({ disputeCaseId: "unknown" }))).toMatchObject({
345
+ __typename: "DisputeCaseNotFoundRejection",
346
+ });
347
+ const created = await draft(api, { transactionIds: ["card-1"] });
348
+ await api.cancel({ disputeCaseId: created.id });
349
+ expect(getRejection(await api.submit({ disputeCaseId: created.id }))).toMatchObject({
350
+ __typename: "DisputeCaseWrongStatusRejection",
351
+ currentStatus: "Canceled",
352
+ });
353
+ const submittedCase = await draft(api, { transactionIds: ["card-2"], fraudDetails });
354
+ await api.submit({ disputeCaseId: submittedCase.id });
355
+ expect(getRejection(await api.cancel({ disputeCaseId: submittedCase.id }))).toMatchObject({
356
+ __typename: "DisputeCaseWrongStatusRejection",
357
+ currentStatus: "Submitted",
358
+ expectedStatuses: ["Draft"],
359
+ });
360
+ });
361
+ });
362
+ describe("check order", () => {
363
+ const precedence = [
364
+ [
365
+ "limit before not found",
366
+ {},
367
+ { transactionIds: Array.from({ length: 101 }, (_, index) => `unknown-${index}`) },
368
+ "DisputeCaseTransactionsLimitExceededRejection",
369
+ ],
370
+ [
371
+ "not found before not reportable",
372
+ {},
373
+ { transactionIds: ["unknown", "transfer-3"] },
374
+ "DisputeCaseTransactionsNotFoundRejection",
375
+ ],
376
+ [
377
+ "not reportable before multiple accounts",
378
+ {},
379
+ { transactionIds: ["transfer-3", "transfer-2"] },
380
+ "DisputeCaseTransactionsNotReportableRejection",
381
+ ],
382
+ [
383
+ "multiple cardholders before outside reporting window",
384
+ {},
385
+ { transactionIds: ["card-1", "card-3", "transfer-4"] },
386
+ "DisputeCaseMultipleCardHoldersRejection",
387
+ ],
388
+ [
389
+ "outside reporting window before details not applicable",
390
+ {},
391
+ { transactionIds: ["transfer-4"], fraudCardDetails: { isLostOrStolen: true } },
392
+ "DisputeCaseTransactionsOutsideReportingWindowRejection",
393
+ ],
394
+ [
395
+ "details not applicable before account membership not found",
396
+ {},
397
+ {
398
+ transactionIds: ["transfer-1"],
399
+ fraudCardDetails: { isLostOrStolen: true },
400
+ victimAccountMembershipId: "member-other",
401
+ },
402
+ "DisputeCaseDetailsNotApplicableRejection",
403
+ ],
404
+ ];
405
+ test.each(precedence)("draft: %s", async (_, config, input, expected) => {
406
+ var _a;
407
+ expect((_a = getRejection(await createApi(config).draft(input))) === null || _a === void 0 ? void 0 : _a.__typename).toBe(expected);
408
+ });
409
+ test("update: malformed input before case not found", async () => {
410
+ const result = await createApi().update({ disputeCaseId: "unknown", transactionIds: [] });
411
+ expect(result.match({ Ok: () => undefined, Error: error => error.kind })).toBe("error");
412
+ });
413
+ test("submit: outside reporting window before already reported, already reported before incomplete", async () => {
414
+ var _a, _b;
415
+ const seeded = await draft(createApi(), { transactionIds: ["transfer-1"] });
416
+ const outsideAndReported = {
417
+ ...seeded,
418
+ id: "case-1",
419
+ transactions: [{ id: "transfer-4", rail: "Transfer" }],
420
+ };
421
+ const api = createApi({
422
+ cases: [outsideAndReported, { ...seeded, id: "case-2" }],
423
+ alreadyReported: [
424
+ { transactionId: "transfer-4", disputeCaseId: null },
425
+ { transactionId: "transfer-1", disputeCaseId: null },
426
+ ],
427
+ });
428
+ expect((_a = getRejection(await api.submit({ disputeCaseId: "case-1" }))) === null || _a === void 0 ? void 0 : _a.__typename).toBe("DisputeCaseTransactionsOutsideReportingWindowRejection");
429
+ expect((_b = getRejection(await api.submit({ disputeCaseId: "case-2" }))) === null || _b === void 0 ? void 0 : _b.__typename).toBe("DisputeCaseTransactionsAlreadyReportedRejection");
430
+ });
431
+ test("submit: DisputeCaseTransactionsNotFoundRejection when a transaction can no longer be reported", async () => {
432
+ const seeded = await draft(createApi(), { transactionIds: ["transfer-1"], fraudDetails });
433
+ const api = createApi({
434
+ cases: [{ ...seeded, transactions: [{ id: "gone", rail: "Transfer" }] }],
435
+ });
436
+ expect(getRejection(await api.submit({ disputeCaseId: seeded.id }))).toMatchObject({
437
+ __typename: "DisputeCaseTransactionsNotFoundRejection",
438
+ transactionIds: ["gone"],
439
+ });
440
+ });
441
+ });
442
+ describe("details and victim after a transaction change", () => {
443
+ test("transfer → card → transfer doesn't bring the victim back", async () => {
444
+ const api = createApi();
445
+ const created = await draft(api, {
446
+ transactionIds: ["transfer-1"],
447
+ victimAccountMembershipId: "member-2",
448
+ });
449
+ await api.update({ disputeCaseId: created.id, transactionIds: ["card-1"] });
450
+ const back = getOk(await api.update({ disputeCaseId: created.id, transactionIds: ["transfer-1"] }));
451
+ expect(back.hasVictim).toBe(false);
452
+ expect(back.victimAccountMembershipId).toBeNull();
453
+ });
454
+ test("moving to another account removes the victim", async () => {
455
+ const api = createApi({
456
+ transactions: [transfer1, { ...transfer1, id: "transfer-5", accountId: "account-2" }],
457
+ });
458
+ const created = await draft(api, {
459
+ transactionIds: ["transfer-1"],
460
+ victimAccountMembershipId: "member-2",
461
+ });
462
+ const moved = getOk(await api.update({ disputeCaseId: created.id, transactionIds: ["transfer-5"] }));
463
+ expect(moved.hasVictim).toBe(false);
464
+ });
465
+ test("a victim sent with new transactions is kept", async () => {
466
+ const api = createApi({
467
+ transactions: [transfer1, { ...transfer1, id: "transfer-5", accountId: "account-2" }],
468
+ });
469
+ const created = await draft(api, {
470
+ transactionIds: ["transfer-1"],
471
+ victimAccountMembershipId: "member-2",
472
+ });
473
+ const moved = getOk(await api.update({
474
+ disputeCaseId: created.id,
475
+ transactionIds: ["transfer-5"],
476
+ victimAccountMembershipId: "member-other",
477
+ }));
478
+ expect(moved.victimAccountMembershipId).toBe("member-other");
479
+ });
480
+ test("removing card transactions removes card details", async () => {
481
+ const api = createApi();
482
+ const created = await draft(api, {
483
+ transactionIds: ["card-1", "transfer-1"],
484
+ fraudCardDetails: { isLostOrStolen: true },
485
+ });
486
+ expect(created.transactions).toEqual([
487
+ { id: "card-1", rail: "Card" },
488
+ { id: "transfer-1", rail: "Transfer" },
489
+ ]);
490
+ const updated = getOk(await api.update({ disputeCaseId: created.id, transactionIds: ["transfer-1"] }));
491
+ expect(updated.fraudCardDetails).toBeNull();
492
+ });
493
+ });
494
+ describe("expiry", () => {
495
+ test("a draft expires at the end of its oldest transaction's reporting window", async () => {
496
+ let now = NOW;
497
+ const api = createApi({
498
+ transactions: [{ ...transfer1, reportingWindowEndsAt: "2026-12-01T00:00:00.000Z" }],
499
+ now: () => now,
500
+ });
501
+ const created = await draft(api, { transactionIds: ["transfer-1"] });
502
+ expect(created.statusInfo).toEqual({ status: "Draft", expiresAt: "2026-12-01T00:00:00.000Z" });
503
+ now = new Date("2026-12-02T00:00:00.000Z");
504
+ const expired = getOk(await api.getCase({ disputeCaseId: created.id })).toUndefined();
505
+ expect(expired === null || expired === void 0 ? void 0 : expired.statusInfo).toEqual({
506
+ status: "Expired",
507
+ expiredAt: "2026-12-01T00:00:00.000Z",
508
+ });
509
+ expect(getRejection(await api.update({ disputeCaseId: created.id, fraudDetails }))).toMatchObject({
510
+ __typename: "DisputeCaseWrongStatusRejection",
511
+ currentStatus: "Expired",
512
+ });
513
+ });
514
+ });
515
+ describe("more precedence", () => {
516
+ test("draft: multiple accounts before multiple cardholders", async () => {
517
+ var _a;
518
+ const api = createApi({
519
+ transactions: [
520
+ card1,
521
+ {
522
+ ...card1,
523
+ id: "card-9",
524
+ accountId: "account-2",
525
+ cardholderAccountMembershipId: "member-other",
526
+ },
527
+ ],
528
+ });
529
+ expect((_a = getRejection(await api.draft({ transactionIds: ["card-1", "card-9"] }))) === null || _a === void 0 ? void 0 : _a.__typename).toBe("DisputeCaseMultipleAccountsRejection");
530
+ });
531
+ test("update: case not found and wrong status before transaction checks", async () => {
532
+ var _a, _b;
533
+ const api = createApi();
534
+ expect((_a = getRejection(await api.update({ disputeCaseId: "unknown", transactionIds: ["unknown"] }))) === null || _a === void 0 ? void 0 : _a.__typename).toBe("DisputeCaseNotFoundRejection");
535
+ const created = await draft(api, { transactionIds: ["card-1"] });
536
+ await api.cancel({ disputeCaseId: created.id });
537
+ expect((_b = getRejection(await api.update({ disputeCaseId: created.id, transactionIds: ["unknown"] }))) === null || _b === void 0 ? void 0 : _b.__typename).toBe("DisputeCaseWrongStatusRejection");
538
+ });
539
+ test("submit: not found before not reportable, not reportable before outside reporting window", async () => {
540
+ var _a, _b;
541
+ const seeded = await draft(createApi(), { transactionIds: ["transfer-1"], fraudDetails });
542
+ const api = createApi({
543
+ cases: [
544
+ {
545
+ ...seeded,
546
+ id: "case-1",
547
+ transactions: [
548
+ { id: "gone", rail: "Transfer" },
549
+ { id: "transfer-3", rail: "Transfer" },
550
+ ],
551
+ },
552
+ {
553
+ ...seeded,
554
+ id: "case-2",
555
+ transactions: [
556
+ { id: "transfer-3", rail: "Transfer" },
557
+ { id: "transfer-4", rail: "Transfer" },
558
+ ],
559
+ },
560
+ ],
561
+ });
562
+ expect((_a = getRejection(await api.submit({ disputeCaseId: "case-1" }))) === null || _a === void 0 ? void 0 : _a.__typename).toBe("DisputeCaseTransactionsNotFoundRejection");
563
+ expect((_b = getRejection(await api.submit({ disputeCaseId: "case-2" }))) === null || _b === void 0 ? void 0 : _b.__typename).toBe("DisputeCaseTransactionsNotReportableRejection");
564
+ });
565
+ });
566
+ describe("left-out fields are kept", () => {
567
+ const transfer6 = { ...transfer1, id: "transfer-6" };
568
+ test("adding a transfer keeps the victim and transfer details", async () => {
569
+ const api = createApi({ transactions: [transfer1, transfer6] });
570
+ const created = await draft(api, {
571
+ transactionIds: ["transfer-1"],
572
+ victimAccountMembershipId: "member-2",
573
+ fraudTransferDetails: { hasContactedBankOrBeneficiary: true },
574
+ });
575
+ const updated = getOk(await api.update({ disputeCaseId: created.id, transactionIds: ["transfer-1", "transfer-6"] }));
576
+ expect(updated.victimAccountMembershipId).toBe("member-2");
577
+ expect(updated.fraudTransferDetails).toEqual({ hasContactedBankOrBeneficiary: true });
578
+ const referenced = getOk(await api.update({ disputeCaseId: created.id, externalReference: "ref" }));
579
+ expect(referenced.hasVictim).toBe(true);
580
+ });
581
+ test("adding a card transaction keeps card details", async () => {
582
+ const api = createApi();
583
+ const created = await draft(api, {
584
+ transactionIds: ["card-1"],
585
+ fraudCardDetails: { hasContactedMerchant: true },
586
+ });
587
+ const updated = getOk(await api.update({ disputeCaseId: created.id, transactionIds: ["card-1", "card-2"] }));
588
+ expect(updated.fraudCardDetails).toEqual({
589
+ isLostOrStolen: null,
590
+ lostOrStolenNoticedAt: null,
591
+ hasContactedMerchant: true,
592
+ });
593
+ });
594
+ test("a victim the caller can't see stays on the case", async () => {
595
+ const seeded = await draft(createApi(), {
596
+ transactionIds: ["transfer-1"],
597
+ victimAccountMembershipId: "member-2",
598
+ });
599
+ const api = createApi({ cases: [{ ...seeded, victimAccountMembershipId: null }] });
600
+ const updated = getOk(await api.update({ disputeCaseId: seeded.id, externalReference: "ref" }));
601
+ expect(updated.hasVictim).toBe(true);
602
+ expect(updated.victimAccountMembershipId).toBeNull();
603
+ });
604
+ });
605
+ test("findDrafts returns the newest draft first", async () => {
606
+ const seeded = await draft(createApi(), { transactionIds: ["card-1"] });
607
+ const newer = { ...seeded, id: "newer", createdAt: "2026-11-19T00:00:00.000Z" };
608
+ const older = { ...seeded, id: "older", createdAt: "2026-11-01T00:00:00.000Z" };
609
+ const api = createApi({ cases: [newer, older] });
610
+ const drafts = getOk(await api.findDrafts({ accountId: "account-1", transactionIds: ["card-1"] }));
611
+ expect(drafts.map(item => item.id)).toEqual(["newer", "older"]);
612
+ });
613
+ describe("dates", () => {
614
+ const later = (days) => new Date(NOW.getTime() + days * 24 * 60 * 60 * 1000);
615
+ test("submitting or canceling again doesn't change the case", async () => {
616
+ let now = NOW;
617
+ const api = createApi({ now: () => now });
618
+ const toSubmit = await draft(api, { transactionIds: ["card-1"], fraudDetails });
619
+ const toCancel = await draft(api, { transactionIds: ["card-2"] });
620
+ const submitted = getOk(await api.submit({ disputeCaseId: toSubmit.id }));
621
+ const canceled = getOk(await api.cancel({ disputeCaseId: toCancel.id }));
622
+ now = later(1);
623
+ expect(getOk(await api.submit({ disputeCaseId: toSubmit.id }))).toEqual(submitted);
624
+ expect(getOk(await api.cancel({ disputeCaseId: toCancel.id }))).toEqual(canceled);
625
+ });
626
+ test("a draft expires 30 days after its last update", async () => {
627
+ var _a;
628
+ let now = NOW;
629
+ const api = createApi({ now: () => now });
630
+ const created = await draft(api, { transactionIds: ["transfer-1"] });
631
+ now = later(10);
632
+ const updated = getOk(await api.update({ disputeCaseId: created.id, fraudDetails }));
633
+ expect(updated.statusInfo).toEqual({ status: "Draft", expiresAt: later(40).toISOString() });
634
+ now = later(40);
635
+ expect((_a = getOk(await api.getCase({ disputeCaseId: created.id })).toUndefined()) === null || _a === void 0 ? void 0 : _a.statusInfo).toEqual({
636
+ status: "Expired",
637
+ expiredAt: later(40).toISOString(),
638
+ });
639
+ expect(getOk(await api.findDrafts({ accountId: "account-1", transactionIds: ["transfer-1"] }))).toEqual([]);
640
+ expect(getRejection(await api.update({ disputeCaseId: created.id, fraudDetails }))).toMatchObject({
641
+ __typename: "DisputeCaseWrongStatusRejection",
642
+ currentStatus: "Expired",
643
+ });
644
+ });
645
+ });
646
+ test("null clears the victim", async () => {
647
+ const api = createApi();
648
+ const created = await draft(api, {
649
+ transactionIds: ["transfer-1"],
650
+ fraudDetails,
651
+ victimAccountMembershipId: "member-2",
652
+ });
653
+ const cleared = getOk(await api.update({ disputeCaseId: created.id, victimAccountMembershipId: null }));
654
+ expect(cleared.hasVictim).toBe(false);
655
+ expect(getRejection(await api.submit({ disputeCaseId: created.id }))).toMatchObject({
656
+ __typename: "DisputeCaseIncompleteRejection",
657
+ missingFields: [{ path: ["victimAccountMembershipId"] }],
658
+ });
659
+ });
@@ -0,0 +1 @@
1
+ export {};