diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index ecd0fc26785a..5009645cfb25 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -1,5 +1,5 @@ import Onyx from 'react-native-onyx'; -import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; import type {FormOnyxValues} from '@components/Form/types'; import type {PaymentData, SearchQueryJSON} from '@components/Search/types'; @@ -14,7 +14,7 @@ import {isReportListItemType, isTransactionListItemType} from '@libs/SearchUIUti import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import FILTER_KEYS from '@src/types/form/SearchAdvancedFiltersForm'; -import type {LastPaymentMethod} from '@src/types/onyx'; +import type {LastPaymentMethod, SearchResults} from '@src/types/onyx'; import type {SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults'; import * as Report from './Report'; @@ -34,6 +34,15 @@ Onyx.connect({ }, }); +let allSnapshots: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.SNAPSHOT, + callback: (val) => { + allSnapshots = val; + }, + waitForCollectionCallback: true, +}); + function handleActionButtonPress(hash: number, item: TransactionListItemType | ReportListItemType, goToItem: () => void) { // The transactionID is needed to handle actions taken on `status:all` where transactions on single expense reports can be approved/paid. // We need the transactionID to display the loading indicator for that list item's action. @@ -41,9 +50,7 @@ function handleActionButtonPress(hash: number, item: TransactionListItemType | R switch (item.action) { case CONST.SEARCH.ACTION_TYPES.PAY: { - const lastPolicyPaymentMethod = item.policyID ? (lastPaymentMethod?.[item.policyID] as ValueOf) : null; - const amount = isReportListItemType(item) ? item.total ?? 0 : item.formattedTotal; - return lastPolicyPaymentMethod ? payMoneyRequestOnSearch(hash, [{reportID: item.reportID, amount, paymentType: lastPolicyPaymentMethod}], transactionID) : goToItem(); + return getPayActionCallback(hash, item, goToItem); } case CONST.SEARCH.ACTION_TYPES.APPROVE: return approveMoneyRequestOnSearch(hash, [item.reportID], transactionID); @@ -52,6 +59,24 @@ function handleActionButtonPress(hash: number, item: TransactionListItemType | R } } +function getPayActionCallback(hash: number, item: TransactionListItemType | ReportListItemType, goToItem: () => void) { + const lastPolicyPaymentMethod = item.policyID ? (lastPaymentMethod?.[item.policyID] as ValueOf) : null; + + if (!lastPolicyPaymentMethod) { + return goToItem(); + } + + const amount = isReportListItemType(item) ? item.total ?? 0 : item.formattedTotal; + const transactionID = isTransactionListItemType(item) ? item.transactionID : undefined; + + if (lastPolicyPaymentMethod === CONST.IOU.PAYMENT_TYPE.ELSEWHERE) { + return payMoneyRequestOnSearch(hash, [{reportID: item.reportID, amount, paymentType: lastPolicyPaymentMethod}], transactionID); + } + + const hasVBBA = !!allSnapshots?.[`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`]?.data?.[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`]?.achAccount?.bankAccountID; + return hasVBBA ? payMoneyRequestOnSearch(hash, [{reportID: item.reportID, amount, paymentType: lastPolicyPaymentMethod}], transactionID) : goToItem(); +} + function getOnyxLoadingData(hash: number): {optimisticData: OnyxUpdate[]; finallyData: OnyxUpdate[]} { const optimisticData: OnyxUpdate[] = [ { diff --git a/src/types/onyx/SearchResults.ts b/src/types/onyx/SearchResults.ts index 1d7abe7d63cf..bd5502e76a5f 100644 --- a/src/types/onyx/SearchResults.ts +++ b/src/types/onyx/SearchResults.ts @@ -6,6 +6,7 @@ import type TransactionListItem from '@components/SelectionList/Search/Transacti import type {ReportActionListItemType, ReportListItemType, TransactionListItemType} from '@components/SelectionList/types'; import type CONST from '@src/CONST'; import type ONYXKEYS from '@src/ONYXKEYS'; +import type {ACHAccount} from './Policy'; import type {InvoiceReceiver} from './Report'; import type ReportActionName from './ReportActionName'; import type ReportNameValuePairs from './ReportNameValuePairs'; @@ -209,7 +210,7 @@ type SearchPolicy = { autoReimbursementLimit?: number; /** The verified bank account linked to the policy */ - achAccount?: Record; + achAccount?: ACHAccount; /** The current user's role in the policy */ role: ValueOf;