Skip to content

Commit

Permalink
Fix rules for TIN
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodyowl committed Aug 22, 2023
1 parent 6259539 commit c1787fd
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 63 deletions.
70 changes: 53 additions & 17 deletions clients/banking/src/components/MembershipDetailEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { useUrqlMutation } from "@swan-io/lake/src/hooks/useUrqlMutation";
import { showToast } from "@swan-io/lake/src/state/toasts";
import { CountryPicker } from "@swan-io/shared-business/src/components/CountryPicker";
import { GMapAddressSearchInput } from "@swan-io/shared-business/src/components/GMapAddressSearchInput";
import { TaxIdentificationNumberInput } from "@swan-io/shared-business/src/components/TaxIdentificationNumberInput";
import { CountryCCA3, allCountries } from "@swan-io/shared-business/src/constants/countries";
import { validateIndividualTaxNumber } from "@swan-io/shared-business/src/utils/validation";
import dayjs from "dayjs";
import { useState } from "react";
import { StyleSheet, View } from "react-native";
Expand All @@ -30,7 +32,6 @@ import {
validateBirthdate,
validateName,
validateRequired,
validateTaxIdentificationNumber,
} from "../utils/validations";
import { MembershipCancelConfirmationModal } from "./MembershipCancelConfirmationModal";
import { MembershipInvitationLinkModal } from "./MembershipInvitationLinkModal";
Expand Down Expand Up @@ -188,11 +189,31 @@ export const MembershipDetailEditor = ({
initialValue: editingAccountMembership.taxIdentificationNumber ?? "",
strategy: "onBlur",
validate: (value, { getFieldState }) => {
return match({ accountCountry, residencyAddressCountry: getFieldState("country").value })
return match({
accountCountry,
country: getFieldState("country").value,
canViewAccount: editingAccountMembership.canViewAccount,
canInitiatePayment: editingAccountMembership.canInitiatePayments,
})
.with(
{ accountCountry: "DEU", residencyAddressCountry: "DEU" },
{ accountCountry: "NLD" },
() => validateTaxIdentificationNumber(value),
P.intersection(
{ accountCountry: "DEU", country: "DEU" },
P.union(
{
canViewAccount: true,
},
{ canInitiatePayment: true },
),
),
({ accountCountry }) =>
combineValidators(
validateRequired,
validateIndividualTaxNumber(accountCountry),
)(value),
)
.with({ accountCountry: "DEU" }, { accountCountry: "NLD" }, ({ accountCountry }) =>
validateIndividualTaxNumber(accountCountry)(value),
)
.otherwise(() => {});
},
Expand Down Expand Up @@ -727,21 +748,36 @@ export const MembershipDetailEditor = ({
.with(
{ accountCountry: "DEU", country: "DEU" },
{ accountCountry: "NLD" },
() => (
({ accountCountry, country }) => (
<Field name="taxIdentificationNumber">
{({ value, valid, error, onChange }) => (
<LakeLabel
label={t("membershipDetail.edit.taxIdentificationNumber")}
render={id => (
<LakeTextInput
placeholder={locale.taxIdentificationNumberPlaceholder}
id={id}
value={value}
valid={valid}
error={error}
onChangeText={onChange}
/>
)}
<TaxIdentificationNumberInput
accountCountry={accountCountry}
isCompany={false}
value={value}
valid={valid}
error={error}
onChange={onChange}
required={match({
accountCountry,
country,
canViewAccount: editingAccountMembership.canViewAccount,
canInitiatePayment: editingAccountMembership.canInitiatePayments,
})
.with({ accountCountry: "NLD" }, () => true)
.with(
P.intersection(
{ accountCountry: "DEU", country: "DEU" },
P.union(
{
canViewAccount: true,
},
{ canInitiatePayment: true },
),
),
() => true,
)
.otherwise(() => false)}
/>
)}
</Field>
Expand Down
66 changes: 43 additions & 23 deletions clients/banking/src/components/NewMembershipWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,10 @@ export const NewMembershipWizard = ({
const [memberAddition, addMember] = useUrqlMutation(AddAccountMembershipDocument);

const steps: Step[] = match({ accountCountry, partiallySavedValues })
.with(
{ accountCountry: "DEU" },
{ accountCountry: "NLD" },
{ partiallySavedValues: { canInitiatePayments: true, canViewAccount: true } },
() => ["Informations" as const, "Address" as const],
)
.with({ accountCountry: "DEU" }, { accountCountry: "NLD" }, () => [
"Informations" as const,
"Address" as const,
])
.otherwise(() => ["Informations" as const]);

const { Field, FieldsListener, setFieldValue, submitForm } = useForm<FormState>({
Expand Down Expand Up @@ -213,13 +211,19 @@ export const NewMembershipWizard = ({
initialValue: partiallySavedValues?.addressLine1 ?? "",
validate: (value, { getFieldState }) => {
return match({
accountCountry,
canViewAccount: getFieldState("canViewAccount").value,
canInitiatePayments: getFieldState("canInitiatePayments").value,
})
.with({ canViewAccount: true }, { canInitiatePayments: true }, () => {
const validate = combineValidators(validateRequired, validateAddressLine);
return validate(value);
})
.with(
{ accountCountry: "NLD" },
{ canViewAccount: true },
{ canInitiatePayments: true },
() => {
const validate = combineValidators(validateRequired, validateAddressLine);
return validate(value);
},
)
.otherwise(() => {
return validateAddressLine(value);
});
Expand All @@ -229,38 +233,56 @@ export const NewMembershipWizard = ({
initialValue: partiallySavedValues?.postalCode ?? "",
validate: (value, { getFieldState }) => {
return match({
accountCountry,
canViewAccount: getFieldState("canViewAccount").value,
canInitiatePayments: getFieldState("canInitiatePayments").value,
})
.with({ canViewAccount: true }, { canInitiatePayments: true }, () => {
return validateRequired(value);
})
.with(
{ accountCountry: "NLD" },
{ canViewAccount: true },
{ canInitiatePayments: true },
() => {
return validateRequired(value);
},
)
.otherwise(() => undefined);
},
},
city: {
initialValue: partiallySavedValues?.city ?? "",
validate: (value, { getFieldState }) => {
return match({
accountCountry,
canViewAccount: getFieldState("canViewAccount").value,
canInitiatePayments: getFieldState("canInitiatePayments").value,
})
.with({ canViewAccount: true }, { canInitiatePayments: true }, () => {
return validateRequired(value);
})
.with(
{ accountCountry: "NLD" },
{ canViewAccount: true },
{ canInitiatePayments: true },
() => {
return validateRequired(value);
},
)
.otherwise(() => undefined);
},
},
country: {
initialValue: partiallySavedValues?.country ?? accountCountry ?? "FRA",
validate: (value, { getFieldState }) => {
return match({
accountCountry,
canViewAccount: getFieldState("canViewAccount").value,
canInitiatePayments: getFieldState("canInitiatePayments").value,
})
.with({ canViewAccount: true }, { canInitiatePayments: true }, () => {
return validateRequired(value);
})
.with(
{ accountCountry: "NLD" },
{ canViewAccount: true },
{ canInitiatePayments: true },
() => {
return validateRequired(value);
},
)
.otherwise(() => undefined);
},
},
Expand All @@ -276,12 +298,10 @@ export const NewMembershipWizard = ({
})
.with(
P.intersection(
P.union(
{ accountCountry: "DEU", residencyAddressCountry: "DEU" },
{ accountCountry: "NLD" },
),
P.union({ accountCountry: "DEU", residencyAddressCountry: "DEU" }),
P.union({ canViewAccount: true }, { canInitiatePayments: true }),
),
{ accountCountry: "NLD" },
() =>
combineValidators(
validateRequired,
Expand Down
1 change: 1 addition & 0 deletions clients/banking/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"accountDetails.settings.contractsDescription": "For your convenience, here are copies of your contracts:",
"accountDetails.settings.language": "Official document language",
"accountDetails.settings.languageDescription": "Choose the language you want to be used for official documents such as account statements and bank details.",
"accountDetails.settings.legalRepresentativeTaxIdentificationNumber": "Legal Representative Tax Identification Number",
"accountDetails.settings.partnershipConditions": "{projectName} partnership conditions",
"accountDetails.settings.save": "Save",
"accountDetails.settings.tab": "Settings",
Expand Down
19 changes: 15 additions & 4 deletions clients/banking/src/pages/AccountDetailsSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@ export const AccountDetailsSettingsPage = ({
taxIdentificationNumber: {
initialValue: holderInfo?.taxIdentificationNumber ?? "",
sanitize: value => value.trim(),
validate: isCompany
? validateCompanyTaxNumber(accountCountry)
: validateIndividualTaxNumber(accountCountry),
validate:
account?.country === "NLD"
? combineValidators(validateRequired, validateIndividualTaxNumber(accountCountry))
: isCompany
? validateCompanyTaxNumber(accountCountry)
: validateIndividualTaxNumber(accountCountry),
},
});

Expand Down Expand Up @@ -168,6 +171,7 @@ export const AccountDetailsSettingsPage = ({
const shouldEditTaxIdentificationNumber =
(account.country === "DEU" && account.holder.residencyAddress.country === "DEU") ||
account.country === "NLD";
const isTaxIdentificationRequired = account.country === "NLD";

const tcuUrl = account.holder.onboarding?.tcuUrl;

Expand Down Expand Up @@ -235,6 +239,11 @@ export const AccountDetailsSettingsPage = ({
<Field name="taxIdentificationNumber">
{({ error, onBlur, onChange, valid, value }) => (
<TaxIdentificationNumberInput
label={
account.country === "NLD"
? t("accountDetails.settings.legalRepresentativeTaxIdentificationNumber")
: undefined
}
value={value}
error={error}
valid={valid}
Expand All @@ -243,6 +252,7 @@ export const AccountDetailsSettingsPage = ({
onBlur={onBlur}
accountCountry={accountCountry}
isCompany={isCompany}
required={isTaxIdentificationRequired}
/>
)}
</Field>
Expand All @@ -252,7 +262,8 @@ export const AccountDetailsSettingsPage = ({
)}

{shouldEditTaxIdentificationNumber &&
isNullishOrEmpty(holderInfo?.taxIdentificationNumber) && (
isNullishOrEmpty(holderInfo?.taxIdentificationNumber) &&
!isTaxIdentificationRequired && (
<>
<LakeAlert
variant="info"
Expand Down
19 changes: 0 additions & 19 deletions clients/banking/src/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,6 @@ export const validateTransferReference: Validator<string> = value => {
}
};

export const validateTaxIdentificationNumber: Validator<string> = value => {
if (!value) {
return t("common.form.required");
}
if (value.length !== 11 && value.length !== 10) {
return t("common.form.invalidTaxIdentificationNumber");
}
};

export const validateIndividualTaxNumber: Validator<string> = value => {
if (!value) {
return;
}
// accept 11 digits
if (!/^\d{11}$/.test(value)) {
return t("common.form.invalidTaxIdentificationNumber");
}
};

export const validateAccountNameLength: Validator<string> = value => {
const maxLength = 256;
if (value.length > maxLength) {
Expand Down

0 comments on commit c1787fd

Please sign in to comment.