@swan-io/shared-business 18.4.0 → 18.5.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.
Files changed (65) hide show
  1. package/package.json +3 -3
  2. package/src/components/FraudReport/FraudReportLayout.d.ts +23 -0
  3. package/src/components/FraudReport/FraudReportLayout.js +35 -0
  4. package/src/components/FraudReport/IncidentStep.d.ts +7 -0
  5. package/src/components/FraudReport/IncidentStep.js +55 -0
  6. package/src/components/FraudReport/IntroStep.d.ts +5 -0
  7. package/src/components/FraudReport/IntroStep.js +28 -0
  8. package/src/components/FraudReport/TransactionsStep.d.ts +16 -0
  9. package/src/components/FraudReport/TransactionsStep.js +138 -0
  10. package/src/components/FraudReport/VictimStep.d.ts +9 -0
  11. package/src/components/FraudReport/VictimStep.js +55 -0
  12. package/src/components/InlineDatePicker.js +7 -6
  13. package/src/constants/__tests__/countries.test.js +32 -1
  14. package/src/constants/countries.d.ts +2 -1
  15. package/src/constants/countries.js +36 -2
  16. package/src/hooks/__tests__/useFraudReportFlow.test.d.ts +1 -0
  17. package/src/hooks/__tests__/useFraudReportFlow.test.js +979 -0
  18. package/src/hooks/useFraudReportFlow.d.ts +32 -0
  19. package/src/hooks/useFraudReportFlow.js +260 -0
  20. package/src/locales/de.json +76 -0
  21. package/src/locales/en.json +76 -0
  22. package/src/locales/es.json +76 -0
  23. package/src/locales/fi.json +76 -0
  24. package/src/locales/fr.json +76 -0
  25. package/src/locales/it.json +76 -0
  26. package/src/locales/nl.json +76 -0
  27. package/src/locales/pt.json +76 -0
  28. package/src/utils/__tests__/fraudReport.fakeApi.test.js +42 -13
  29. package/src/utils/__tests__/fraudReport.prefill.test.d.ts +1 -0
  30. package/src/utils/__tests__/fraudReport.prefill.test.js +149 -0
  31. package/src/utils/__tests__/fraudReport.rejections.test.d.ts +1 -0
  32. package/src/utils/__tests__/fraudReport.rejections.test.js +163 -0
  33. package/src/utils/__tests__/fraudReport.resume.test.d.ts +1 -0
  34. package/src/utils/__tests__/fraudReport.resume.test.js +67 -0
  35. package/src/utils/__tests__/fraudReport.stepper.test.d.ts +1 -0
  36. package/src/utils/__tests__/fraudReport.stepper.test.js +102 -0
  37. package/src/utils/__tests__/fraudReport.toInput.test.js +28 -8
  38. package/src/utils/__tests__/fraudReport.transactions.test.d.ts +1 -0
  39. package/src/utils/__tests__/fraudReport.transactions.test.js +28 -0
  40. package/src/utils/__tests__/fraudReport.types.test.d.ts +1 -0
  41. package/src/utils/__tests__/fraudReport.types.test.js +22 -0
  42. package/src/utils/__tests__/fraudReport.victim.test.d.ts +1 -0
  43. package/src/utils/__tests__/fraudReport.victim.test.js +142 -0
  44. package/src/utils/__tests__/phone.test.js +60 -5
  45. package/src/utils/fraudReport/fakeApi.d.ts +7 -4
  46. package/src/utils/fraudReport/fakeApi.js +18 -5
  47. package/src/utils/fraudReport/prefill.d.ts +3 -0
  48. package/src/utils/fraudReport/prefill.js +14 -0
  49. package/src/utils/fraudReport/rejections.d.ts +10 -0
  50. package/src/utils/fraudReport/rejections.js +53 -0
  51. package/src/utils/fraudReport/resume.d.ts +4 -0
  52. package/src/utils/fraudReport/resume.js +26 -0
  53. package/src/utils/fraudReport/stepper.d.ts +16 -0
  54. package/src/utils/fraudReport/stepper.js +21 -0
  55. package/src/utils/fraudReport/toInput.d.ts +9 -0
  56. package/src/utils/fraudReport/toInput.js +20 -12
  57. package/src/utils/fraudReport/transactions.d.ts +6 -0
  58. package/src/utils/fraudReport/transactions.js +7 -0
  59. package/src/utils/fraudReport/types.d.ts +8 -0
  60. package/src/utils/fraudReport/validation.d.ts +1 -0
  61. package/src/utils/fraudReport/validation.js +1 -0
  62. package/src/utils/fraudReport/victim.d.ts +30 -0
  63. package/src/utils/fraudReport/victim.js +28 -0
  64. package/src/utils/phone.d.ts +1 -0
  65. package/src/utils/phone.js +31 -4
@@ -1,13 +1,13 @@
1
1
  import { Option } from "@bloodyowl/boxed";
2
2
  import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
3
- import { createFakeFraudReportApi } from "../fraudReport/fakeApi";
3
+ import { createFakeFraudReportApi, createFakeTransaction } from "../fraudReport/fakeApi";
4
4
  import { getCaseRail, getTransactionRail } from "../fraudReport/rail";
5
5
  import { toDraftInput, toUpdateInput } from "../fraudReport/toInput";
