diff --git a/src/libs/PaymentUtils.ts b/src/libs/PaymentUtils.ts index 1251f0341fbd..47c47895a322 100644 --- a/src/libs/PaymentUtils.ts +++ b/src/libs/PaymentUtils.ts @@ -72,20 +72,29 @@ type BusinessBankAccountOption = { methodID: number | undefined; }; +/** + * Auth rejects a wallet transfer to a bank account that is not open or not personal. The US check matches hasExpensifyPaymentMethod. + */ +function isWalletTransferBankAccount(bankAccountJSON: BankAccount): boolean { + const bankAccount = new BankAccountModel(bankAccountJSON); + + return ( + bankAccount.getPendingAction() !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && + bankAccount.isOpen() && + bankAccount.getType() === CONST.BANK_ACCOUNT.TYPE.PERSONAL && + bankAccount.getCountry() === CONST.COUNTRY.US + ); +} + +function getWalletTransferBankAccounts(bankAccountList: Record): Record { + return Object.fromEntries(Object.entries(bankAccountList).filter(([, bankAccount]) => isWalletTransferBankAccount(bankAccount))); +} + /** * Check to see if user has either a debit card or personal US bank account added that can be used with a wallet. */ function hasExpensifyPaymentMethod(fundList: Record, bankAccountList: Record, shouldIncludeDebitCard = true): boolean { - const validBankAccount = Object.values(bankAccountList).some((bankAccountJSON) => { - const bankAccount = new BankAccountModel(bankAccountJSON); - - return ( - bankAccount.getPendingAction() !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && - bankAccount.isOpen() && - bankAccount.getType() === CONST.BANK_ACCOUNT.TYPE.PERSONAL && - bankAccount?.getCountry() === CONST.COUNTRY.US - ); - }); + const validBankAccount = Object.values(bankAccountList).some(isWalletTransferBankAccount); // Hide any billing cards that are not P2P debit cards for now because you cannot make them your default method, or delete them const validDebitCard = Object.values(fundList).some((card) => card?.accountData?.additionalData?.isP2PDebitCard ?? false); @@ -357,6 +366,7 @@ function getActivePaymentType( export { hasExpensifyPaymentMethod, + getWalletTransferBankAccounts, getPaymentMethodDescription, formatPaymentMethods, getBusinessBankAccountOptions, diff --git a/src/pages/settings/Wallet/ChooseTransferAccountPage.tsx b/src/pages/settings/Wallet/ChooseTransferAccountPage.tsx index d3bb5dd7d444..dbaa3c85a327 100644 --- a/src/pages/settings/Wallet/ChooseTransferAccountPage.tsx +++ b/src/pages/settings/Wallet/ChooseTransferAccountPage.tsx @@ -15,6 +15,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; import {getLastFourDigits} from '@libs/BankAccountUtils'; import Navigation from '@libs/Navigation/Navigation'; +import {getWalletTransferBankAccounts} from '@libs/PaymentUtils'; import {openPersonalBankAccountSetupView} from '@userActions/BankAccounts'; import {saveWalletTransferAccountTypeAndID} from '@userActions/PaymentMethods'; @@ -68,7 +69,7 @@ function ChooseTransferAccountPage() { const [draftAccountID, setDraftAccountID] = useState(); const currentSelectedAccountID = draftAccountID ?? selectedAccountID; - const options = Object.values(bankAccountsList ?? {}).map((bankAccount, index): BankAccountListItem => { + const options = Object.values(getWalletTransferBankAccounts(bankAccountsList ?? {})).map((bankAccount, index): BankAccountListItem => { const bankName = (bankAccount.accountData?.additionalData?.bankName ?? '') as BankName; const bankAccountNumber = bankAccount.accountData?.accountNumber ?? ''; const bankAccountID = bankAccount.accountData?.bankAccountID ?? bankAccount.methodID; diff --git a/src/pages/settings/Wallet/TransferBalancePage.tsx b/src/pages/settings/Wallet/TransferBalancePage.tsx index 7ddf802646cf..f04a8319dc0b 100644 --- a/src/pages/settings/Wallet/TransferBalancePage.tsx +++ b/src/pages/settings/Wallet/TransferBalancePage.tsx @@ -25,7 +25,7 @@ import { } from '@libs/actions/PaymentMethods'; import {getLatestErrorMessage} from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; -import {calculateWalletTransferBalanceFee, formatPaymentMethods, hasExpensifyPaymentMethod} from '@libs/PaymentUtils'; +import {calculateWalletTransferBalanceFee, formatPaymentMethods, getWalletTransferBankAccounts, hasExpensifyPaymentMethod} from '@libs/PaymentUtils'; import variables from '@styles/variables'; @@ -80,7 +80,7 @@ function TransferBalancePage() { * Get the selected/default payment method account for wallet transfer */ function getSelectedPaymentMethodAccount(): PaymentMethod | undefined { - const paymentMethods = formatPaymentMethods(bankAccountList ?? {}, paymentCardList, styles, translate); + const paymentMethods = formatPaymentMethods(getWalletTransferBankAccounts(bankAccountList ?? {}), paymentCardList, styles, translate); const defaultAccount = paymentMethods.find((method) => method.isDefault); const selectedAccount = paymentMethods.find( @@ -93,7 +93,7 @@ function TransferBalancePage() { saveWalletTransferMethodType(filterPaymentMethodType); // If we only have a single option for the given paymentMethodType do not force the user to make a selection - const combinedPaymentMethods = formatPaymentMethods(bankAccountList ?? {}, paymentCardList, styles, translate); + const combinedPaymentMethods = formatPaymentMethods(getWalletTransferBankAccounts(bankAccountList ?? {}), paymentCardList, styles, translate); const filteredMethods = combinedPaymentMethods.filter((paymentMethod) => paymentMethod.accountType === filterPaymentMethodType); if (filteredMethods.length === 1) { diff --git a/tests/unit/PaymentUtilsTest.ts b/tests/unit/PaymentUtilsTest.ts index 4a1ecef8ca40..289defc3e117 100644 --- a/tests/unit/PaymentUtilsTest.ts +++ b/tests/unit/PaymentUtilsTest.ts @@ -2,7 +2,7 @@ import type {BankAccountMenuItem} from '@components/Search/types'; import {approveMoneyRequest} from '@libs/actions/IOU/ReportWorkflow'; import Navigation from '@libs/Navigation/Navigation'; -import {getActivePaymentType, getBusinessBankAccountOptions, isSecondaryActionAPaymentOption, selectPaymentType} from '@libs/PaymentUtils'; +import {getActivePaymentType, getBusinessBankAccountOptions, getWalletTransferBankAccounts, isSecondaryActionAPaymentOption, selectPaymentType} from '@libs/PaymentUtils'; import type {SelectPaymentTypeParams} from '@libs/PaymentUtils'; import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; @@ -10,7 +10,7 @@ import CONST from '@src/CONST'; import {calculateWalletTransferBalanceFee} from '@src/libs/PaymentUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; -import type {Report} from '@src/types/onyx'; +import type {BankAccount, Report} from '@src/types/onyx'; import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import type PaymentMethod from '@src/types/onyx/PaymentMethod'; @@ -644,4 +644,54 @@ describe('PaymentUtils', () => { expect(resultWithUnknownDefault.map((option) => option.methodID)).toEqual([1, 2]); }); }); + + describe('getWalletTransferBankAccounts', () => { + type BankAccountOptions = { + type: string; + state?: string; + country?: string; + pendingAction?: BankAccount['pendingAction']; + }; + + const createBankAccount = (bankAccountID: number, {type, state = CONST.BANK_ACCOUNT.STATE.OPEN, country = CONST.COUNTRY.US, pendingAction}: BankAccountOptions): BankAccount => ({ + accountType: CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT, + bankCurrency: CONST.CURRENCY.USD, + bankCountry: CONST.COUNTRY.US, + methodID: bankAccountID, + pendingAction, + accountData: {bankAccountID, type, state, additionalData: {country}}, + }); + + const toBankAccountList = (bankAccounts: BankAccount[]): Record => + Object.fromEntries(bankAccounts.map((bankAccount) => [String(bankAccount.methodID), bankAccount])); + + it('keeps only open personal US accounts', () => { + // Given a personal account next to a business account, the wallet pseudo-account, a pending personal account and a non-US personal account + const personal = createBankAccount(1, {type: CONST.BANK_ACCOUNT.TYPE.PERSONAL}); + const bankAccountList = toBankAccountList([ + personal, + createBankAccount(2, {type: CONST.BANK_ACCOUNT.TYPE.BUSINESS}), + createBankAccount(3, {type: CONST.BANK_ACCOUNT_TYPES.WALLET}), + createBankAccount(4, {type: CONST.BANK_ACCOUNT.TYPE.PERSONAL, state: CONST.BANK_ACCOUNT.STATE.PENDING}), + createBankAccount(5, {type: CONST.BANK_ACCOUNT.TYPE.PERSONAL, country: 'GB'}), + ]); + + // When the wallet transfer destinations are picked, since Auth rejects accounts that are not open or not personal and hasExpensifyPaymentMethod only counts US ones + const result = getWalletTransferBankAccounts(bankAccountList); + + // Then only the personal account remains, so the business one can't be offered or auto-selected + expect(result).toEqual(toBankAccountList([personal])); + }); + + it('drops a personal account pending deletion', () => { + // Given a personal account the user just deleted offline + const bankAccountList = toBankAccountList([createBankAccount(1, {type: CONST.BANK_ACCOUNT.TYPE.PERSONAL, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE})]); + + // When the wallet transfer destinations are picked + const result = getWalletTransferBankAccounts(bankAccountList); + + // Then it is not offered, matching hasExpensifyPaymentMethod which hides the transfer view in that state + expect(result).toEqual({}); + }); + }); });