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
30 changes: 20 additions & 10 deletions src/libs/PaymentUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, BankAccount>): Record<string, BankAccount> {
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<string, Fund>, bankAccountList: Record<string, BankAccount>, 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);
Expand Down Expand Up @@ -357,6 +366,7 @@ function getActivePaymentType(

export {
hasExpensifyPaymentMethod,
getWalletTransferBankAccounts,
getPaymentMethodDescription,
formatPaymentMethods,
getBusinessBankAccountOptions,
Expand Down
3 changes: 2 additions & 1 deletion src/pages/settings/Wallet/ChooseTransferAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -68,7 +69,7 @@ function ChooseTransferAccountPage() {
const [draftAccountID, setDraftAccountID] = useState<string>();
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;
Expand Down
6 changes: 3 additions & 3 deletions src/pages/settings/Wallet/TransferBalancePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
54 changes: 52 additions & 2 deletions tests/unit/PaymentUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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';

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';

Expand Down Expand Up @@ -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<string, BankAccount> =>
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({});
});
});
});
Loading