From 0985122eae3ce2bfdde9d0f3596252e3f035840c Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Thu, 19 Mar 2026 15:14:26 +0100 Subject: [PATCH] lazy B2B invoice report lookup in getPayMoneyRequestParams --- src/components/MoneyReportHeader.tsx | 4 --- .../MoneyRequestReportPreviewContent.tsx | 5 --- src/libs/actions/IOU/index.ts | 33 +++++++++++++++---- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/components/MoneyReportHeader.tsx b/src/components/MoneyReportHeader.tsx index 7905bbcab4d0b..78ee5cb27970f 100644 --- a/src/components/MoneyReportHeader.tsx +++ b/src/components/MoneyReportHeader.tsx @@ -23,7 +23,6 @@ import useLocalize from '@hooks/useLocalize'; import useMobileSelectionMode from '@hooks/useMobileSelectionMode'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; -import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport'; import usePaymentAnimations from '@hooks/usePaymentAnimations'; import usePaymentOptions from '@hooks/usePaymentOptions'; import usePermissions from '@hooks/usePermissions'; @@ -673,7 +672,6 @@ function MoneyReportHeader({ const isReportSubmitter = isCurrentUserSubmitter(chatIOUReport); const isChatReportDM = isDM(chatReport); - const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID); const isSelectionModePaymentRef = useRef(false); const confirmPayment = useCallback( ({paymentType: type, payAsBusiness, methodID, paymentMethod}: PaymentActionParams) => { @@ -706,7 +704,6 @@ function MoneyReportHeader({ currentUserAccountIDParam: accountID, currentUserEmailParam: email ?? '', payAsBusiness, - existingB2BInvoiceReport, methodID, paymentMethod, activePolicy, @@ -768,7 +765,6 @@ function MoneyReportHeader({ introSelected, accountID, email, - existingB2BInvoiceReport, activePolicy, policy, currentSearchQueryJSON, diff --git a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx index 10d28496053fe..8affdae0e89d8 100644 --- a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx +++ b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx @@ -33,7 +33,6 @@ import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; -import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport'; import usePaymentAnimations from '@hooks/usePaymentAnimations'; import usePermissions from '@hooks/usePermissions'; import usePolicy from '@hooks/usePolicy'; @@ -254,8 +253,6 @@ function MoneyRequestReportPreviewContent({ const hasReceipts = transactionsWithReceipts.length > 0; const isScanning = hasReceipts && areAllRequestsBeingSmartScanned; - const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID); - const {isDelegateAccessRestricted} = useDelegateNoAccessState(); const {showDelegateNoAccessModal} = useDelegateNoAccessActions(); const [reportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`); @@ -290,7 +287,6 @@ function MoneyRequestReportPreviewContent({ currentUserAccountIDParam: currentUserAccountID, currentUserEmailParam: currentUserEmail, payAsBusiness, - existingB2BInvoiceReport, methodID, paymentMethod, activePolicy, @@ -326,7 +322,6 @@ function MoneyRequestReportPreviewContent({ introSelected, currentUserAccountID, currentUserEmail, - existingB2BInvoiceReport, activePolicy, policy, betas, diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 3841fd6e1bca7..52b42d838c8ef 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -152,6 +152,7 @@ import { hasNonReimbursableTransactions as hasNonReimbursableTransactionsReportUtils, hasOutstandingChildRequest, hasViolations as hasViolationsReportUtils, + isArchivedNonExpenseReport, isArchivedReport, isClosedReport as isClosedReportUtil, isDeprecatedGroupDM, @@ -326,7 +327,6 @@ type PayInvoiceArgs = { currentUserAccountIDParam: number; currentUserEmailParam: string; payAsBusiness?: boolean; - existingB2BInvoiceReport?: OnyxEntry; methodID?: number; paymentMethod?: PaymentMethod; activePolicy?: OnyxTypes.Policy; @@ -1026,6 +1026,25 @@ function getAllReports(): OnyxCollection { return allReports; } +function getExistingB2BInvoiceReport(receiverID: string | number | undefined, policyID?: string): OnyxEntry { + return Object.values(allReports ?? {}).find((report) => { + if (!report || !isInvoiceRoom(report)) { + return false; + } + const rnvp = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]; + if (isArchivedNonExpenseReport(report, isArchivedReport(rnvp))) { + return false; + } + const isSameReceiver = + report.invoiceReceiver && + report.invoiceReceiver.type === CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS && + 'policyID' in report.invoiceReceiver && + report.invoiceReceiver.policyID === receiverID; + + return report.policyID === policyID && isSameReceiver; + }); +} + function getAllReportActionsFromIOU(): OnyxCollection { return allReportActions; } @@ -9301,7 +9320,6 @@ function getPayMoneyRequestParams({ introSelected, paymentPolicyID, lastUsedPaymentMethod, - existingB2BInvoiceReport, activePolicy, iouReportCurrentNextStepDeprecated, betas, @@ -9317,7 +9335,6 @@ function getPayMoneyRequestParams({ bankAccountID?: number; paymentPolicyID?: string | undefined; lastUsedPaymentMethod?: OnyxTypes.LastPaymentMethodType; - existingB2BInvoiceReport?: OnyxEntry; activePolicy?: OnyxEntry; currentUserAccountIDParam: number; currentUserEmailParam?: string; @@ -9384,8 +9401,12 @@ function getPayMoneyRequestParams({ onyxData.failureData?.push(...(policyFailureData ?? []), {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, value: activePolicy?.id ?? null}); } - if (isIndividualInvoiceRoom(chatReport) && payAsBusiness && existingB2BInvoiceReport) { - chatReport = existingB2BInvoiceReport; + if (isIndividualInvoiceRoom(chatReport) && payAsBusiness) { + const existingB2B = getExistingB2BInvoiceReport(activePolicy?.id, chatReport.policyID); + + if (existingB2B) { + chatReport = existingB2B; + } } let total = (iouReport?.total ?? 0) - (iouReport?.nonReimbursableTotal ?? 0); @@ -11353,7 +11374,6 @@ function payInvoice({ currentUserAccountIDParam, currentUserEmailParam, payAsBusiness = false, - existingB2BInvoiceReport, methodID, paymentMethod, activePolicy, @@ -11385,7 +11405,6 @@ function payInvoice({ full: true, payAsBusiness, bankAccountID: methodID, - existingB2BInvoiceReport, activePolicy, currentUserAccountIDParam, currentUserEmailParam,