diff --git a/src/libs/actions/BankAccounts.js b/src/libs/actions/BankAccounts.js index d56db2ae1c65..2ed82a6e60f1 100644 --- a/src/libs/actions/BankAccounts.js +++ b/src/libs/actions/BankAccounts.js @@ -1,5 +1,6 @@ import _ from 'underscore'; import Onyx from 'react-native-onyx'; +import lodashGet from 'lodash/get'; import CONST from '../../CONST'; import * as DeprecatedAPI from '../deprecatedAPI'; import * as Plaid from './Plaid'; @@ -8,6 +9,7 @@ import ONYXKEYS from '../../ONYXKEYS'; import * as PaymentMethods from './PaymentMethods'; import Growl from '../Growl'; import * as Localize from '../Localize'; +import * as store from './ReimbursementAccount/store'; export { setupWithdrawalAccount, @@ -103,6 +105,14 @@ function addPersonalBankAccount(account, password, plaidLinkToken) { * @param {Number} bankAccountID */ function deleteBankAccount(bankAccountID) { + const reimbursementBankAccountId = lodashGet(store.getReimbursementAccountInSetup(), 'bankAccountID'); + + // If the account is same, then delete function is called in `resetFreePlanBankAccount`, hence early returned + if (reimbursementBankAccountId === bankAccountID) { + ReimbursementAccount.resetFreePlanBankAccount(); + return; + } + DeprecatedAPI.DeleteBankAccount({ bankAccountID, }).then((response) => { diff --git a/src/libs/actions/PaymentMethods.js b/src/libs/actions/PaymentMethods.js index af54f858a40a..987095e63a72 100644 --- a/src/libs/actions/PaymentMethods.js +++ b/src/libs/actions/PaymentMethods.js @@ -11,6 +11,7 @@ import Navigation from '../Navigation/Navigation'; import * as CardUtils from '../CardUtils'; import ROUTES from '../../ROUTES'; import NameValuePair from './NameValuePair'; +import * as store from './ReimbursementAccount/store'; /** * Deletes a debit card @@ -59,6 +60,19 @@ function continueSetup() { kycWallRef.current.continue(); } +/** + * Clears local reimbursement account if it doesn't exist in bankAccounts + * @param {Object[]} bankAccounts + */ +function cleanLocalReimbursementData(bankAccounts) { + const bankAccountID = lodashGet(store.getReimbursementAccountInSetup(), 'bankAccountID'); + + // We check if the bank account list doesn't have the reimbursementAccount + if (!_.find(bankAccounts, bankAccount => bankAccount.bankAccountID === bankAccountID)) { + Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {achData: null, shouldShowResetModal: false}); + } +} + /** * Calls the API to get the user's bankAccountList, cardList, wallet, and payPalMe * @@ -77,6 +91,7 @@ function getPaymentMethods() { // Convert bank accounts/cards from an array of objects, to a map with the bankAccountID as the key const bankAccounts = _.object(_.map(lodashGet(response, 'bankAccountList', []), bankAccount => [bankAccount.bankAccountID, bankAccount])); const debitCards = _.object(_.map(lodashGet(response, 'fundList', []), fund => [fund.fundID, fund])); + cleanLocalReimbursementData(bankAccounts); Onyx.multiSet({ [ONYXKEYS.IS_LOADING_PAYMENT_METHODS]: false, [ONYXKEYS.USER_WALLET]: lodashGet(response, 'userWallet', {}), @@ -273,4 +288,5 @@ export { saveWalletTransferAccountTypeAndID, saveWalletTransferMethodType, dismissWalletConfirmModal, + cleanLocalReimbursementData, };