@swan-io/shared-business 18.4.0 → 18.6.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/BirthdatePicker.d.ts +3 -1
- package/src/components/BirthdatePicker.js +2 -2
- package/src/components/FraudReport/CardDetailsStep.d.ts +7 -0
- package/src/components/FraudReport/CardDetailsStep.js +46 -0
- package/src/components/FraudReport/FraudReportLayout.d.ts +23 -0
- package/src/components/FraudReport/FraudReportLayout.js +35 -0
- package/src/components/FraudReport/FraudsterDetailsStep.d.ts +7 -0
- package/src/components/FraudReport/FraudsterDetailsStep.js +56 -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 +32 -0
- package/src/components/FraudReport/TransactionPicker.d.ts +13 -0
- package/src/components/FraudReport/TransactionPicker.js +213 -0
- package/src/components/FraudReport/TransactionPickerFilters.d.ts +8 -0
- package/src/components/FraudReport/TransactionPickerFilters.js +213 -0
- package/src/components/FraudReport/TransactionRows.d.ts +8 -0
- package/src/components/FraudReport/TransactionRows.js +32 -0
- package/src/components/FraudReport/TransactionsStep.d.ts +16 -0
- package/src/components/FraudReport/TransactionsStep.js +120 -0
- package/src/components/FraudReport/VictimStep.d.ts +11 -0
- package/src/components/FraudReport/VictimStep.js +88 -0
- package/src/components/InlineDatePicker.js +7 -6
- package/src/components/LakeModal.js +1 -1
- 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 +1067 -0
- package/src/hooks/useFraudReportFlow.d.ts +32 -0
- package/src/hooks/useFraudReportFlow.js +265 -0
- package/src/locales/de.json +130 -0
- package/src/locales/en.json +130 -0
- package/src/locales/es.json +130 -0
- package/src/locales/fi.json +130 -0
- package/src/locales/fr.json +130 -0
- package/src/locales/it.json +130 -0
- package/src/locales/nl.json +130 -0
- package/src/locales/pt.json +130 -0
- package/src/utils/__tests__/fraudReport.card.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.card.test.js +39 -0
- package/src/utils/__tests__/fraudReport.fakeApi.test.js +42 -13
- package/src/utils/__tests__/fraudReport.fraudster.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.fraudster.test.js +35 -0
- package/src/utils/__tests__/fraudReport.messages.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.messages.test.js +28 -0
- package/src/utils/__tests__/fraudReport.picker.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.picker.test.js +171 -0
- package/src/utils/__tests__/fraudReport.pickerFilters.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.pickerFilters.test.js +144 -0
- package/src/utils/__tests__/fraudReport.prefill.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.prefill.test.js +151 -0
- package/src/utils/__tests__/fraudReport.rejections.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.rejections.test.js +155 -0
- package/src/utils/__tests__/fraudReport.resume.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.resume.test.js +81 -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.js +111 -37
- package/src/utils/__tests__/fraudReport.transactions.test.d.ts +1 -0
- package/src/utils/__tests__/fraudReport.transactions.test.js +38 -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 +168 -0
- package/src/utils/__tests__/phone.test.js +60 -5
- package/src/utils/__tests__/validation.test.js +69 -2
- package/src/utils/fraudReport/card.d.ts +11 -0
- package/src/utils/fraudReport/card.js +15 -0
- package/src/utils/fraudReport/fakeApi.d.ts +7 -4
- package/src/utils/fraudReport/fakeApi.js +19 -6
- package/src/utils/fraudReport/fraudster.d.ts +10 -0
- package/src/utils/fraudReport/fraudster.js +11 -0
- package/src/utils/fraudReport/messages.d.ts +4 -0
- package/src/utils/fraudReport/messages.js +7 -0
- package/src/utils/fraudReport/picker.d.ts +18 -0
- package/src/utils/fraudReport/picker.js +49 -0
- package/src/utils/fraudReport/pickerFilters.d.ts +26 -0
- package/src/utils/fraudReport/pickerFilters.js +62 -0
- package/src/utils/fraudReport/prefill.d.ts +3 -0
- package/src/utils/fraudReport/prefill.js +16 -0
- package/src/utils/fraudReport/rejections.d.ts +9 -0
- package/src/utils/fraudReport/rejections.js +49 -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 +14 -3
- package/src/utils/fraudReport/toInput.js +37 -31
- package/src/utils/fraudReport/transactions.d.ts +8 -0
- package/src/utils/fraudReport/transactions.js +11 -0
- package/src/utils/fraudReport/types.d.ts +8 -0
- package/src/utils/fraudReport/validation.d.ts +2 -1
- package/src/utils/fraudReport/validation.js +5 -0
- package/src/utils/fraudReport/victim.d.ts +41 -0
- package/src/utils/fraudReport/victim.js +37 -0
- package/src/utils/phone.d.ts +1 -0
- package/src/utils/phone.js +31 -4
- package/src/utils/validation.d.ts +2 -0
- package/src/utils/validation.js +14 -7
|
@@ -1,41 +1,47 @@
|
|
|
1
1
|
import { Option } from "@bloodyowl/boxed";
|
|
2
2
|
import { match, P } from "ts-pattern";
|
|
3
3
|
import { getCaseRail } from "./rail";
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
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
|
-
})
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
12
|
+
});
|
|
13
|
+
const toFraudDetails = (values) => {
|
|
14
|
+
const hasFraudster = getDetailsSteps(values).includes("Fraudster");
|
|
15
|
+
return toRequiredFraudDetails(values).map(required => { var _a; 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: (_a = values.hasFiledPoliceComplaint) !== null && _a !== void 0 ? _a : null,
|
|
24
|
+
}); });
|
|
25
|
+
};
|
|
26
|
+
const toFraudCardDetails = (values, { sendEmpty }) => {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
const details = {
|
|
29
|
+
isLostOrStolen: (_a = values.isLostOrStolen) !== null && _a !== void 0 ? _a : null,
|
|
30
|
+
lostOrStolenNoticedAt: toFilled(values.lostOrStolenNoticedAt).toNull(),
|
|
31
|
+
hasContactedMerchant: (_b = values.hasContactedMerchant) !== null && _b !== void 0 ? _b : null,
|
|
32
|
+
};
|
|
33
|
+
return sendEmpty || Object.values(details).some(value => value !== null)
|
|
34
|
+
? Option.Some(details)
|
|
35
|
+
: Option.None();
|
|
30
36
|
};
|
|
31
|
-
const toDetails = (values,
|
|
32
|
-
const fraudDetails = toFraudDetails(values
|
|
37
|
+
const toDetails = (values, { isUpdate }) => {
|
|
38
|
+
const fraudDetails = toFraudDetails(values)
|
|
33
39
|
.map(value => ({ fraudDetails: value }))
|
|
34
40
|
.toUndefined();
|
|
35
41
|
return match(getCaseRail(values.transactions))
|
|
36
42
|
.with(Option.P.Some(P.union("CardOnly", "Mixed")), () => ({
|
|
37
43
|
...fraudDetails,
|
|
38
|
-
...toFraudCardDetails(values,
|
|
44
|
+
...toFraudCardDetails(values, { sendEmpty: isUpdate })
|
|
39
45
|
.map(value => ({ fraudCardDetails: value }))
|
|
40
46
|
.toUndefined(),
|
|
41
47
|
}))
|
|
@@ -48,24 +54,24 @@ const toDetails = (values, savedDetails) => {
|
|
|
48
54
|
.with(Option.P.None, () => ({ ...fraudDetails }))
|
|
49
55
|
.exhaustive();
|
|
50
56
|
};
|
|
51
|
-
const toTransactionIds = (values) => values.transactions.map(({ id }) => id);
|
|
57
|
+
export const toTransactionIds = (values) => values.transactions.map(({ id }) => id);
|
|
52
58
|
const isSameSelection = (transactionIds, savedTransactionIds) => {
|
|
53
59
|
const savedIds = new Set(savedTransactionIds);
|
|
54
60
|
return (new Set(transactionIds).size === savedIds.size && transactionIds.every(id => savedIds.has(id)));
|
|
55
61
|
};
|
|
56
62
|
export const toDraftInput = (values) => ({
|
|
57
63
|
transactionIds: toTransactionIds(values),
|
|
58
|
-
...toDetails(values),
|
|
64
|
+
...toDetails(values, { isUpdate: false }),
|
|
59
65
|
...match(values.externalReference)
|
|
60
66
|
.with(P.union(undefined, ""), () => undefined)
|
|
61
67
|
.otherwise(externalReference => ({ externalReference })),
|
|
62
68
|
});
|
|
63
|
-
export const toUpdateInput = ({
|
|
69
|
+
export const toUpdateInput = ({ disputeCaseId, values, savedTransactionIds, }) => {
|
|
64
70
|
const transactionIds = toTransactionIds(values);
|
|
65
71
|
return {
|
|
66
|
-
disputeCaseId
|
|
72
|
+
disputeCaseId,
|
|
67
73
|
...(isSameSelection(transactionIds, savedTransactionIds) ? undefined : { transactionIds }),
|
|
68
|
-
...toDetails(values,
|
|
74
|
+
...toDetails(values, { isUpdate: true }),
|
|
69
75
|
...match(values.externalReference)
|
|
70
76
|
.with(undefined, () => undefined)
|
|
71
77
|
.with("", () => ({ externalReference: null }))
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FraudReportTransaction } from "./types";
|
|
2
|
+
export declare const MAX_TRANSACTIONS = 100;
|
|
3
|
+
export declare const getTransactionsOverLimit: (count: number) => number;
|
|
4
|
+
export declare const removeDuplicateTransactions: (transactions: FraudReportTransaction[]) => FraudReportTransaction[];
|
|
5
|
+
export declare const getTransactionsTotal: (transactions: Pick<FraudReportTransaction, "amount">[]) => {
|
|
6
|
+
value: number;
|
|
7
|
+
currency: string;
|
|
8
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The most transactions a dispute case takes (`DisputeCaseTransactionsLimitExceededRejection`).
|
|
2
|
+
export const MAX_TRANSACTIONS = 100;
|
|
3
|
+
// How many transactions a selection has to lose to fit in one report.
|
|
4
|
+
export const getTransactionsOverLimit = (count) => Math.max(0, count - MAX_TRANSACTIONS);
|
|
5
|
+
export const removeDuplicateTransactions = (transactions) => transactions.filter((transaction, index) => transactions.findIndex(({ id }) => id === transaction.id) === index);
|
|
6
|
+
export const getTransactionsTotal = (transactions) => {
|
|
7
|
+
var _a;
|
|
8
|
+
var _b;
|
|
9
|
+
const cents = transactions.reduce((total, { amount }) => total + Math.round(Number(amount.value) * 100), 0);
|
|
10
|
+
return { value: cents / 100, currency: (_b = (_a = transactions[0]) === null || _a === void 0 ? void 0 : _a.amount.currency) !== null && _b !== void 0 ? _b : "EUR" };
|
|
11
|
+
};
|
|
@@ -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;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { Validator } from "@swan-io/use-form";
|
|
2
2
|
import type { SharedInformationWithFraudster } from "./types";
|
|
3
|
-
export type FraudReportValidationError = "Required" | "Duplicates" | "InvalidDate" | "DateInFuture" | "NoneWithOtherValues" | "TooLong" | "InvalidCharacters";
|
|
3
|
+
export type FraudReportValidationError = "Required" | "Duplicates" | "TooMany" | "InvalidDate" | "DateInFuture" | "NoneWithOtherValues" | "TooLong" | "InvalidCharacters";
|
|
4
4
|
type FraudReportValidator<Value> = Validator<Value, FraudReportValidationError>;
|
|
5
5
|
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>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import dayjs from "dayjs";
|
|
2
|
+
import { MAX_TRANSACTIONS } from "./transactions";
|
|
2
3
|
export const DESCRIPTION_MAX_LENGTH = 2000;
|
|
3
4
|
export const DISCOVERY_DETAILS_MAX_LENGTH = 1000;
|
|
4
5
|
export const EXTERNAL_REFERENCE_MAX_LENGTH = 255;
|
|
@@ -10,7 +11,11 @@ export const validateTransactionIds = transactionIds => {
|
|
|
10
11
|
if (new Set(transactionIds).size !== transactionIds.length) {
|
|
11
12
|
return "Duplicates";
|
|
12
13
|
}
|
|
14
|
+
if (transactionIds.length > MAX_TRANSACTIONS) {
|
|
15
|
+
return "TooMany";
|
|
16
|
+
}
|
|
13
17
|
};
|
|
18
|
+
export const validateSelected = (value) => (value == null ? "Required" : undefined);
|
|
14
19
|
export const validateNotInFuture = value => {
|
|
15
20
|
if (value == null || value === "") {
|
|
16
21
|
return;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Option } from "@bloodyowl/boxed";
|
|
2
|
+
import type { FraudReportTransaction } from "./types";
|
|
3
|
+
export type FraudReportAccountMembership = {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
phoneNumber?: string;
|
|
7
|
+
};
|
|
8
|
+
export type VictimAnswer = "Myself" | "SomeoneElse";
|
|
9
|
+
type VictimChoice = {
|
|
10
|
+
answer: Option<VictimAnswer>;
|
|
11
|
+
selectedId: Option<string>;
|
|
12
|
+
};
|
|
13
|
+
type CurrentAccountMembership = Option<FraudReportAccountMembership>;
|
|
14
|
+
export declare const getOtherAccountMemberships: ({ currentAccountMembership, accountMemberships, }: {
|
|
15
|
+
currentAccountMembership: CurrentAccountMembership;
|
|
16
|
+
accountMemberships: FraudReportAccountMembership[];
|
|
17
|
+
}) => FraudReportAccountMembership[];
|
|
18
|
+
export declare const getInitialVictimChoice: ({ currentAccountMembership, victimAccountMembershipId, }: {
|
|
19
|
+
currentAccountMembership: CurrentAccountMembership;
|
|
20
|
+
victimAccountMembershipId: string | undefined;
|
|
21
|
+
}) => VictimChoice;
|
|
22
|
+
export declare const getSelectedAccountMembershipId: ({ currentAccountMembership, accountMemberships, choice, }: {
|
|
23
|
+
currentAccountMembership: CurrentAccountMembership;
|
|
24
|
+
accountMemberships: FraudReportAccountMembership[];
|
|
25
|
+
choice: VictimChoice;
|
|
26
|
+
}) => Option<string>;
|
|
27
|
+
export declare const getVictimAccountMembershipId: ({ currentAccountMembership, accountMemberships, choice, }: {
|
|
28
|
+
currentAccountMembership: CurrentAccountMembership;
|
|
29
|
+
accountMemberships: FraudReportAccountMembership[];
|
|
30
|
+
choice: VictimChoice;
|
|
31
|
+
}) => Option<string>;
|
|
32
|
+
export declare const getVictimChange: ({ transactions, previousVictimAccountMembershipId, victimAccountMembershipId, }: {
|
|
33
|
+
transactions: FraudReportTransaction[];
|
|
34
|
+
previousVictimAccountMembershipId: string | undefined;
|
|
35
|
+
victimAccountMembershipId: string | undefined;
|
|
36
|
+
}) => {
|
|
37
|
+
changed: boolean;
|
|
38
|
+
removed: FraudReportTransaction[];
|
|
39
|
+
kept: FraudReportTransaction[];
|
|
40
|
+
};
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
};
|
|
29
|
+
export const getVictimChange = ({ transactions, previousVictimAccountMembershipId, victimAccountMembershipId, }) => {
|
|
30
|
+
const changed = victimAccountMembershipId != null &&
|
|
31
|
+
previousVictimAccountMembershipId !== victimAccountMembershipId;
|
|
32
|
+
return {
|
|
33
|
+
changed,
|
|
34
|
+
removed: changed ? transactions.filter(({ rail }) => rail === "Card") : [],
|
|
35
|
+
kept: changed ? transactions.filter(({ rail }) => rail !== "Card") : transactions,
|
|
36
|
+
};
|
|
37
|
+
};
|
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
|
}
|
|
@@ -20,6 +20,8 @@ export { printFormat as printIbanFormat } from "iban";
|
|
|
20
20
|
export declare const validateIban: (iban: string) => string | undefined;
|
|
21
21
|
export declare const validateDate: (value: ExtractedDate | undefined) => string | undefined;
|
|
22
22
|
export declare const validateBirthdate: (value: ExtractedDate | undefined) => string | undefined;
|
|
23
|
+
export declare const validateAdultBirthdate: (value: ExtractedDate | undefined) => string | undefined;
|
|
24
|
+
export declare const validateAdultBirthdateString: (value: string | undefined) => string | undefined;
|
|
23
25
|
export declare const getTaxNumberLabel: (isCompany: boolean, country: CompanyCountryCCA3) => string;
|
|
24
26
|
export declare const getIndividualTaxNumberPlaceholder: (country: IndividualCountryCCA3 | CompanyCountryCCA3) => string | undefined;
|
|
25
27
|
export declare const getCompanyTaxNumberPlaceholder: (country: IndividualCountryCCA3 | CompanyCountryCCA3) => string | undefined;
|
package/src/utils/validation.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isNullish } from "@swan-io/lake/src/utils/nullish";
|
|
1
|
+
import { isNotNullishOrEmpty, isNullish } from "@swan-io/lake/src/utils/nullish";
|
|
2
2
|
import dayjs from "dayjs";
|
|
3
3
|
import { isValid as isValidIban } from "iban";
|
|
4
4
|
import { match, P } from "ts-pattern";
|
|
5
|
-
import { formatExtractedDate } from "./date";
|
|
5
|
+
import { extractDate, formatExtractedDate } from "./date";
|
|
6
6
|
import { locale, t } from "./i18n";
|
|
7
7
|
const EMAIL_REGEX = /^[A-Z0-9_+.-]*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i;
|
|
8
8
|
const VAT_NUMBER_REGEX = /^((AT)?U[0-9]{8}|(BE)?[01][0-9]{9}|(BG)?[0-9]{9,10}|(CY)?[0-9]{8}L|(CZ)?[0-9]{8,10}|(DE)?[0-9]{9}|(DK)?[0-9]{8}|(EE)?[0-9]{9}|(EL|GR)?[0-9]{9}|(ES)?[0-9A-Z][0-9]{7}[0-9A-Z]|(FI)?[0-9]{8}|(FR)?[0-9A-Z]{2}[0-9]{9}|(GB)?([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3})|(HU)?[0-9]{8}|(IE)?[0-9]S[0-9]{5}L|(IT)?[0-9]{11}|(LT)?([0-9]{9}|[0-9]{12})|(LU)?[0-9]{8}|(LV)?[0-9]{11}|(MT)?[0-9]{8}|(NL)?[0-9]{9}B[0-9]{2}|(PL)?[0-9]{10}|(PT)?[0-9]{9}|(RO)?[0-9]{2,10}|(SE)?[0-9]{12}|(SI)?[0-9]{8}|(SK)?[0-9]{10})$/;
|
|
@@ -221,20 +221,27 @@ export const validateDate = (value) => {
|
|
|
221
221
|
return t("validation.invalidDate");
|
|
222
222
|
}
|
|
223
223
|
};
|
|
224
|
-
|
|
224
|
+
const MAX_AGE = 150;
|
|
225
|
+
const ADULT_AGE = 18;
|
|
226
|
+
const getBirthdateError = (value, adultsOnly) => {
|
|
225
227
|
if (isNullish(value)) {
|
|
226
228
|
return t("validation.invalidBirthDate");
|
|
227
229
|
}
|
|
228
230
|
const date = dayjs.utc(formatExtractedDate(value), "YYYY-MM-DD", true);
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
if (!date.isValid() || isBirthdateOver150years || isBirthdateWithin4years) {
|
|
231
|
+
const today = dayjs.utc().startOf("day");
|
|
232
|
+
if (!date.isValid() || date.isBefore(today.subtract(MAX_AGE, "years"))) {
|
|
232
233
|
return t("validation.invalidBirthDate");
|
|
233
234
|
}
|
|
234
|
-
if (date.isAfter(
|
|
235
|
+
if (date.isAfter(today)) {
|
|
235
236
|
return t("validation.birthdateCannotBeFuture");
|
|
236
237
|
}
|
|
238
|
+
if (adultsOnly && today.diff(date, "years") < ADULT_AGE) {
|
|
239
|
+
return t("validation.birthdateMustBeAdult");
|
|
240
|
+
}
|
|
237
241
|
};
|
|
242
|
+
export const validateBirthdate = (value) => getBirthdateError(value, false);
|
|
243
|
+
export const validateAdultBirthdate = (value) => getBirthdateError(value, true);
|
|
244
|
+
export const validateAdultBirthdateString = (value) => isNotNullishOrEmpty(value) ? validateAdultBirthdate(extractDate(value)) : undefined;
|
|
238
245
|
/*
|
|
239
246
|
* Translation helpers for tax validation
|
|
240
247
|
*/
|