@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,20 +1,28 @@
1
1
  import { Option } from "@bloodyowl/boxed";
2
2
  import { match, P } from "ts-pattern";
3
3
  import { getCaseRail } from "./rail";
4
- const toFilled = (value) => Option.fromNullable(value).filter(item => item.trim() !== "");
5
- const toFilledList = (values) => Option.fromNullable(values).filter(items => items.length > 0);
6
- const toFraudDetails = (values, savedDetails) => Option.allFromDict({
4
+ import { getDetailsSteps } from "./stepper";
5
+ export const toFilled = (value) => Option.fromNullable(value).filter(item => item.trim() !== "");
6
+ export const toFilledList = (values) => Option.fromNullable(values).filter(items => items.length > 0);
7
+ // The incident answers `fraudDetails` can't be sent without
8
+ export const toRequiredFraudDetails = (values) => Option.allFromDict({
7
9
  reportedReason: Option.fromNullable(values.reportedReason),
8
10
  reportedTypology: Option.fromNullable(values.reportedTypology),
9
11
  description: toFilled(values.description),
10
- }).map(required => { var _a; var _b; return ({
11
- ...required,
12
- discoveredAt: toFilled(values.discoveredAt).toNull(),
13
- discoveryDetails: toFilled(values.discoveryDetails).toNull(),
14
- fraudsterChannels: toFilledList(values.fraudsterChannels).toNull(),
15
- sharedInformationWithFraudster: toFilledList(values.sharedInformationWithFraudster).toNull(),
16
- hasFiledPoliceComplaint: (_b = (_a = savedDetails === null || savedDetails === void 0 ? void 0 : savedDetails.fraudDetails) === null || _a === void 0 ? void 0 : _a.hasFiledPoliceComplaint) !== null && _b !== void 0 ? _b : null,
17
- }); });
12
+ });
13
+ const toFraudDetails = (values, savedDetails) => {
14
+ const hasFraudster = getDetailsSteps(values).includes("Fraudster");
15
+ return toRequiredFraudDetails(values).map(required => { var _a; var _b; return ({
16
+ ...required,
17
+ discoveredAt: toFilled(values.discoveredAt).toNull(),
18
+ discoveryDetails: toFilled(values.discoveryDetails).toNull(),
19
+ fraudsterChannels: hasFraudster ? toFilledList(values.fraudsterChannels).toNull() : null,
20
+ sharedInformationWithFraudster: hasFraudster
21
+ ? toFilledList(values.sharedInformationWithFraudster).toNull()
22
+ : null,
23
+ hasFiledPoliceComplaint: (_b = (_a = savedDetails === null || savedDetails === void 0 ? void 0 : savedDetails.fraudDetails) === null || _a === void 0 ? void 0 : _a.hasFiledPoliceComplaint) !== null && _b !== void 0 ? _b : null,
24
+ }); });
25
+ };
18
26
  const toFraudCardDetails = (values, savedDetails) => {
19
27
  var _a;
20
28
  var _b;
@@ -48,7 +56,7 @@ const toDetails = (values, savedDetails) => {
48
56
  .with(Option.P.None, () => ({ ...fraudDetails }))
49
57
  .exhaustive();
50
58
  };
51
- const toTransactionIds = (values) => values.transactions.map(({ id }) => id);
59
+ export const toTransactionIds = (values) => values.transactions.map(({ id }) => id);
52
60
  const isSameSelection = (transactionIds, savedTransactionIds) => {
53
61
  const savedIds = new Set(savedTransactionIds);
54
62
  return (new Set(transactionIds).size === savedIds.size && transactionIds.every(id => savedIds.has(id)));
@@ -0,0 +1,6 @@
1
+ import type { FraudReportTransaction } from "./types";
2
+ export declare const removeDuplicateTransactions: (transactions: FraudReportTransaction[]) => FraudReportTransaction[];
3
+ export declare const getTransactionsTotal: (transactions: Pick<FraudReportTransaction, "amount">[]) => {
4
+ value: number;
5
+ currency: string;
6
+ };
@@ -0,0 +1,7 @@
1
+ export const removeDuplicateTransactions = (transactions) => transactions.filter((transaction, index) => transactions.findIndex(({ id }) => id === transaction.id) === index);
2
+ export const getTransactionsTotal = (transactions) => {
3
+ var _a;
4
+ var _b;
5
+ const cents = transactions.reduce((total, { amount }) => total + Math.round(Number(amount.value) * 100), 0);
6
+ return { value: cents / 100, currency: (_b = (_a = transactions[0]) === null || _a === void 0 ? void 0 : _a.amount.currency) !== null && _b !== void 0 ? _b : "EUR" };
7
+ };
@@ -79,9 +79,17 @@ export type DisputeCaseStatusInfo = {
79
79
  submittedAt: string;
80
80
  };
81
81
  export type FraudReportRail = "Card" | "Transfer";
82
+ export type FraudReportTransactionType = "CardOutDebit" | "SepaCreditTransferOut" | "SepaInstantCreditTransferOut" | "InternationalCreditTransferOut" | "InternalCreditTransferOut";
82
83
  export type FraudReportTransaction = {
83
84
  id: string;
84
85
  rail: FraudReportRail;
86
+ type: FraudReportTransactionType;
87
+ executionDate: string;
88
+ counterparty: string;
89
+ amount: {
90
+ value: string;
91
+ currency: string;
92
+ };
85
93
  };
86
94
  export type FraudReportCase = {
87
95
  id: string;
@@ -6,6 +6,7 @@ export declare const DESCRIPTION_MAX_LENGTH = 2000;
6
6
  export declare const DISCOVERY_DETAILS_MAX_LENGTH = 1000;
7
7
  export declare const EXTERNAL_REFERENCE_MAX_LENGTH = 255;
8
8
  export declare const validateTransactionIds: FraudReportValidator<string[]>;
9
+ export declare const validateSelected: <T>(value: T | undefined) => FraudReportValidationError | undefined;
9
10
  export declare const validateNotInFuture: FraudReportValidator<string | undefined>;
10
11
  export declare const validateSharedInformation: FraudReportValidator<SharedInformationWithFraudster[] | undefined>;
11
12
  export declare const validateDescription: FraudReportValidator<string | undefined>;
@@ -11,6 +11,7 @@ export const validateTransactionIds = transactionIds => {
11
11
  return "Duplicates";
12
12
  }
13
13
  };
14
+ export const validateSelected = (value) => (value == null ? "Required" : undefined);
14
15
  export const validateNotInFuture = value => {
15
16
  if (value == null || value === "") {
16
17
  return;
@@ -0,0 +1,30 @@
1
+ import { Option } from "@bloodyowl/boxed";
2
+ export type FraudReportAccountMembership = {
3
+ id: string;
4
+ name: string;
5
+ };
6
+ export type VictimAnswer = "Myself" | "SomeoneElse";
7
+ type VictimChoice = {
8
+ answer: Option<VictimAnswer>;
9
+ selectedId: Option<string>;
10
+ };
11
+ type CurrentAccountMembership = Option<FraudReportAccountMembership>;
12
+ export declare const getOtherAccountMemberships: ({ currentAccountMembership, accountMemberships, }: {
13
+ currentAccountMembership: CurrentAccountMembership;
14
+ accountMemberships: FraudReportAccountMembership[];
15
+ }) => FraudReportAccountMembership[];
16
+ export declare const getInitialVictimChoice: ({ currentAccountMembership, victimAccountMembershipId, }: {
17
+ currentAccountMembership: CurrentAccountMembership;
18
+ victimAccountMembershipId: string | undefined;
19
+ }) => VictimChoice;
20
+ export declare const getSelectedAccountMembershipId: ({ currentAccountMembership, accountMemberships, choice, }: {
21
+ currentAccountMembership: CurrentAccountMembership;
22
+ accountMemberships: FraudReportAccountMembership[];
23
+ choice: VictimChoice;
24
+ }) => Option<string>;
25
+ export declare const getVictimAccountMembershipId: ({ currentAccountMembership, accountMemberships, choice, }: {
26
+ currentAccountMembership: CurrentAccountMembership;
27
+ accountMemberships: FraudReportAccountMembership[];
28
+ choice: VictimChoice;
29
+ }) => Option<string>;
30
+ export {};
@@ -0,0 +1,28 @@
1
+ import { Option } from "@bloodyowl/boxed";
2
+ import { match } from "ts-pattern";
3
+ export const getOtherAccountMemberships = ({ currentAccountMembership, accountMemberships, }) => accountMemberships.filter(({ id }) => currentAccountMembership.match({
4
+ Some: current => id !== current.id,
5
+ None: () => true,
6
+ }));
7
+ export const getInitialVictimChoice = ({ currentAccountMembership, victimAccountMembershipId, }) => {
8
+ const victim = Option.fromUndefined(victimAccountMembershipId);
9
+ return {
10
+ answer: currentAccountMembership.flatMap(current => victim.map(id => (id === current.id ? "Myself" : "SomeoneElse"))),
11
+ selectedId: victim,
12
+ };
13
+ };
14
+ export const getSelectedAccountMembershipId = ({ currentAccountMembership, accountMemberships, choice, }) => choice.selectedId.filter(id => getOtherAccountMemberships({ currentAccountMembership, accountMemberships }).some(item => item.id === id));
15
+ export const getVictimAccountMembershipId = ({ currentAccountMembership, accountMemberships, choice, }) => {
16
+ const selectedId = getSelectedAccountMembershipId({
17
+ currentAccountMembership,
18
+ accountMemberships,
19
+ choice,
20
+ });
21
+ return currentAccountMembership.match({
22
+ None: () => selectedId,
23
+ Some: current => choice.answer.flatMap(answer => match(answer)
24
+ .with("Myself", () => Option.Some(current.id))
25
+ .with("SomeoneElse", () => selectedId)
26
+ .exhaustive()),
27
+ });
28
+ };
@@ -1,5 +1,6 @@
1
1
  import { E164Number } from "libphonenumber-js";
2
2
  import { Country } from "../constants/countries";
3
+ export declare const getDefaultPhoneCountry: (languages?: readonly string[]) => Country;
3
4
  export declare const parsePhoneNumber: (value?: string) => {
4
5
  country: Country;
5
6
  nationalNumber: string;
@@ -1,10 +1,37 @@
1
- import { Lazy, Result } from "@bloodyowl/boxed";
1
+ import { Lazy, Option, Result } from "@bloodyowl/boxed";
2
2
  import { isNullish } from "@swan-io/lake/src/utils/nullish";
3
3
  import { getCountryCallingCode, isSupportedCountry, parsePhoneNumberWithError, } from "libphonenumber-js";
4
- import { getCountryByCCA2, isCountryCCA2 } from "../constants/countries";
5
- import { getMostLikelyUserCountry } from "./localization";
4
+ import { france, getCountryByCCA2, getCountryByCCA3, isCountryCCA2, phoneMarketCountries, } from "../constants/countries";
5
+ const phoneMarketByLanguage = {
6
+ fr: "FRA",
7
+ de: "DEU",
8
+ es: "ESP",
9
+ it: "ITA",
10
+ nl: "NLD",
11
+ pt: "PRT",
12
+ };
13
+ // A region that is one of our markets wins (nl-BE → Belgium), otherwise the language decides
14
+ // (de-AT → Germany). Belgium has no language of its own, so only its region leads to it
15
+ const getPhoneMarket = (languageTag) => Result.fromExecution(() => new Intl.Locale(languageTag))
16
+ .toOption()
17
+ .flatMap(({ language, region }) => Option.fromNullable(region)
18
+ .filter(isCountryCCA2)
19
+ .map(getCountryByCCA2)
20
+ .filter(country => phoneMarketCountries.includes(country.cca3))
21
+ .orElse(Option.fromNullable(phoneMarketByLanguage[language]).map(getCountryByCCA3)));
22
+ // Swan operates in a handful of markets, most users being in France: preselect the first
23
+ // market matching the browser languages, France otherwise
24
+ export const getDefaultPhoneCountry = (languages = navigator.languages) => {
25
+ for (const languageTag of languages) {
26
+ const market = getPhoneMarket(languageTag);
27
+ if (market.isSome()) {
28
+ return market.get();
29
+ }
30
+ }
31
+ return france;
32
+ };
6
33
  export const parsePhoneNumber = (value) => {
7
- const fallback = Lazy(() => ({ country: getMostLikelyUserCountry(), nationalNumber: "" }));
34
+ const fallback = Lazy(() => ({ country: getDefaultPhoneCountry(), nationalNumber: "" }));
8
35
  if (isNullish(value) || value.trim() === "") {
9
36
  return fallback.get();
10
37
  }