diff --git a/src/libs/actions/BankAccounts.ts b/src/libs/actions/BankAccounts.ts index 459de1a27c5f..331ed33c508a 100644 --- a/src/libs/actions/BankAccounts.ts +++ b/src/libs/actions/BankAccounts.ts @@ -38,6 +38,7 @@ import type {BankAccountList, LastPaymentMethod, LastPaymentMethodType, Personal import type PlaidBankAccount from '@src/types/onyx/PlaidBankAccount'; import type {BankAccountStep, ReimbursementAccountStep, ReimbursementAccountSubStep} from '@src/types/onyx/ReimbursementAccount'; import type {OnyxData} from '@src/types/onyx/Request'; +import {getMakeDefaultPaymentOnyxData} from './PaymentMethods'; import {setBankAccountSubStep} from './ReimbursementAccount'; export { @@ -387,7 +388,13 @@ function addPersonalBankAccount( API.write(WRITE_COMMANDS.ADD_PERSONAL_BANK_ACCOUNT, parameters, onyxData); } -function deletePaymentBankAccount(bankAccountID: number, personalPolicyID: string | undefined, lastUsedPaymentMethods?: LastPaymentMethod, bankAccount?: OnyxEntry) { +function deletePaymentBankAccount( + bankAccountID: number, + personalPolicyID: string | undefined, + lastUsedPaymentMethods?: LastPaymentMethod, + bankAccount?: OnyxEntry, + newBankAccountID?: number, +) { const parameters: DeletePaymentBankAccountParams = {bankAccountID}; const bankAccountFailureData = { @@ -396,7 +403,7 @@ function deletePaymentBankAccount(bankAccountID: number, personalPolicyID: strin pendingAction: null, }; - const onyxData: OnyxData = { + const onyxData: OnyxData = { optimisticData: [ { onyxMethod: Onyx.METHOD.MERGE, @@ -426,6 +433,38 @@ function deletePaymentBankAccount(bankAccountID: number, personalPolicyID: strin ], }; + if (newBankAccountID) { + const newDefaultPaymentMethodOnyxData = getMakeDefaultPaymentOnyxData(newBankAccountID); + onyxData.optimisticData?.push( + ...(newDefaultPaymentMethodOnyxData as Array>), + ); + onyxData.optimisticData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.BANK_ACCOUNT_LIST, + value: { + [newBankAccountID]: { + isDefault: true, + }, + }, + }); + onyxData.failureData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.USER_WALLET, + value: { + walletLinkedAccountID: bankAccountID, + walletLinkedAccountType: CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT, + }, + }); + onyxData.failureData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.BANK_ACCOUNT_LIST, + value: { + [newBankAccountID]: { + isDefault: false, + }, + }, + }); + } for (const paymentMethodID of Object.keys(lastUsedPaymentMethods ?? {})) { const lastUsedPaymentMethod = lastUsedPaymentMethods?.[paymentMethodID] as LastPaymentMethodType; diff --git a/src/libs/actions/PaymentMethods.ts b/src/libs/actions/PaymentMethods.ts index 117edc0e6a9b..a860f5b734a2 100644 --- a/src/libs/actions/PaymentMethods.ts +++ b/src/libs/actions/PaymentMethods.ts @@ -78,7 +78,7 @@ function getPaymentMethods(includePartiallySetupBankAccounts?: boolean) { function getMakeDefaultPaymentOnyxData( bankAccountID: number, - fundID: number, + fundID?: number, previousPaymentMethod?: PaymentMethod, currentPaymentMethod?: PaymentMethod, isOptimisticData = true, @@ -578,6 +578,7 @@ export { addPaymentCard, getPaymentMethods, makeDefaultPaymentMethod, + getMakeDefaultPaymentOnyxData, continueSetup, addSubscriptionPaymentCard, clearPaymentCardFormErrorAndSubmit, diff --git a/src/pages/settings/Wallet/WalletPage/index.tsx b/src/pages/settings/Wallet/WalletPage/index.tsx index 4b21f3b778d6..c2fb0fea7649 100644 --- a/src/pages/settings/Wallet/WalletPage/index.tsx +++ b/src/pages/settings/Wallet/WalletPage/index.tsx @@ -234,9 +234,45 @@ function WalletPage() { const deletePaymentMethod = useCallback(() => { const bankAccountID = paymentMethod.selectedPaymentMethod.bankAccountID; const fundID = paymentMethod.selectedPaymentMethod.fundID; + let newBankAccountID: number | undefined; + if (paymentMethod.isSelectedPaymentMethodDefault) { + const paymentCardList = fundList ?? {}; + const allPaymentMethods = formatPaymentMethods(bankAccountList ?? {}, paymentCardList, styles, translate); + + const remainingPaymentMethods = allPaymentMethods + .filter( + (method) => + method.methodID !== paymentMethod.methodID && + method.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT && + method.accountData?.type !== CONST.BANK_ACCOUNT.TYPE.BUSINESS && + method.accountData?.state === CONST.BANK_ACCOUNT.STATE.OPEN && + method.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + ) + .sort((a, b) => { + const aCreated = a.accountData?.created ?? ''; + const bCreated = b.accountData?.created ?? ''; + if (!aCreated && !bCreated) { + return 0; + } + if (!aCreated) { + return 1; + } + if (!bCreated) { + return -1; + } + return new Date(bCreated).getTime() - new Date(aCreated).getTime(); + }); + + if (remainingPaymentMethods.length > 0) { + const newDefaultMethod = remainingPaymentMethods.at(0); + newBankAccountID = + newDefaultMethod?.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT ? (newDefaultMethod?.accountData?.bankAccountID ?? CONST.DEFAULT_NUMBER_ID) : undefined; + } + } + if (paymentMethod.selectedPaymentMethodType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT && bankAccountID) { const bankAccount = bankAccountList?.[paymentMethod.methodID] ?? {}; - deletePaymentBankAccount(bankAccountID, personalPolicyID, lastUsedPaymentMethods, bankAccount); + deletePaymentBankAccount(bankAccountID, personalPolicyID, lastUsedPaymentMethods, bankAccount, newBankAccountID); } else if (paymentMethod.selectedPaymentMethodType === CONST.PAYMENT_METHODS.DEBIT_CARD && fundID) { deletePaymentCard(fundID); } @@ -245,10 +281,14 @@ function WalletPage() { }, [ paymentMethod.selectedPaymentMethod.bankAccountID, paymentMethod.selectedPaymentMethod.fundID, + paymentMethod.isSelectedPaymentMethodDefault, paymentMethod.selectedPaymentMethodType, paymentMethod.methodID, resetSelectedPaymentMethodData, + fundList, bankAccountList, + styles, + translate, lastUsedPaymentMethods, personalPolicyID, ]); diff --git a/src/types/onyx/AccountData.ts b/src/types/onyx/AccountData.ts index 6bb69cd78dc4..ec1ed1df723d 100644 --- a/src/types/onyx/AccountData.ts +++ b/src/types/onyx/AccountData.ts @@ -9,6 +9,9 @@ type AccountData = { /** The name of the institution (bank of america, etc */ addressName?: string; + /** Date when the bank account was created */ + created?: string; + /** Can we use this account to pay other people? */ allowDebit?: boolean;