6
- import { validateDescription, validateDiscoveryDetails, validateExternalReference, validateNotInFuture, validateSharedInformation, validateTransactionIds, } from "../fraudReport/validation";
7
- const card1 = { id: "card-1", rail: "Card" };
8
- const card2 = { id: "card-2", rail: "Card" };
9
- const transfer1 = { id: "transfer-1", rail: "Transfer" };
10
- const transfer2 = { id: "transfer-2", rail: "Transfer" };
6
+ import { validateDescription, validateDiscoveryDetails, validateExternalReference, validateNotInFuture, validateSelected, validateSharedInformation, validateTransactionIds, } from "../fraudReport/validation";
7
+ const card1 = createFakeTransaction("card-1", "CardOutDebit");
8
+ const card2 = createFakeTransaction("card-2", "CardOutDebit");
9
+ const transfer1 = createFakeTransaction("transfer-1", "SepaCreditTransferOut");
10
+ const transfer2 = createFakeTransaction("transfer-2", "SepaCreditTransferOut");
11
11
  const DESCRIPTION_REGEX = /^(?=[\s\S]*\S)[\s\S]{1,2000}$/;
12
12
  const DISCOVERY_DETAILS_REGEX = /^(?=[\s\S]*\S)[\s\S]{1,1000}$/;
13
13
  const EXTERNAL_REFERENCE_REGEX = /^[\x20-\x7E]{1,255}$/;
@@ -92,6 +92,14 @@ describe("validateTransactionIds", () => {
92
92
  expect(validateTransactionIds(["card-1", "transfer-1"])).toBeUndefined();
93
93
  });
94
94
  });
