Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/libs/ReimbursementAccountUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ const hasInProgressNonUSDVBBA = (achData?: ACHDataReimbursementAccount): boolean
return !!achData?.bankAccountID && !!achData?.created;
};

/**
* Returns achData.bankAccountID coerced to a number, defaulting to 0 when missing so VBBA API calls avoid NaN from undefined.
*/
function getBankAccountIDAsNumber(achData?: ACHDataReimbursementAccount): number {
return Number(achData?.bankAccountID ?? CONST.DEFAULT_NUMBER_ID);
}

/** Returns true if VBBA flow is in progress */
const hasInProgressVBBA = (achData?: ACHDataReimbursementAccount, isNonUSDWorkspace?: boolean, policyID?: string) => {
if (achData?.policyID !== policyID) {
Expand All @@ -60,5 +67,5 @@ const hasInProgressVBBA = (achData?: ACHDataReimbursementAccount, isNonUSDWorksp
return hasInProgressUSDVBBA(achData);
};

export {getRouteForCurrentStep, hasInProgressUSDVBBA, hasInProgressNonUSDVBBA, hasInProgressVBBA, REIMBURSEMENT_ACCOUNT_ROUTE_NAMES};
export {getBankAccountIDAsNumber, getRouteForCurrentStep, hasInProgressUSDVBBA, hasInProgressNonUSDVBBA, hasInProgressVBBA, REIMBURSEMENT_ACCOUNT_ROUTE_NAMES};
export type {ReimbursementAccountStepToOpen};
4 changes: 2 additions & 2 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function getOnyxDataForConnectingVBBAAndLastPaymentMethod(
*/
function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAccount: PlaidBankAccount, policyID: string) {
Comment thread
c3024 marked this conversation as resolved.
Resolved
const parameters: ConnectBankAccountParams = {
bankAccountID,
bankAccountID: !Number.isNaN(bankAccountID) ? bankAccountID : CONST.DEFAULT_NUMBER_ID,
routingNumber: selectedPlaidBankAccount.routingNumber,
accountNumber: selectedPlaidBankAccount.accountNumber,
bank: selectedPlaidBankAccount.bankName,
Expand Down Expand Up @@ -1162,7 +1162,7 @@ function acceptACHContractForBankAccount(bankAccountID: number, params: ACHContr
*/
function connectBankAccountManually(bankAccountID: number, bankAccount: PlaidBankAccount, policyID: string) {
const parameters: ConnectBankAccountParams = {
bankAccountID,
bankAccountID: !Number.isNaN(bankAccountID) ? bankAccountID : CONST.DEFAULT_NUMBER_ID,
routingNumber: bankAccount.routingNumber,
accountNumber: bankAccount.accountNumber,
bank: bankAccount.bankName,
Expand Down
16 changes: 9 additions & 7 deletions src/pages/ReimbursementAccount/USD/BankInfo/BankInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, {useEffect} from 'react';
import React, {useEffect, useRef} from 'react';
import InteractiveStepWrapper from '@components/InteractiveStepWrapper';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useSubStep from '@hooks/useSubStep';
import type {SubStepProps} from '@hooks/useSubStep/types';
import getPlaidOAuthReceivedRedirectURI from '@libs/getPlaidOAuthReceivedRedirectURI';
import {getBankAccountIDAsNumber} from '@libs/ReimbursementAccountUtils';
import getSubStepValues from '@pages/ReimbursementAccount/utils/getSubStepValues';
import {connectBankAccountManually, connectBankAccountWithPlaid} from '@userActions/BankAccounts';
import {hideBankAccountErrors} from '@userActions/ReimbursementAccount';
Expand Down Expand Up @@ -41,7 +42,7 @@ function BankInfo({onBackButtonPress, policyID, setUSDBankAccountStep}: BankInfo
const [plaidLinkToken] = useOnyx(ONYXKEYS.PLAID_LINK_TOKEN);
const {translate} = useLocalize();

const [redirectedFromPlaidToManual, setRedirectedFromPlaidToManual] = React.useState(false);
const redirectedFromPlaidToManualRef = useRef(false);
const values = getSubStepValues(BANK_INFO_STEP_KEYS, reimbursementAccountDraft, reimbursementAccount ?? {});

let setupType = reimbursementAccount?.achData?.subStep ?? '';
Expand All @@ -51,7 +52,7 @@ function BankInfo({onBackButtonPress, policyID, setUSDBankAccountStep}: BankInfo
setupType = CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID;
}

const bankAccountID = Number(reimbursementAccount?.achData?.bankAccountID);
const bankAccountID = getBankAccountIDAsNumber(reimbursementAccount?.achData);
const submit = (submitData: unknown) => {
const data = submitData as ReimbursementAccountForm;
if (setupType === CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL) {
Expand Down Expand Up @@ -86,19 +87,20 @@ function BankInfo({onBackButtonPress, policyID, setUSDBankAccountStep}: BankInfo
};

const bodyContent = setupType === CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID ? plaidSubSteps : manualSubSteps;
// eslint-disable-next-line @typescript-eslint/no-deprecated
const {componentToRender: SubStep, isEditing, screenIndex, nextScreen, prevScreen, moveTo} = useSubStep<BankInfoSubStepProps>({bodyContent, startFrom: 0, onFinished: submit});

// Some services user connects to via Plaid return dummy account numbers and routing numbers e.g. Chase
// In this case we need to redirect user to manual flow to enter real account number and routing number
// and we need to do it only once so redirectedFromPlaidToManual flag is used
useEffect(() => {
if (redirectedFromPlaidToManual) {
if (redirectedFromPlaidToManualRef.current) {
return;
}
if (setupType === CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL && values.bankName !== '' && !redirectedFromPlaidToManual) {
setRedirectedFromPlaidToManual(true);
if (setupType === CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL && values.bankName !== '') {
redirectedFromPlaidToManualRef.current = true;
}
}, [redirectedFromPlaidToManual, setupType, values.bankName]);
}, [setupType, values.bankName]);

const handleBackButtonPress = () => {
if (screenIndex === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import useOnyx from '@hooks/useOnyx';
import usePrevious from '@hooks/usePrevious';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import {getBankAccountIDAsNumber} from '@libs/ReimbursementAccountUtils';
import {setBankAccountSubStep, validatePlaidSelection} from '@userActions/BankAccounts';
import {updateReimbursementAccountDraft} from '@userActions/ReimbursementAccount';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import INPUT_IDS from '@src/types/form/ReimbursementAccountForm';

Expand Down Expand Up @@ -48,7 +48,7 @@ function Plaid({onNext, setUSDBankAccountStep}: PlaidProps) {
onNext(bankAccountData);
}, [plaidData, reimbursementAccountDraft, onNext]);

const bankAccountID = reimbursementAccount?.achData?.bankAccountID ?? CONST.DEFAULT_NUMBER_ID;
const bankAccountID = getBankAccountIDAsNumber(reimbursementAccount?.achData);

useEffect(() => {
const plaidBankAccounts = plaidData?.bankAccounts ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import useOnyx from '@hooks/useOnyx';
import useSubStep from '@hooks/useSubStep';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
import {getBankAccountIDAsNumber} from '@libs/ReimbursementAccountUtils';
import {updateBeneficialOwnersForBankAccount} from '@userActions/BankAccounts';
import {setDraftValues} from '@userActions/FormActions';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -69,7 +70,7 @@ function BeneficialOwnersStep({onBackButtonPress}: BeneficialOwnersStepProps) {
);

updateBeneficialOwnersForBankAccount(
Number(reimbursementAccount?.achData?.bankAccountID ?? CONST.DEFAULT_NUMBER_ID),
getBankAccountIDAsNumber(reimbursementAccount?.achData),
{
ownsMoreThan25Percent: isUserUBO,
beneficialOwners: JSON.stringify(beneficialOwners),
Expand Down Expand Up @@ -109,6 +110,7 @@ function BeneficialOwnersStep({onBackButtonPress}: BeneficialOwnersStepProps) {
moveTo,
resetScreenIndex,
goToTheLastStep,
// eslint-disable-next-line @typescript-eslint/no-deprecated
} = useSubStep<BeneficialOwnerSubStepProps>({
bodyContent,
startFrom: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import useOnyx from '@hooks/useOnyx';
import useSubStep from '@hooks/useSubStep';
import type {SubStepProps} from '@hooks/useSubStep/types';
import {parsePhoneNumber} from '@libs/PhoneNumber';
import {getBankAccountIDAsNumber} from '@libs/ReimbursementAccountUtils';
import {isValidWebsite} from '@libs/ValidationUtils';
import getInitialSubStepForBusinessInfo from '@pages/ReimbursementAccount/USD/utils/getInitialSubStepForBusinessInfo';
import getSubStepValues from '@pages/ReimbursementAccount/utils/getSubStepValues';
Expand Down Expand Up @@ -59,13 +60,14 @@ function BusinessInfo({onBackButtonPress}: BusinessInfoProps) {
);

const policyID = reimbursementAccount?.achData?.policyID;
const bankAccountID = getBankAccountIDAsNumber(reimbursementAccount?.achData);
const values = useMemo(() => getSubStepValues(BUSINESS_INFO_STEP_KEYS, reimbursementAccountDraft, reimbursementAccount), [reimbursementAccount, reimbursementAccountDraft]);

const submit = useCallback(
(isConfirmPage: boolean) => {
const companyWebsite = Str.sanitizeURL(values.website, CONST.COMPANY_WEBSITE_DEFAULT_SCHEME);
updateCompanyInformationForBankAccount(
Number(reimbursementAccount?.achData?.bankAccountID ?? CONST.DEFAULT_NUMBER_ID),
bankAccountID,
{
...values,
...getBankAccountFields(['routingNumber', 'accountNumber', 'bankName', 'plaidAccountID', 'plaidAccessToken', 'isSavings']),
Expand All @@ -77,7 +79,7 @@ function BusinessInfo({onBackButtonPress}: BusinessInfoProps) {
isConfirmPage,
);
},
[reimbursementAccount?.achData?.bankAccountID, values, getBankAccountFields, policyID],
[bankAccountID, values, getBankAccountFields, policyID],
);

const isBankAccountVerifying = reimbursementAccount?.achData?.state === CONST.BANK_ACCOUNT.STATE.VERIFYING;
Expand All @@ -91,6 +93,7 @@ function BusinessInfo({onBackButtonPress}: BusinessInfoProps) {
prevScreen,
moveTo,
goToTheLastStep,
// eslint-disable-next-line @typescript-eslint/no-deprecated
} = useSubStep({bodyContent, startFrom, onFinished: () => submit(true), onNextSubStep: () => submit(false)});

const handleBackButtonPress = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useSubStep from '@hooks/useSubStep';
import type {SubStepProps} from '@hooks/useSubStep/types';
import {getBankAccountIDAsNumber} from '@libs/ReimbursementAccountUtils';
import getSubStepValues from '@pages/ReimbursementAccount/utils/getSubStepValues';
import {acceptACHContractForBankAccount} from '@userActions/BankAccounts';
import CONST from '@src/CONST';
Expand All @@ -29,10 +30,11 @@ function CompleteVerification({onBackButtonPress}: CompleteVerificationProps) {

const values = useMemo(() => getSubStepValues(COMPLETE_VERIFICATION_KEYS, reimbursementAccountDraft, reimbursementAccount), [reimbursementAccount, reimbursementAccountDraft]);
const policyID = reimbursementAccount?.achData?.policyID;
const bankAccountID = getBankAccountIDAsNumber(reimbursementAccount?.achData);

const submit = useCallback(() => {
acceptACHContractForBankAccount(
Number(reimbursementAccount?.achData?.bankAccountID),
bankAccountID,
{
isAuthorizedToUseBankAccount: values.isAuthorizedToUseBankAccount,
certifyTrueInformation: values.certifyTrueInformation,
Expand All @@ -41,7 +43,7 @@ function CompleteVerification({onBackButtonPress}: CompleteVerificationProps) {
policyID,
policyID ? lastPaymentMethod?.[policyID] : undefined,
);
}, [reimbursementAccount?.achData?.bankAccountID, values.isAuthorizedToUseBankAccount, values.certifyTrueInformation, values.acceptTermsAndConditions, policyID, lastPaymentMethod]);
}, [bankAccountID, values.isAuthorizedToUseBankAccount, values.certifyTrueInformation, values.acceptTermsAndConditions, policyID, lastPaymentMethod]);

// eslint-disable-next-line @typescript-eslint/no-deprecated
const {componentToRender: SubStep, isEditing, screenIndex, nextScreen, prevScreen, moveTo, goToTheLastStep} = useSubStep({bodyContent, startFrom: 0, onFinished: submit});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import {validateBankAccount} from '@libs/actions/BankAccounts';
import getPermittedDecimalSeparator from '@libs/getPermittedDecimalSeparator';
import {getBankAccountIDAsNumber} from '@libs/ReimbursementAccountUtils';
import {isRequiredFulfilled} from '@libs/ValidationUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -61,6 +62,7 @@ function BankAccountValidationForm({requiresTwoFactorAuth, reimbursementAccount,
const styles = useThemeStyles();

const policyID = reimbursementAccount?.achData?.policyID;
const bankAccountID = getBankAccountIDAsNumber(reimbursementAccount?.achData);
const decimalSeparator = toLocaleDigit('.');
const permittedDecimalSeparator = getPermittedDecimalSeparator(decimalSeparator);
const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM> => {
Expand Down Expand Up @@ -90,12 +92,11 @@ function BankAccountValidationForm({requiresTwoFactorAuth, reimbursementAccount,
const validateCode = [amount1, amount2, amount3].join(',');

// Send valid amounts to BankAccountAPI::validateBankAccount in Web-Expensify
const bankAccountID = Number(reimbursementAccount?.achData?.bankAccountID ?? CONST.DEFAULT_NUMBER_ID);
if (bankAccountID) {
validateBankAccount(bankAccountID, validateCode, policyID);
}
},
[reimbursementAccount?.achData?.bankAccountID, policyID, permittedDecimalSeparator],
[bankAccountID, policyID, permittedDecimalSeparator],
);
// On android autoCapitalize="words" is necessary when keyboardType="decimal-pad" or inputMode="decimal" to prevent input lag.
// See https://github.com/Expensify/App/issues/51868 for more information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useSubStep from '@hooks/useSubStep';
import type {SubStepProps} from '@hooks/useSubStep/types';
import {getBankAccountIDAsNumber} from '@libs/ReimbursementAccountUtils';
import getInitialSubStepForPersonalInfo from '@pages/ReimbursementAccount/USD/utils/getInitialSubStepForPersonalInfo';
import getSubStepValues from '@pages/ReimbursementAccount/utils/getSubStepValues';
import {updatePersonalInformationForBankAccount} from '@userActions/BankAccounts';
Expand Down Expand Up @@ -37,7 +38,7 @@ function PersonalInfo({onBackButtonPress, ref}: PersonalInfoProps) {

const policyID = reimbursementAccount?.achData?.policyID;
const values = useMemo(() => getSubStepValues(PERSONAL_INFO_STEP_KEYS, reimbursementAccountDraft, reimbursementAccount), [reimbursementAccount, reimbursementAccountDraft]);
const bankAccountID = Number(reimbursementAccount?.achData?.bankAccountID);
const bankAccountID = getBankAccountIDAsNumber(reimbursementAccount?.achData);
const submit = useCallback(
(isConfirmPage: boolean) => {
updatePersonalInformationForBankAccount(bankAccountID, {...values}, policyID, isConfirmPage);
Expand All @@ -55,6 +56,7 @@ function PersonalInfo({onBackButtonPress, ref}: PersonalInfoProps) {
prevScreen,
moveTo,
goToTheLastStep,
// eslint-disable-next-line @typescript-eslint/no-deprecated
} = useSubStep({bodyContent, startFrom, onFinished: () => submit(true), onNextSubStep: () => submit(false)});

const handleBackButtonPress = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function VerifiedBankAccountFlowEntryPoint({

const personalBankAccounts = bankAccountList ? Object.keys(bankAccountList).filter((key) => bankAccountList[key].accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT) : [];

const removeExistingBankAccountDetails = () => {
const removeExistingBankAccountDetails = useCallback(() => {
const bankAccountData: Partial<ReimbursementAccountForm> = {
[bankInfoStepKeys.ROUTING_NUMBER]: '',
[bankInfoStepKeys.ACCOUNT_NUMBER]: '',
Expand All @@ -118,7 +118,7 @@ function VerifiedBankAccountFlowEntryPoint({
};
updateReimbursementAccountDraft(bankAccountData);
updateReimbursementAccount({bankAccountID: 0});
};
}, []);

/**
* Prepares and redirects user to next step in the USD flow
Expand Down Expand Up @@ -149,14 +149,16 @@ function VerifiedBankAccountFlowEntryPoint({
return;
}

removeExistingBankAccountDetails();
prepareNextStep(CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL);
setReimbursementAccountOptionPressed(CONST.BANK_ACCOUNT.SETUP_TYPE.NONE);
} else if (reimbursementAccountOptionPressed === CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID) {
removeExistingBankAccountDetails();
openPlaidView();
prepareNextStep(CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID);
setReimbursementAccountOptionPressed(CONST.BANK_ACCOUNT.SETUP_TYPE.NONE);
}
}, [isAccountValidated, isNonUSDWorkspace, prepareNextStep, reimbursementAccountOptionPressed, setNonUSDBankAccountStep]);
}, [isAccountValidated, isNonUSDWorkspace, prepareNextStep, reimbursementAccountOptionPressed, removeExistingBankAccountDetails, setNonUSDBankAccountStep]);

const handleConnectManually = () => {
if (!isAccountValidated) {
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/BankAccountsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {write} from '@libs/API';
import {WRITE_COMMANDS} from '@libs/API/types';
import {connectBankAccountManually, connectBankAccountWithPlaid} from '@userActions/BankAccounts';
import CONST from '@src/CONST';
import type PlaidBankAccount from '@src/types/onyx/PlaidBankAccount';

jest.mock('@libs/API');

const mockWrite = jest.mocked(write);

const policyID = 'policy123';

const selectedPlaidBankAccount: PlaidBankAccount = {
accountNumber: '123456789',
plaidAccountID: 'plaid-account-id',
routingNumber: '021000021',
mask: '6789',
plaidAccessToken: 'plaid-access-token',
bankName: 'Test Bank',
isSavings: false,
};

describe('BankAccounts', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('connectBankAccountWithPlaid', () => {
test('sends DEFAULT_NUMBER_ID when bankAccountID is NaN', () => {
// Given NaN for bankAccountID from the VBBA flow (e.g. undefined achData.bankAccountID)
// When connecting with Plaid
connectBankAccountWithPlaid(Number.NaN, selectedPlaidBankAccount, policyID);

// Then the API payload must not contain NaN
expect(mockWrite).toHaveBeenCalledWith(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID, expect.objectContaining({bankAccountID: CONST.DEFAULT_NUMBER_ID}), expect.anything());
});

test('passes through finite bankAccountID unchanged', () => {
const existingID = 4242;
connectBankAccountWithPlaid(existingID, selectedPlaidBankAccount, policyID);

expect(mockWrite).toHaveBeenCalledWith(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID, expect.objectContaining({bankAccountID: existingID}), expect.anything());
});
});

describe('connectBankAccountManually', () => {
test('sends DEFAULT_NUMBER_ID when bankAccountID is NaN', () => {
connectBankAccountManually(Number.NaN, selectedPlaidBankAccount, policyID);

expect(mockWrite).toHaveBeenCalledWith(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_MANUALLY, expect.objectContaining({bankAccountID: CONST.DEFAULT_NUMBER_ID}), expect.anything());
});

test('passes through finite bankAccountID unchanged', () => {
const existingID = 99;
connectBankAccountManually(existingID, selectedPlaidBankAccount, policyID);

expect(mockWrite).toHaveBeenCalledWith(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_MANUALLY, expect.objectContaining({bankAccountID: existingID}), expect.anything());
});
});
});
Loading
Loading