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
8 changes: 6 additions & 2 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,11 @@ function getOnyxDataForConnectingVBBAAndLastPaymentMethod(policyID?: string, las

/**
* Submit Bank Account step with Plaid data so php can perform some checks.
*
* @returns whether a backend request was started. Callers use this to know if they should wait for the
* request to settle before navigating, since the new Chase flow switches to manual entry without any request.
*/
function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAccount: PlaidBankAccount, policyID: string | undefined) {
function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAccount: PlaidBankAccount, policyID: string | undefined): boolean {
const isChaseBank = selectedPlaidBankAccount.bankName?.toLowerCase() === CONST.BANK_NAMES.CHASE;
if (bankAccountID === CONST.DEFAULT_NUMBER_ID && isChaseBank) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {
Expand All @@ -457,7 +460,7 @@ function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAcc
accountNumber: '',
routingNumber: '',
});
return;
return false;
}

const parameters: ConnectBankAccountParams = {
Expand All @@ -473,6 +476,7 @@ function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAcc
};

API.write(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID, parameters, getVBBADataForOnyx());
return true;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/pages/ReimbursementAccount/USD/BankInfo/BankInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ function BankInfo({onBackButtonPress, onSubmit, policyID}: BankInfoProps) {
const bankAccountID = getBankAccountIDAsNumber(reimbursementAccount?.achData);
const submit = (submitData: unknown) => {
const data = submitData as ReimbursementAccountForm;

// Deferred navigation must only be armed when a bank account request was actually sent. The new Chase
// Plaid flow sends nothing and switches to manual entry instead, so advancing there would move the user
// to the next step with no bank account created.
let didStartRequest = false;
if (setupType === CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL) {
connectBankAccountManually(
bankAccountID,
Expand All @@ -73,14 +78,15 @@ function BankInfo({onBackButtonPress, onSubmit, policyID}: BankInfoProps) {
},
policyID,
);
didStartRequest = true;
} else if (setupType === CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID) {
const previousPlaidAccountID = reimbursementAccount?.achData?.plaidAccountID;
const newPlaidAccountID = data[BANK_INFO_STEP_KEYS.PLAID_ACCOUNT_ID];
const plaidAccountIDChanged = !!bankAccountID && !!previousPlaidAccountID && previousPlaidAccountID !== newPlaidAccountID;
if (plaidAccountIDChanged) {
deletePaymentBankAccount(bankAccountID, undefined);
}
connectBankAccountWithPlaid(
didStartRequest = connectBankAccountWithPlaid(
plaidAccountIDChanged ? CONST.DEFAULT_NUMBER_ID : bankAccountID,
{
[BANK_INFO_STEP_KEYS.ROUTING_NUMBER]: data[BANK_INFO_STEP_KEYS.ROUTING_NUMBER] ?? '',
Expand All @@ -94,6 +100,11 @@ function BankInfo({onBackButtonPress, onSubmit, policyID}: BankInfoProps) {
policyID,
);
}

if (!didStartRequest) {
return;
}

markSubmitting();
};

Expand Down
30 changes: 27 additions & 3 deletions tests/actions/BankAccountsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ describe('actions/BankAccounts', () => {
} as Partial<ReimbursementAccountForm>);

// When we connect with Plaid for Chase on a new account
connectBankAccountWithPlaid(CONST.DEFAULT_NUMBER_ID, getPlaidBankAccount(CONST.BANK_NAMES_USER_FRIENDLY[CONST.BANK_NAMES.CHASE]), POLICY_ID);
const didStartRequest = connectBankAccountWithPlaid(CONST.DEFAULT_NUMBER_ID, getPlaidBankAccount(CONST.BANK_NAMES_USER_FRIENDLY[CONST.BANK_NAMES.CHASE]), POLICY_ID);
await waitForBatchedUpdates();

// Then we should not call the backend, and should move user to manual with cleared account/routing draft fields
const reimbursementAccount = await getOnyxValue(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
const reimbursementAccountDraft = await getOnyxValue(ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM_DRAFT);

// ...and report that no request was started, so the caller does not arm deferred navigation
expect(didStartRequest).toBe(false);
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID, 0);
expect(reimbursementAccount?.achData?.currentStep).toBe(CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT);
expect(reimbursementAccount?.achData?.subStep).toBe(CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL);
Expand All @@ -97,9 +99,10 @@ describe('actions/BankAccounts', () => {
const selectedPlaidBankAccount = getPlaidBankAccount(CONST.BANK_NAMES_USER_FRIENDLY[CONST.BANK_NAMES.CHASE]);

// When we connect with Plaid
connectBankAccountWithPlaid(bankAccountID, selectedPlaidBankAccount, POLICY_ID);
const didStartRequest = connectBankAccountWithPlaid(bankAccountID, selectedPlaidBankAccount, POLICY_ID);
return waitForBatchedUpdates().then(() => {
// Then we should call the existing API command
// Then we should call the existing API command and report that a request was started
expect(didStartRequest).toBe(true);
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID, 1);
const call = TestHelper.getFetchMockCalls(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID).at(0);
if (!call) {
Expand Down Expand Up @@ -127,6 +130,27 @@ describe('actions/BankAccounts', () => {
);
});
});

test('does not short-circuit a new account when the bank is not Chase', async () => {
// Given a new (bankAccountID 0) non-Chase bank account in Plaid setup
await Onyx.set(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {
achData: {
currentStep: CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT,
subStep: CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID,
},
});

// When we connect with Plaid
const didStartRequest = connectBankAccountWithPlaid(CONST.DEFAULT_NUMBER_ID, getPlaidBankAccount('Wells Fargo'), POLICY_ID);
await waitForBatchedUpdates();

// Then we should call the API, report that a request was started, and stay in the Plaid sub step
expect(didStartRequest).toBe(true);
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.CONNECT_BANK_ACCOUNT_WITH_PLAID, 1);

const reimbursementAccount = await getOnyxValue(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
expect(reimbursementAccount?.achData?.subStep).toBe(CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID);
});
});

describe('openPersonalBankAccountSetupView', () => {
Expand Down
Loading