95
+ describe("validateSelected", () => {
96
+ test("requires a value", () => {
97
+ expect(validateSelected(undefined)).toBe("Required");
98
+ });
99
+ test("accepts a selected value", () => {
100
+ expect(validateSelected("Manipulated")).toBeUndefined();
101
+ });
102
+ });
95
103
  describe("validateNotInFuture", () => {
96
104
  beforeEach(() => {
97
105
  vi.useFakeTimers();
@@ -385,6 +393,18 @@ describe("toUpdateInput", () => {
385
393
  savedTransactionIds: [],
386
394
  })).not.toHaveProperty("fraudTransferDetails");
387
395
  });
396
+ test("clears the fraudster answers once no fraudster convinced the user", () => {
397
+ expect(toUpdateInput({
398
+ savedCase,
399
+ values: { ...filled, reportedReason: "Unauthorized", transactions: [card1] },
400
+ savedTransactionIds: ["card-1"],
401
+ }).fraudDetails).toStrictEqual({
402
+ ...fraudDetails,
403
+ reportedReason: "Unauthorized",
404
+ fraudsterChannels: null,
405
+ sharedInformationWithFraudster: null,
406
+ });
407
+ });
388
408
  test("leaves the transaction IDs out when the selection didn't change", () => {
389
409
  expect(toUpdateInput({
390
410
  savedCase,
@@ -449,10 +469,10 @@ describe("inputs sent to the API", () => {
449
469
  {
450
470
  id: "card-1",
451
471
  accountId: "account-1",
452
- rail: "Card",
472
+ type: "CardOutDebit",
453
473
  cardholderAccountMembershipId: "member-2",
454
474
  },
455
- { id: "transfer-1", accountId: "account-1", rail: "Transfer" },
475
+ { id: "transfer-1", accountId: "account-1", type: "SepaCreditTransferOut" },
456
476
  ],
457
477
  accountMemberships: [
458
478
  { id: "member-1", accountId: "account-1" },
@@ -0,0 +1,28 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { createFakeTransaction } from "../fraudReport/fakeApi";
3
+ import { getTransactionsTotal, removeDuplicateTransactions } from "../fraudReport/transactions";
4
+ const withAmount = (value, currency = "EUR") => createFakeTransaction(`card-${value}`, "CardOutDebit", { amount: { value, currency } });
5
+ describe("getTransactionsTotal", () => {
6
+ test("adds the amounts without rounding drift", () => {
7
+ expect(getTransactionsTotal([withAmount("1.15"), withAmount("2.30")])).toEqual({
8
+ value: 3.45,
9
+ currency: "EUR",
10
+ });
11
+ });
12
+ test("uses the first transaction's currency, while mixed currencies are pending Product", () => {
13
+ expect(getTransactionsTotal([withAmount("5.00", "GBP"), withAmount("1.00")])).toEqual({
14
+ value: 6,
15
+ currency: "GBP",
16
+ });
17
+ });
18
+ test("is zero euros when nothing is selected", () => {
19
+ expect(getTransactionsTotal([])).toEqual({ value: 0, currency: "EUR" });
20
+ });
21
+ });
22
+ describe("removeDuplicateTransactions", () => {
23
+ test("keeps the first of rows with the same ID, in order", () => {
24
+ const card = createFakeTransaction("card-1", "CardOutDebit");
25
+ const transfer = createFakeTransaction("transfer-1", "SepaCreditTransferOut");
26
+ expect(removeDuplicateTransactions([card, transfer, { ...card, counterparty: "Lidl" }])).toEqual([card, transfer]);
27
+ });
28
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { isTranslationKey } from "../i18n";
3
+ // A Record, so a rejection added to the contract fails typecheck until it is listed here
4
+ const rejectionTypenames = {
5
+ AccountMembershipNotFoundRejection: true,
6
+ DisputeCaseDetailsNotApplicableRejection: true,
7
+ DisputeCaseIncompleteRejection: true,
8
+ DisputeCaseMultipleAccountsRejection: true,
9
+ DisputeCaseMultipleCardHoldersRejection: true,
10
+ DisputeCaseNotFoundRejection: true,
11
+ DisputeCaseTransactionsAlreadyReportedRejection: true,
12
+ DisputeCaseTransactionsLimitExceededRejection: true,
13
+ DisputeCaseTransactionsNotFoundRejection: true,
14
+ DisputeCaseTransactionsNotReportableRejection: true,
15
+ DisputeCaseTransactionsOutsideReportingWindowRejection: true,
16
+ DisputeCaseWrongStatusRejection: true,
17
+ };
18
+ describe("fraud report rejections", () => {
19
+ test.each(Object.keys(rejectionTypenames))("%s has a translation key", typename => {
20
+ expect(isTranslationKey(`rejection.${typename}`)).toBe(true);
21
+ });
22
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,142 @@
1
+ import { Option } from "@bloodyowl/boxed";
2
+ import { describe, expect, test } from "vitest";
3
+ import { getInitialVictimChoice, getOtherAccountMemberships, getSelectedAccountMembershipId, getVictimAccountMembershipId, } from "../fraudReport/victim";
4
+ const julia = { id: "member-1", name: "Julia Fox" };
5
+ const nicolas = { id: "member-2", name: "Nicolas Saison" };
6
+ const ada = { id: "member-3", name: "Ada Lovelace" };
7
+ const accountMemberships = [julia, nicolas, ada];
8
+ const asMember = Option.Some(julia);
9
+ const asOperator = Option.None();
10
+ const choiceOf = (answer, selectedId) => ({
11
+ answer: Option.fromUndefined(answer),
12
+ selectedId: Option.fromUndefined(selectedId),
13
+ });
14
+ describe("getOtherAccountMemberships", () => {
15
+ test("leaves the current member out of the picker", () => {
16
+ expect(getOtherAccountMemberships({ currentAccountMembership: asMember, accountMemberships })).toEqual([nicolas, ada]);
17
+ });
18
+ test("lists every member for an operator", () => {
19
+ expect(getOtherAccountMemberships({ currentAccountMembership: asOperator, accountMemberships })).toEqual(accountMemberships);
20
+ });
21
+ });
22
+ describe("getInitialVictimChoice", () => {
23
+ test.each([
24
+ {
25
+ name: "nothing chosen yet",
26
+ victimAccountMembershipId: undefined,
27
+ answer: undefined,
28
+ victim: undefined,
29
+ },
30
+ {
31
+ name: "the member themselves",
32
+ victimAccountMembershipId: "member-1",
33
+ answer: "Myself",
34
+ victim: "member-1",
35
+ },
36
+ {
37
+ name: "another member",
38
+ victimAccountMembershipId: "member-2",
39
+ answer: "SomeoneElse",
40
+ victim: "member-2",
41
+ },
42
+ {
43
+ name: "a member no longer listed",
44
+ victimAccountMembershipId: "member-9",
45
+ answer: "SomeoneElse",
46
+ victim: undefined,
47
+ },
48
+ ])("gives back $name when a member comes back", ({ victimAccountMembershipId, answer, victim }) => {
49
+ const choice = getInitialVictimChoice({
50
+ currentAccountMembership: asMember,
51
+ victimAccountMembershipId,
52
+ });
53
+ expect(choice.answer.toUndefined()).toBe(answer);
54
+ expect(getVictimAccountMembershipId({
55
+ currentAccountMembership: asMember,
56
+ accountMemberships,
57
+ choice,
58
+ }).toUndefined()).toBe(victim);
59
+ });
60
+ test.each([
61
+ { name: "nothing chosen yet", victimAccountMembershipId: undefined, victim: undefined },
62
+ { name: "a listed member", victimAccountMembershipId: "member-1", victim: "member-1" },
63
+ { name: "a member no longer listed", victimAccountMembershipId: "member-9", victim: undefined },
64
+ ])("gives back $name when an operator comes back, with no answer", ({ victimAccountMembershipId, victim }) => {
65
+ const choice = getInitialVictimChoice({
66
+ currentAccountMembership: asOperator,
67
+ victimAccountMembershipId,
68
+ });
69
+ expect(choice.answer.isNone()).toBe(true);
70
+ expect(getVictimAccountMembershipId({
71
+ currentAccountMembership: asOperator,
72
+ accountMemberships,
73
+ choice,
74
+ }).toUndefined()).toBe(victim);
75
+ });
76
+ });
77
+ describe("getSelectedAccountMembershipId", () => {
78
+ test.each([
79
+ { name: "another listed member", selectedId: "member-2", shown: "member-2" },
80
+ { name: "the member themselves", selectedId: "member-1", shown: undefined },
81
+ ])("shows $name in a member's picker", ({ selectedId, shown }) => {
82
+ expect(getSelectedAccountMembershipId({
83
+ currentAccountMembership: asMember,
84
+ accountMemberships,
85
+ choice: choiceOf("SomeoneElse", selectedId),
86
+ }).toUndefined()).toBe(shown);
87
+ });
88
+ test("drops a pick once the member leaves the list", () => {
89
+ const choice = choiceOf("SomeoneElse", "member-2");
90
+ expect(getSelectedAccountMembershipId({
91
+ currentAccountMembership: asMember,
92
+ accountMemberships,
93
+ choice,
94
+ }).toUndefined()).toBe("member-2");
95
+ expect(getSelectedAccountMembershipId({
96
+ currentAccountMembership: asMember,
97
+ accountMemberships: [julia, ada],
98
+ choice,
99
+ }).toUndefined()).toBeUndefined();
100
+ });
101
+ });
102
+ describe("getVictimAccountMembershipId", () => {
103
+ test.each([
104
+ { name: "no answer", answer: undefined, selectedId: "member-2", victim: undefined },
105
+ { name: "Myself", answer: "Myself", selectedId: "member-2", victim: "member-1" },
106
+ {
107
+ name: "SomeoneElse with a member picked",
108
+ answer: "SomeoneElse",
109
+ selectedId: "member-2",
110
+ victim: "member-2",
111
+ },
112
+ {
113
+ name: "SomeoneElse with no member picked",
114
+ answer: "SomeoneElse",
115
+ selectedId: undefined,
116
+ victim: undefined,
117
+ },
118
+ {
119
+ name: "SomeoneElse with a member no longer listed",
120
+ answer: "SomeoneElse",
121
+ selectedId: "member-9",
122
+ victim: undefined,
123
+ },
124
+ ])("gives the victim for a member answering $name", ({ answer, selectedId, victim }) => {
125
+ expect(getVictimAccountMembershipId({
126
+ currentAccountMembership: asMember,
127
+ accountMemberships,
128
+ choice: choiceOf(answer, selectedId),
129
+ }).toUndefined()).toBe(victim);
130
+ });
131
+ test.each([
132
+ { name: "a member picked", selectedId: "member-2", victim: "member-2" },
133
+ { name: "no member picked", selectedId: undefined, victim: undefined },
134
+ { name: "a member no longer listed", selectedId: "member-9", victim: undefined },
135
+ ])("gives the victim for an operator with $name", ({ selectedId, victim }) => {
136
+ expect(getVictimAccountMembershipId({
137
+ currentAccountMembership: asOperator,
138
+ accountMemberships,
139
+ choice: choiceOf(undefined, selectedId),
140
+ }).toUndefined()).toBe(victim);
141
+ });
142
+ });
@@ -1,7 +1,6 @@
1
- import { afterEach, describe, expect, it, vi } from "vitest";
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
2
  import { france, getCountryByCCA2, getCountryByCCA3 } from "../../constants/countries";
3
- import { getMostLikelyUserCountry } from "../localization";
4
- import { maskPhoneNumber, parsePhoneNumber, prefixPhoneNumber } from "../phone";
3
+ import { getDefaultPhoneCountry, maskPhoneNumber, parsePhoneNumber, prefixPhoneNumber, } from "../phone";
5
4
  const gambia = getCountryByCCA2("GM");
6
5
  const jamaica = getCountryByCCA2("JM");
7
6
  const curacao = getCountryByCCA2("CW");
@@ -51,8 +50,11 @@ describe("parsePhoneNumber splits an international number into its country and n
51
50
  });
52
51
  });
53
52
  });
54
- describe("parsePhoneNumber falls back to the most likely user country and an empty national number", () => {
55
- const fallback = { country: getMostLikelyUserCountry(), nationalNumber: "" };
53
+ describe("parsePhoneNumber falls back to the default phone country and an empty national number", () => {
54
+ const fallback = { country: france, nationalNumber: "" };
55
+ beforeEach(() => {
56
+ vi.stubGlobal("navigator", { languages: ["en-GB"] });
57
+ });
56
58
  afterEach(() => {
57
59
  vi.unstubAllGlobals();
58
60
  });
@@ -88,6 +90,59 @@ describe("parsePhoneNumber falls back to the most likely user country and an emp
88
90
  });
89
91
  });
90
92
  });
93
+ describe("getDefaultPhoneCountry picks one of our markets from the browser languages", () => {
94
+ const germany = getCountryByCCA2("DE");
95
+ const belgium = getCountryByCCA2("BE");
96
+ afterEach(() => {
97
+ vi.unstubAllGlobals();
98
+ });
99
+ it.each([[["en"]], [["en-GB"]], [["en-US"]], [["ja-JP"]], [[]]])("falls back to France for %j", languages => {
100
+ expect(getDefaultPhoneCountry(languages)).toEqual(france);
101
+ });
102
+ it.each([
103
+ ["fr", "FR"],
104
+ ["de", "DE"],
105
+ ["es", "ES"],
106
+ ["it", "IT"],
107
+ ["nl", "NL"],
108
+ ["pt", "PT"],
109
+ ])("maps the %s language to its country", (language, cca2) => {
110
+ expect(getDefaultPhoneCountry([language])).toEqual(getCountryByCCA2(cca2));
111
+ });
112
+ it.each([
113
+ ["nl-BE", "BE"],
114
+ ["fr-BE", "BE"],
115
+ ["en-BE", "BE"],
116
+ ["en-DE", "DE"],
117
+ ["es-PT", "PT"],
118
+ ])("uses the region of %s when it is one of our markets", (language, cca2) => {
119
+ expect(getDefaultPhoneCountry([language])).toEqual(getCountryByCCA2(cca2));
120
+ });
121
+ it.each([
122
+ ["de-AT", "DE"],
123
+ ["fr-CA", "FR"],
124
+ ["pt-BR", "PT"],
125
+ ])("uses the language of %s when its region isn't one of our markets", (language, cca2) => {
126
+ expect(getDefaultPhoneCountry([language])).toEqual(getCountryByCCA2(cca2));
127
+ });
128
+ it("keeps the browser's order of preference", () => {
129
+ expect(getDefaultPhoneCountry(["de-DE", "fr-FR"])).toEqual(germany);
130
+ expect(getDefaultPhoneCountry(["de", "nl-BE"])).toEqual(germany);
131
+ });
132
+ it("skips languages that don't match one of our markets", () => {
133
+ expect(getDefaultPhoneCountry(["en-US", "nl-BE"])).toEqual(belgium);
134
+ });
135
+ it("matches the language and region case-insensitively", () => {
136
+ expect(getDefaultPhoneCountry(["NL-be"])).toEqual(belgium);
137
+ });
138
+ it("skips invalid language tags", () => {
139
+ expect(getDefaultPhoneCountry(["not a tag", "de"])).toEqual(germany);
140
+ });
141
+ it("reads the browser languages by default", () => {
142
+ vi.stubGlobal("navigator", { languages: ["it-IT"] });
143
+ expect(getDefaultPhoneCountry()).toEqual(getCountryByCCA2("IT"));
144
+ });
145
+ });
91
146
  describe("prefixPhoneNumber turns a national number into an E.164 number", () => {
92
147
  it("prefixes a French national number", () => {
93
148
  expect(prefixPhoneNumber(france, "612345678")).toEqual({
@@ -1,16 +1,18 @@
1
1
  import type { FraudReportApi } from "./api";
2
- import type { DisputeCaseTransactionNotReportableReason, FraudReportCase } from "./types";
2
+ import type { DisputeCaseTransactionNotReportableReason, FraudReportCase, FraudReportTransaction, FraudReportTransactionType } from "./types";
3
+ type FakeTransactionDetails = Partial<Pick<FraudReportTransaction, "executionDate" | "counterparty" | "amount">>;
3
4
  export type FakeTransaction = {
4
5
  id: string;
5
6
  accountId: string;
6
7
  notReportableReason?: DisputeCaseTransactionNotReportableReason;
7
8
  reportingWindowEndsAt?: string;
8
- } & ({
9
- rail: "Card";
9
+ } & FakeTransactionDetails & ({
10
+ type: "CardOutDebit";
10
11
  cardholderAccountMembershipId: string;
11
12
  } | {
12
- rail: "Transfer";
13
+ type: Exclude<FraudReportTransactionType, "CardOutDebit">;
13
14
  });
15
+ export declare const createFakeTransaction: (id: string, type: FraudReportTransactionType, details?: FakeTransactionDetails) => FraudReportTransaction;
14
16
  export type FakeAccountMembership = {
15
17
  id: string;
16
18
  accountId: string;
@@ -27,3 +29,4 @@ export type FakeFraudReportApiConfig = {
27
29
  now?: () => Date;
28
30
  };
29
31
  export declare const createFakeFraudReportApi: (config: FakeFraudReportApiConfig) => FraudReportApi;
32
+ export {};
@@ -1,5 +1,18 @@
1
1
  import { Future, Option, Result } from "@bloodyowl/boxed";
2
2
  import { match } from "ts-pattern";
3
+ const getRail = (type) => match(type)
4
+ .returnType()
5
+ .with("CardOutDebit", () => "Card")
6
+ .with("SepaCreditTransferOut", "SepaInstantCreditTransferOut", "InternationalCreditTransferOut", "InternalCreditTransferOut", () => "Transfer")
7
+ .exhaustive();
8
+ export const createFakeTransaction = (id, type, details = {}) => { var _a, _b, _c; return ({
9
+ id,
10
+ rail: getRail(type),
11
+ type,
12
+ executionDate: (_a = details.executionDate) !== null && _a !== void 0 ? _a : "2026-11-04T10:00:00.000Z",
13
+ counterparty: (_b = details.counterparty) !== null && _b !== void 0 ? _b : `Merchant ${id}`,
14
+ amount: (_c = details.amount) !== null && _c !== void 0 ? _c : { value: "10.00", currency: "EUR" },
15
+ }); };
3
16
  const MAX_TRANSACTIONS = 100;
4
17
  const DRAFT_LIFETIME_MS = 30 * 24 * 60 * 60 * 1000;
5
18
  const isDefined = (value) => value !== undefined;
@@ -45,7 +58,7 @@ export const createFakeFraudReportApi = (config) => {
45
58
  const findTransaction = (id) => config.transactions.find(item => item.id === id);
46
59
  const getTransactions = (ids) => ids.map(findTransaction).filter(isDefined);
47
60
  const getIds = (item) => item.transactions.map(({ id }) => id);
48
- const getCardholder = (ids) => { var _a; var _b; return (_b = (_a = getTransactions(ids).flatMap(item => (item.rail === "Card" ? [item] : []))[0]) === null || _a === void 0 ? void 0 : _a.cardholderAccountMembershipId) !== null && _b !== void 0 ? _b : null; };
61
+ const getCardholder = (ids) => { var _a; var _b; return (_b = (_a = getTransactions(ids).flatMap(item => (item.type === "CardOutDebit" ? [item] : []))[0]) === null || _a === void 0 ? void 0 : _a.cardholderAccountMembershipId) !== null && _b !== void 0 ? _b : null; };
49
62
  const cases = new Map(((_c = config.cases) !== null && _c !== void 0 ? _c : []).map(item => [
50
63
  item.id,
51
64
  {
@@ -113,7 +126,7 @@ export const createFakeFraudReportApi = (config) => {
113
126
  : Option.None();
114
127
  };
115
128
  const checkSingleCardholder = (ids) => {
116
- const accountMembershipIds = unique(getTransactions(ids).flatMap(item => item.rail === "Card" ? [item.cardholderAccountMembershipId] : []));
129
+ const accountMembershipIds = unique(getTransactions(ids).flatMap(item => item.type === "CardOutDebit" ? [item.cardholderAccountMembershipId] : []));
117
130
  return accountMembershipIds.length > 1
118
131
  ? Option.Some({
119
132
  __typename: "DisputeCaseMultipleCardHoldersRejection",
@@ -154,8 +167,8 @@ export const createFakeFraudReportApi = (config) => {
154
167
  var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
155
168
  const transactionIds = (_b = input.transactionIds) !== null && _b !== void 0 ? _b : (existing != null ? getIds(existing) : []);
156
169
  const transactions = getTransactions(transactionIds);
157
- const hasCard = transactions.some(item => item.rail === "Card");
158
- const hasTransfer = transactions.some(item => item.rail === "Transfer");
170
+ const hasCard = transactions.some(item => getRail(item.type) === "Card");
171
+ const hasTransfer = transactions.some(item => getRail(item.type) === "Transfer");
159
172
  const accountId = (_d = (_c = (_a = transactions[0]) === null || _a === void 0 ? void 0 : _a.accountId) !== null && _c !== void 0 ? _c : existing === null || existing === void 0 ? void 0 : existing.accountId) !== null && _d !== void 0 ? _d : "";
160
173
  const sentVictim = input.victimAccountMembershipId;
161
174
  const checkLimit = () => input.transactionIds != null && input.transactionIds.length > MAX_TRANSACTIONS
@@ -219,7 +232,7 @@ export const createFakeFraudReportApi = (config) => {
219
232
  id: (_e = existing === null || existing === void 0 ? void 0 : existing.id) !== null && _e !== void 0 ? _e : crypto.randomUUID(),
220
233
  accountId,
221
234
  statusInfo: { status: "Draft", expiresAt: new Date(expiresAt).toISOString() },
222
- transactions: transactions.map(({ id, rail }) => ({ id, rail })),
235
+ transactions: transactions.map(({ id, type, executionDate, counterparty, amount }) => createFakeTransaction(id, type, { executionDate, counterparty, amount })),
223
236
  fraudDetails: input.fraudDetails === undefined
224
237
  ? ((_f = existing === null || existing === void 0 ? void 0 : existing.fraudDetails) !== null && _f !== void 0 ? _f : null)
225
238
  : input.fraudDetails === null
@@ -0,0 +1,3 @@
1
+ import type { FraudReportFormValues } from "./toInput";
2
+ import type { FraudReportCase } from "./types";
3
+ export declare const toFormValues: ({ transactions, fraudDetails, fraudCardDetails, victimAccountMembershipId, externalReference, }: Pick<FraudReportCase, "transactions" | "fraudDetails" | "fraudCardDetails" | "victimAccountMembershipId" | "externalReference">) => FraudReportFormValues;
@@ -0,0 +1,14 @@
1
+ export const toFormValues = ({ transactions, fraudDetails, fraudCardDetails, victimAccountMembershipId, externalReference, }) => { var _a, _b, _c, _d; return ({
2
+ transactions,
3
+ victimAccountMembershipId: victimAccountMembershipId !== null && victimAccountMembershipId !== void 0 ? victimAccountMembershipId : undefined,
4
+ reportedReason: fraudDetails === null || fraudDetails === void 0 ? void 0 : fraudDetails.reportedReason,
5
+ reportedTypology: fraudDetails === null || fraudDetails === void 0 ? void 0 : fraudDetails.reportedTypology,
6
+ description: fraudDetails === null || fraudDetails === void 0 ? void 0 : fraudDetails.description,
7
+ discoveredAt: (_a = fraudDetails === null || fraudDetails === void 0 ? void 0 : fraudDetails.discoveredAt) !== null && _a !== void 0 ? _a : undefined,
8
+ discoveryDetails: (_b = fraudDetails === null || fraudDetails === void 0 ? void 0 : fraudDetails.discoveryDetails) !== null && _b !== void 0 ? _b : undefined,
9
+ fraudsterChannels: fraudDetails === null || fraudDetails === void 0 ? void 0 : fraudDetails.fraudsterChannels,
10
+ sharedInformationWithFraudster: fraudDetails === null || fraudDetails === void 0 ? void 0 : fraudDetails.sharedInformationWithFraudster,
11
+ isLostOrStolen: (_c = fraudCardDetails === null || fraudCardDetails === void 0 ? void 0 : fraudCardDetails.isLostOrStolen) !== null && _c !== void 0 ? _c : undefined,
12
+ lostOrStolenNoticedAt: (_d = fraudCardDetails === null || fraudCardDetails === void 0 ? void 0 : fraudCardDetails.lostOrStolenNoticedAt) !== null && _d !== void 0 ? _d : undefined,
13
+ externalReference: externalReference !== null && externalReference !== void 0 ? externalReference : undefined,
14
+ }); };
@@ -0,0 +1,10 @@
1
+ import type { FraudReportLayoutStep } from "./stepper";
2
+ import { type FraudReportDetailsStep, type FraudReportRejection } from "./types";
3
+ type FixingStep = Exclude<FraudReportLayoutStep, "Submitted" | "Documents">;
4
+ export type FraudReportRejectionStep = FixingStep | "Submitted" | "Rejected";
5
+ export type FraudReportRejectionContext = {
6
+ hasDraft: boolean;
7
+ detailsSteps: readonly FraudReportDetailsStep[];
8
+ };
9
+ export declare const getRejectionStep: (rejection: FraudReportRejection, context: FraudReportRejectionContext) => FraudReportRejectionStep;
10
+ export {};
@@ -0,0 +1,53 @@
1
+ import { Array, Option } from "@bloodyowl/boxed";
2
+ import { match, P } from "ts-pattern";
3
+ import { fraudReportDetailsSteps, } from "./types";
4
+ const fixingSteps = [
5
+ "Victim",
6
+ "Transactions",
7
+ "Incident",
8
+ ...fraudReportDetailsSteps,
9
+ ];
10
+ // Once the draft exists, the flow keeps the victim: going back to Victim can't fix it
11
+ const getVictimStep = ({ hasDraft }) => hasDraft ? Option.None() : Option.Some("Victim");
12
+ // A sub-step missing from the flow is fixed where the choice that adds it is made
13
+ const getDetailsStep = (step, { detailsSteps }) => detailsSteps.includes(step)
14
+ ? step
15
+ : match(step)
16
+ .returnType()
17
+ .with("Fraudster", () => "Incident")
18
+ .with("Card", "Transfer", () => "Transactions")
19
+ .exhaustive();
20
+ // Only fields with an input in the form map to a step: going back can't fix the others
21
+ const getFieldStep = ({ path }, context) => match(path)
22
+ .returnType()
23
+ .with(["victimAccountMembershipId"], () => getVictimStep(context))
24
+ .with(["transactionIds"], () => Option.Some("Transactions"))
25
+ .with(["fraudDetails", P.union("fraudsterChannels", "sharedInformationWithFraudster")], () => Option.Some(getDetailsStep("Fraudster", context)))
26
+ .with(["fraudDetails"], [
27
+ "fraudDetails",
28
+ P.union("reportedReason", "reportedTypology", "description", "discoveredAt", "discoveryDetails"),
29
+ ], () => Option.Some("Incident"))
30
+ .with(["fraudCardDetails"], ["fraudCardDetails", P.union("isLostOrStolen", "lostOrStolenNoticedAt")], () => Option.Some(getDetailsStep("Card", context)))
31
+ .otherwise(() => Option.None());
32
+ const getEarliestStep = (fields, context) => {
33
+ var _a;
34
+ const steps = Array.filterMap(fields, field => getFieldStep(field, context));
35
+ return (_a = fixingSteps.find(step => steps.includes(step))) !== null && _a !== void 0 ? _a : "Rejected";
36
+ };
37
+ export const getRejectionStep = (rejection, context) => match(rejection)
38
+ .returnType()
39
+ .with({ __typename: "AccountMembershipNotFoundRejection" }, () => getVictimStep(context)
40
+ .map((step) => step)
41
+ .getOr("Rejected"))
42
+ .with({
43
+ __typename: P.union("DisputeCaseTransactionsNotFoundRejection", "DisputeCaseTransactionsNotReportableRejection", "DisputeCaseTransactionsLimitExceededRejection", "DisputeCaseMultipleAccountsRejection", "DisputeCaseMultipleCardHoldersRejection",
44
+ // Details that don't apply come from the selection: changing it is the fix
45
+ "DisputeCaseDetailsNotApplicableRejection"),
46
+ }, () => "Transactions")
47
+ .with({ __typename: "DisputeCaseIncompleteRejection" }, ({ missingFields }) => getEarliestStep(missingFields, context))
48
+ // Submitted elsewhere (another tab, an operator): the report reached Swan
49
+ .with({ __typename: "DisputeCaseWrongStatusRejection", currentStatus: "Submitted" }, () => "Submitted")
50
+ .with({
51
+ __typename: P.union("DisputeCaseTransactionsAlreadyReportedRejection", "DisputeCaseTransactionsOutsideReportingWindowRejection", "DisputeCaseWrongStatusRejection", "DisputeCaseNotFoundRejection"),
52
+ }, () => "Rejected")
53
+ .exhaustive();
@@ -0,0 +1,4 @@
1
+ import { FraudReportLayoutStep } from "./stepper";
2
+ import { FraudReportFormValues } from "./toInput";
3
+ export type FraudReportResumeStep = Exclude<FraudReportLayoutStep, "Victim" | "Submitted">;
4
+ export declare const getResumeStep: (values: FraudReportFormValues, startTransactionIds?: string[]) => FraudReportResumeStep;
@@ -0,0 +1,26 @@
1
+ import { Array, Option } from "@bloodyowl/boxed";
2
+ import { match } from "ts-pattern";
3
+ import { getDetailsSteps } from "./stepper";
4
+ import { toFilledList, toRequiredFraudDetails, toTransactionIds, } from "./toInput";
5
+ const isDetailsStepAnswered = (step, values) => match(step)
6
+ .with("Card", () => values.isLostOrStolen != null)
7
+ // The transfer sub-step has no form field yet
8
+ .with("Transfer", () => true)
9
+ .with("Fraudster", () => toFilledList(values.fraudsterChannels).isSome() &&
10
+ toFilledList(values.sharedInformationWithFraudster).isSome())
11
+ .exhaustive();
12
+ // The victim can't change once the draft exists, so a resumed report never opens on it
13
+ export const getResumeStep = (values,
14
+ // The transactions the report was opened from
15
+ startTransactionIds = []) => {
16
+ const draftTransactionIds = new Set(toTransactionIds(values));
17
+ // A draft is found from one of the start transactions: the user adds the ones it doesn't hold
18
+ if (draftTransactionIds.size === 0 ||
19
+ startTransactionIds.some(id => !draftTransactionIds.has(id))) {
20
+ return "Transactions";
21
+ }
22
+ if (toRequiredFraudDetails(values).isNone()) {
23
+ return "Incident";
24
+ }
25
+ return Array.findMap(getDetailsSteps(values), step => isDetailsStepAnswered(step, values) ? Option.None() : Option.Some(step)).getOr("Documents");
26
+ };
@@ -0,0 +1,16 @@
1
+ import { TopLevelStep } from "@swan-io/lake/src/components/LakeStepper";
2
+ import { FraudReason, FraudReportDetailsStep, FraudReportStep, FraudReportTransaction } from "./types";
3
+ export type FraudReportStepperStep = Exclude<FraudReportStep, "Intro">;
4
+ export type FraudReportLayoutStep = Exclude<FraudReportStepperStep, "Details"> | FraudReportDetailsStep;
5
+ export declare const getDetailsSteps: ({ transactions, reportedReason, }: {
6
+ transactions: Pick<FraudReportTransaction, "rail">[];
7
+ reportedReason?: FraudReason;
8
+ }) => ("Card" | "Fraudster" | "Transfer")[];
9
+ export declare const getFraudReportStepper: ({ activeStep, detailsSteps, getLabel, }: {
10
+ activeStep: FraudReportLayoutStep;
11
+ detailsSteps: readonly FraudReportDetailsStep[];
12
+ getLabel: (step: FraudReportStepperStep) => string;
13
+ }) => {
14
+ steps: TopLevelStep[];
15
+ activeStepId: FraudReportLayoutStep;
16
+ };
@@ -0,0 +1,21 @@
1
+ import { match } from "ts-pattern";
2
+ import { getCaseRail } from "./rail";
3
+ import { fraudReportDetailsSteps, fraudReportSteps, } from "./types";
4
+ const stepperSteps = fraudReportSteps.filter((step) => step !== "Intro");
5
+ export const getDetailsSteps = ({ transactions, reportedReason, }) => {
6
+ const caseRail = getCaseRail(transactions).toUndefined();
7
+ return fraudReportDetailsSteps.filter(step => match(step)
8
+ .with("Card", () => caseRail === "CardOnly" || caseRail === "Mixed")
9
+ .with("Transfer", () => caseRail === "TransferOnly" || caseRail === "Mixed")
10
+ .with("Fraudster", () => reportedReason === "Manipulated")
11
+ .exhaustive());
12
+ };
13
+ export const getFraudReportStepper = ({ activeStep, detailsSteps, getLabel, }) => {
14
+ const subSteps = fraudReportDetailsSteps.filter(step => step === activeStep || detailsSteps.includes(step));
15
+ return {
16
+ steps: stepperSteps.map((step) => step === "Details" && subSteps.length > 0
17
+ ? { label: getLabel(step), children: subSteps.map(id => ({ id, label: getLabel(step) })) }
18
+ : { id: step, label: getLabel(step) }),
19
+ activeStepId: activeStep,
20
+ };
21
+ };
@@ -1,3 +1,4 @@
1
+ import { Option } from "@bloodyowl/boxed";
1
2
  import type { DraftFraudDisputeCaseInput, FraudReason, FraudReportCase, FraudReportTransaction, FraudsterChannel, FraudTypology, SharedInformationWithFraudster, UpdateDraftFraudDisputeCaseInput } from "./types";
2
3
  export type FraudReportFormValues = {
3
4
  transactions: FraudReportTransaction[];
@@ -13,6 +14,14 @@ export type FraudReportFormValues = {
13
14
  lostOrStolenNoticedAt?: string;
14
15
  externalReference?: string;
15
16
  };
17
+ export declare const toFilled: (value: string | undefined) => Option<string>;
18
+ export declare const toFilledList: <T>(values: T[] | undefined) => Option<T[]>;
19
+ export declare const toRequiredFraudDetails: (values: FraudReportFormValues) => Option<{
20
+ reportedReason: "Manipulated" | "Unauthorized";
21
+ reportedTypology: "FakeAdvisor" | "FakeSupplierOrInvoice" | "FakeWebsite" | "ImpersonationOrPhishing" | "InvestmentScam" | "Other" | "RomanceScam";
22
+ description: string;
23
+ }>;
24
+ export declare const toTransactionIds: (values: FraudReportFormValues) => string[];
16
25
  export declare const toDraftInput: (values: FraudReportFormValues) => DraftFraudDisputeCaseInput;
17
26
  export declare const toUpdateInput: ({ savedCase, values, savedTransactionIds, }: {
18
27
  savedCase: Pick<FraudReportCase, "id" | "fraudDetails" | "fraudCardDetails">;