@swan-io/shared-business 18.3.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.
- package/package.json +3 -3
- package/src/components/FraudReport/FraudReportLayout.d.ts +23 -0
- package/src/components/FraudReport/FraudReportLayout.js +35 -0
- package/src/components/FraudReport/IncidentStep.d.ts +7 -0
- package/src/components/FraudReport/IncidentStep.js +55 -0
- package/src/components/FraudReport/IntroStep.d.ts +5 -0
- package/src/components/FraudReport/IntroStep.js +28 -0
- package/src/components/FraudReport/TransactionsStep.d.ts +16 -0
- package/src/components/FraudReport/TransactionsStep.js +138 -0
- package/src/components/FraudReport/VictimStep.d.ts +9 -0
- package/src/components/FraudReport/VictimStep.js +55 -0
- package/src/components/InlineDatePicker.js +7 -6
- package/src/constants/__tests__/countries.test.js +32 -1
- package/src/constants/countries.d.ts +2 -1
- package/src/constants/countries.js +36 -2
- package/src/hooks/__tests__/useFraudReportFlow.test.d.ts +1 -0
- package/src/hooks/__tests__/useFraudReportFlow.test.js +979 -0
- package/src/hooks/useFraudReportFlow.d.ts +32 -0
- package/src/hooks/useFraudReportFlow.js +260 -0
- package/src/locales/de.json +76 -0
- package/src/locales/en.json +76 -0
- package/src/locales/es.json +76 -0
- package/src/locales/fi.json +76 -0
- package/src/locales/fr.json +76 -0
- package/src/locales/it.json +76 -0
- package/src/locales/nl.json +76 -0
- package/src/locales/pt.json +76 -0
- package/src/utils/__tests__/fraudReport.fakeApi.test.js +42 -13
- package/src/utils/__tests__/fraudReport.prefill.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.prefill.test.js +149 -0
- package/src/utils/__tests__/fraudReport.rejections.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.rejections.test.js +163 -0
- package/src/utils/__tests__/fraudReport.resume.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.resume.test.js +67 -0
- package/src/utils/__tests__/fraudReport.stepper.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.stepper.test.js +102 -0
- package/src/utils/__tests__/fraudReport.toInput.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.toInput.test.js +549 -0
- package/src/utils/__tests__/fraudReport.transactions.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.transactions.test.js +28 -0
- package/src/utils/__tests__/fraudReport.types.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.types.test.js +22 -0
- package/src/utils/__tests__/fraudReport.victim.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.victim.test.js +142 -0
- package/src/utils/__tests__/phone.test.js +60 -5
- package/src/utils/fraudReport/fakeApi.d.ts +7 -4
- package/src/utils/fraudReport/fakeApi.js +18 -5
- package/src/utils/fraudReport/prefill.d.ts +3 -0
- package/src/utils/fraudReport/prefill.js +14 -0
- package/src/utils/fraudReport/rail.d.ts +5 -0
- package/src/utils/fraudReport/rail.js +14 -0
- package/src/utils/fraudReport/rejections.d.ts +10 -0
- package/src/utils/fraudReport/rejections.js +53 -0
- package/src/utils/fraudReport/resume.d.ts +4 -0
- package/src/utils/fraudReport/resume.js +26 -0
- package/src/utils/fraudReport/stepper.d.ts +16 -0
- package/src/utils/fraudReport/stepper.js +21 -0
- package/src/utils/fraudReport/toInput.d.ts +30 -0
- package/src/utils/fraudReport/toInput.js +82 -0
- package/src/utils/fraudReport/transactions.d.ts +6 -0
- package/src/utils/fraudReport/transactions.js +7 -0
- package/src/utils/fraudReport/types.d.ts +8 -0
- package/src/utils/fraudReport/validation.d.ts +15 -0
- package/src/utils/fraudReport/validation.js +55 -0
- package/src/utils/fraudReport/victim.d.ts +30 -0
- package/src/utils/fraudReport/victim.js +28 -0
- package/src/utils/phone.d.ts +1 -0
- package/src/utils/phone.js +31 -4
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Validator } from "@swan-io/use-form";
|
|
2
|
+
import type { SharedInformationWithFraudster } from "./types";
|
|
3
|
+
export type FraudReportValidationError = "Required" | "Duplicates" | "InvalidDate" | "DateInFuture" | "NoneWithOtherValues" | "TooLong" | "InvalidCharacters";
|
|
4
|
+
type FraudReportValidator<Value> = Validator<Value, FraudReportValidationError>;
|
|
5
|
+
export declare const DESCRIPTION_MAX_LENGTH = 2000;
|
|
6
|
+
export declare const DISCOVERY_DETAILS_MAX_LENGTH = 1000;
|
|
7
|
+
export declare const EXTERNAL_REFERENCE_MAX_LENGTH = 255;
|
|
8
|
+
export declare const validateTransactionIds: FraudReportValidator<string[]>;
|
|
9
|
+
export declare const validateSelected: <T>(value: T | undefined) => FraudReportValidationError | undefined;
|
|
10
|
+
export declare const validateNotInFuture: FraudReportValidator<string | undefined>;
|
|
11
|
+
export declare const validateSharedInformation: FraudReportValidator<SharedInformationWithFraudster[] | undefined>;
|
|
12
|
+
export declare const validateDescription: FraudReportValidator<string | undefined>;
|
|
13
|
+
export declare const validateDiscoveryDetails: FraudReportValidator<string | undefined>;
|
|
14
|
+
export declare const validateExternalReference: FraudReportValidator<string | undefined>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
export const DESCRIPTION_MAX_LENGTH = 2000;
|
|
3
|
+
export const DISCOVERY_DETAILS_MAX_LENGTH = 1000;
|
|
4
|
+
export const EXTERNAL_REFERENCE_MAX_LENGTH = 255;
|
|
5
|
+
const PRINTABLE_ASCII_REGEX = /^[\x20-\x7E]*$/;
|
|
6
|
+
export const validateTransactionIds = transactionIds => {
|
|
7
|
+
if (transactionIds.length === 0) {
|
|
8
|
+
return "Required";
|
|
9
|
+
}
|
|
10
|
+
if (new Set(transactionIds).size !== transactionIds.length) {
|
|
11
|
+
return "Duplicates";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
export const validateSelected = (value) => (value == null ? "Required" : undefined);
|
|
15
|
+
export const validateNotInFuture = value => {
|
|
16
|
+
if (value == null || value === "") {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const date = dayjs.utc(value, "YYYY-MM-DD", true);
|
|
20
|
+
if (!date.isValid()) {
|
|
21
|
+
return "InvalidDate";
|
|
22
|
+
}
|
|
23
|
+
if (date.isAfter(dayjs.utc(), "day")) {
|
|
24
|
+
return "DateInFuture";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export const validateSharedInformation = values => {
|
|
28
|
+
if (values != null && values.length > 1 && values.includes("None")) {
|
|
29
|
+
return "NoneWithOtherValues";
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export const validateDescription = value => {
|
|
33
|
+
if (value == null || value.trim() === "") {
|
|
34
|
+
return "Required";
|
|
35
|
+
}
|
|
36
|
+
if (value.length > DESCRIPTION_MAX_LENGTH) {
|
|
37
|
+
return "TooLong";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
export const validateDiscoveryDetails = value => {
|
|
41
|
+
if (value != null && value.length > DISCOVERY_DETAILS_MAX_LENGTH) {
|
|
42
|
+
return "TooLong";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
export const validateExternalReference = value => {
|
|
46
|
+
if (value == null) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!PRINTABLE_ASCII_REGEX.test(value)) {
|
|
50
|
+
return "InvalidCharacters";
|
|
51
|
+
}
|
|
52
|
+
if (value.length > EXTERNAL_REFERENCE_MAX_LENGTH) {
|
|
53
|
+
return "TooLong";
|
|
54
|
+
}
|
|
55
|
+
};
|
|
@@ -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
|
+
};
|
package/src/utils/phone.d.ts
CHANGED
|
@@ -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;
|
package/src/utils/phone.js
CHANGED
|
@@ -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
|
-
|
|
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:
|
|
34
|
+
const fallback = Lazy(() => ({ country: getDefaultPhoneCountry(), nationalNumber: "" }));
|
|
8
35
|
if (isNullish(value) || value.trim() === "") {
|
|
9
36
|
return fallback.get();
|
|
10
37
|
}
|