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
43 changes: 41 additions & 2 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<PersonalBankAccount>) {
function deletePaymentBankAccount(
bankAccountID: number,
personalPolicyID: string | undefined,
lastUsedPaymentMethods?: LastPaymentMethod,
bankAccount?: OnyxEntry<PersonalBankAccount>,
newBankAccountID?: number,
) {
const parameters: DeletePaymentBankAccountParams = {bankAccountID};

const bankAccountFailureData = {
Expand All @@ -396,7 +403,7 @@ function deletePaymentBankAccount(bankAccountID: number, personalPolicyID: strin
pendingAction: null,
};

const onyxData: OnyxData<typeof ONYXKEYS.BANK_ACCOUNT_LIST | typeof ONYXKEYS.NVP_LAST_PAYMENT_METHOD> = {
const onyxData: OnyxData<typeof ONYXKEYS.BANK_ACCOUNT_LIST | typeof ONYXKEYS.NVP_LAST_PAYMENT_METHOD | typeof ONYXKEYS.USER_WALLET> = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -426,6 +433,38 @@ function deletePaymentBankAccount(bankAccountID: number, personalPolicyID: strin
],
};

if (newBankAccountID) {
const newDefaultPaymentMethodOnyxData = getMakeDefaultPaymentOnyxData(newBankAccountID);
Comment thread
daledah marked this conversation as resolved.
onyxData.optimisticData?.push(
...(newDefaultPaymentMethodOnyxData as Array<OnyxUpdate<typeof ONYXKEYS.BANK_ACCOUNT_LIST | typeof ONYXKEYS.NVP_LAST_PAYMENT_METHOD | typeof ONYXKEYS.USER_WALLET>>),
Comment thread
daledah marked this conversation as resolved.
);
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;

Expand Down
3 changes: 2 additions & 1 deletion src/libs/actions/PaymentMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getPaymentMethods(includePartiallySetupBankAccounts?: boolean) {

function getMakeDefaultPaymentOnyxData(
bankAccountID: number,
fundID: number,
fundID?: number,
previousPaymentMethod?: PaymentMethod,
currentPaymentMethod?: PaymentMethod,
isOptimisticData = true,
Expand Down Expand Up @@ -578,6 +578,7 @@ export {
addPaymentCard,
getPaymentMethods,
makeDefaultPaymentMethod,
getMakeDefaultPaymentOnyxData,
continueSetup,
addSubscriptionPaymentCard,
clearPaymentCardFormErrorAndSubmit,
Expand Down
42 changes: 41 additions & 1 deletion src/pages/settings/Wallet/WalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment thread
daledah marked this conversation as resolved.
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);
}
Expand All @@ -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,
]);
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/AccountData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading