diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index dd0e2e0239dc..8b0788066630 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -29,6 +29,7 @@ import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; import SCREENS from '@src/SCREENS'; import type { + BankAccountList, Beta, IntroSelected, NewGroupChatDraft, @@ -9838,7 +9839,12 @@ function isAllowedToSubmitDraftExpenseReport(report: OnyxEntry): boolean /** * What missing payment method does this report action indicate, if any? */ -function getIndicatedMissingPaymentMethod(userWalletTierName: string | undefined, reportId: string | undefined, reportAction: ReportAction): MissingPaymentMethod | undefined { +function getIndicatedMissingPaymentMethod( + userWalletTierName: string | undefined, + reportId: string | undefined, + reportAction: ReportAction, + bankAccountList: OnyxEntry, +): MissingPaymentMethod | undefined { const isSubmitterOfUnsettledReport = reportId && isCurrentUserSubmitter(getReport(reportId, allReports)) && !isSettled(reportId); if (!reportId || !isSubmitterOfUnsettledReport || !isReimbursementQueuedAction(reportAction)) { return undefined; @@ -9848,17 +9854,17 @@ function getIndicatedMissingPaymentMethod(userWalletTierName: string | undefined return !userWalletTierName || userWalletTierName === CONST.WALLET.TIER_NAME.SILVER ? 'wallet' : undefined; } - return !hasCreditBankAccount() ? 'bankAccount' : undefined; + return !hasCreditBankAccount(bankAccountList) ? 'bankAccount' : undefined; } /** * Checks if report chat contains missing payment method */ -function hasMissingPaymentMethod(userWalletTierName: string | undefined, iouReportID: string | undefined): boolean { +function hasMissingPaymentMethod(userWalletTierName: string | undefined, iouReportID: string | undefined, bankAccountList: OnyxEntry): boolean { const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`] ?? {}; return Object.values(reportActions) .filter(Boolean) - .some((action) => getIndicatedMissingPaymentMethod(userWalletTierName, iouReportID, action) !== undefined); + .some((action) => getIndicatedMissingPaymentMethod(userWalletTierName, iouReportID, action, bankAccountList) !== undefined); } /** diff --git a/src/libs/actions/ReimbursementAccount/store.ts b/src/libs/actions/ReimbursementAccount/store.ts index bc298313ad1e..a92778fc50a3 100644 --- a/src/libs/actions/ReimbursementAccount/store.ts +++ b/src/libs/actions/ReimbursementAccount/store.ts @@ -1,18 +1,8 @@ import type {OnyxEntry} from 'react-native-onyx'; -import Onyx from 'react-native-onyx'; import BankAccount from '@libs/models/BankAccount'; -import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; -let bankAccountList: OnyxEntry; -Onyx.connect({ - key: ONYXKEYS.BANK_ACCOUNT_LIST, - callback: (val) => { - bankAccountList = val; - }, -}); - -function hasCreditBankAccount(): boolean { +function hasCreditBankAccount(bankAccountList: OnyxEntry): boolean { if (!bankAccountList) { return false; } diff --git a/src/pages/home/report/ReportActionItem.tsx b/src/pages/home/report/ReportActionItem.tsx index 5223b8ba2077..bad05ffd94b2 100644 --- a/src/pages/home/report/ReportActionItem.tsx +++ b/src/pages/home/report/ReportActionItem.tsx @@ -96,12 +96,13 @@ function ReportActionItem({ const [currentUserAccountID] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false, selector: (session) => session?.accountID}); const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${getIOUReportIDFromReportActionPreview(action)}`]; const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]; + const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true}); // The app would crash due to subscribing to the entire report collection if parentReportID is an empty string. So we should have a fallback ID here. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID || undefined}`]; const blockedFromConcierge = useBlockedFromConcierge(); const targetReport = isChatThread(report) ? parentReport : report; - const missingPaymentMethod = getIndicatedMissingPaymentMethod(userWalletTierName, targetReport?.reportID, action); + const missingPaymentMethod = getIndicatedMissingPaymentMethod(userWalletTierName, targetReport?.reportID, action, bankAccountList); const taskReport = originalMessage && 'taskReportID' in originalMessage ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${originalMessage.taskReportID}`] : undefined; const linkedReport = originalMessage && 'linkedReportID' in originalMessage ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${originalMessage.linkedReportID}`] : undefined; diff --git a/tests/unit/ReimbursementAccountTest.ts b/tests/unit/ReimbursementAccountTest.ts new file mode 100644 index 000000000000..cf9babd2af29 --- /dev/null +++ b/tests/unit/ReimbursementAccountTest.ts @@ -0,0 +1,33 @@ +import {hasCreditBankAccount} from '@src/libs/actions/ReimbursementAccount/store'; +import type {BankAccountList} from '@src/types/onyx'; + +describe('ReimbursementAccountTest', () => { + describe('hasCreditBankAccount', () => { + it('should return true if there is a credit bank account', () => { + const BANK_ACCOUNT_LIST: BankAccountList = { + // eslint-disable-next-line @typescript-eslint/naming-convention + '1': {accountData: {defaultCredit: true}, bankCurrency: 'USD', bankCountry: 'US'}, + // eslint-disable-next-line @typescript-eslint/naming-convention + '2': {accountData: {defaultCredit: false}, bankCurrency: 'USD', bankCountry: 'US'}, + }; + + const result = hasCreditBankAccount(BANK_ACCOUNT_LIST); + expect(result).toBe(true); + }); + + it('should return false if there is no credit bank account', () => { + const BANK_ACCOUNT_LIST: BankAccountList = { + // eslint-disable-next-line @typescript-eslint/naming-convention + '1': {accountData: {defaultCredit: false}, bankCurrency: 'USD', bankCountry: 'US'}, + }; + + const result = hasCreditBankAccount(BANK_ACCOUNT_LIST); + expect(result).toBe(false); + }); + + it('should return false if there is no bank account list', () => { + const result = hasCreditBankAccount(undefined); + expect(result).toBe(false); + }); + }); +});