diff --git a/src/components/MoneyReportHeader.tsx b/src/components/MoneyReportHeader.tsx index b8a3f185189e..454e8eae2270 100644 --- a/src/components/MoneyReportHeader.tsx +++ b/src/components/MoneyReportHeader.tsx @@ -16,6 +16,7 @@ import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport'; import usePaymentAnimations from '@hooks/usePaymentAnimations'; import usePaymentOptions from '@hooks/usePaymentOptions'; import usePermissions from '@hooks/usePermissions'; +import usePolicy from '@hooks/usePolicy'; import useReportIsArchived from '@hooks/useReportIsArchived'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useSelectedTransactionsActions from '@hooks/useSelectedTransactionsActions'; @@ -196,6 +197,7 @@ function MoneyReportHeader({ const isDownloadingPDF = download?.isDownloading ?? false; const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false}); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); + const activePolicy = usePolicy(activePolicyID); const [integrationsExportTemplates] = useOnyx(ONYXKEYS.NVP_INTEGRATION_SERVER_EXPORT_TEMPLATES, {canBeMissing: true}); const [csvExportLayouts] = useOnyx(ONYXKEYS.NVP_CSV_EXPORT_LAYOUTS, {canBeMissing: true}); const [lastDistanceExpenseType] = useOnyx(ONYXKEYS.NVP_LAST_DISTANCE_EXPENSE_TYPE, {canBeMissing: true}); @@ -414,10 +416,10 @@ function MoneyReportHeader({ } } else if (isInvoiceReport) { startAnimation(); - payInvoice(type, chatReport, moneyRequestReport, introSelected, payAsBusiness, existingB2BInvoiceReport, methodID, paymentMethod); + payInvoice(type, chatReport, moneyRequestReport, introSelected, payAsBusiness, existingB2BInvoiceReport, methodID, paymentMethod, activePolicy); } else { startAnimation(); - payMoneyRequest(type, chatReport, moneyRequestReport, introSelected, undefined, true); + payMoneyRequest(type, chatReport, moneyRequestReport, introSelected, undefined, true, activePolicy); if (currentSearchQueryJSON) { search({ searchKey: currentSearchKey, @@ -439,6 +441,7 @@ function MoneyReportHeader({ moneyRequestReport, introSelected, existingB2BInvoiceReport, + activePolicy, currentSearchQueryJSON, currentSearchKey, isOffline, diff --git a/src/components/ProcessMoneyReportHoldMenu.tsx b/src/components/ProcessMoneyReportHoldMenu.tsx index a8f13c90c733..a458abb6b3ee 100644 --- a/src/components/ProcessMoneyReportHoldMenu.tsx +++ b/src/components/ProcessMoneyReportHoldMenu.tsx @@ -2,6 +2,7 @@ import React, {useMemo} from 'react'; import type {OnyxEntry} from 'react-native-onyx'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; +import usePolicy from '@hooks/usePolicy'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import {approveMoneyRequest, payMoneyRequest} from '@userActions/IOU'; import CONST from '@src/CONST'; @@ -62,6 +63,8 @@ function ProcessMoneyReportHoldMenu({ // We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to apply the correct modal type // eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth const {isSmallScreenWidth} = useResponsiveLayout(); + const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); + const activePolicy = usePolicy(activePolicyID); const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: true}); const onSubmit = (full: boolean) => { @@ -74,7 +77,7 @@ function ProcessMoneyReportHoldMenu({ if (startAnimation) { startAnimation(); } - payMoneyRequest(paymentType, chatReport, moneyRequestReport, introSelected, undefined, full); + payMoneyRequest(paymentType, chatReport, moneyRequestReport, introSelected, undefined, full, activePolicy); } onClose(); }; diff --git a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx index 9434fe158f90..9f913df220bb 100644 --- a/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx +++ b/src/components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent.tsx @@ -27,6 +27,7 @@ import useNetwork from '@hooks/useNetwork'; import useOnyx from '@hooks/useOnyx'; import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport'; import usePaymentAnimations from '@hooks/usePaymentAnimations'; +import usePolicy from '@hooks/usePolicy'; import useReportIsArchived from '@hooks/useReportIsArchived'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useStyleUtils from '@hooks/useStyleUtils'; @@ -117,6 +118,7 @@ function MoneyRequestReportPreviewContent({ }: MoneyRequestReportPreviewContentProps) { const [chatReportMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${chatReportID}`, {canBeMissing: true, allowStaleData: true}); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); + const activePolicy = usePolicy(activePolicyID); const [lastDistanceExpenseType] = useOnyx(ONYXKEYS.NVP_LAST_DISTANCE_EXPENSE_TYPE, {canBeMissing: true}); const shouldShowLoading = !chatReportMetadata?.hasOnceLoadedReportActions && transactions.length === 0 && !chatReportMetadata?.isOptimisticReport; // `hasOnceLoadedReportActions` becomes true before transactions populate fully, @@ -232,13 +234,13 @@ function MoneyRequestReportPreviewContent({ } else if (chatReport && iouReport) { startAnimation(); if (isInvoiceReportUtils(iouReport)) { - payInvoice(type, chatReport, iouReport, introSelected, payAsBusiness, existingB2BInvoiceReport); + payInvoice(type, chatReport, iouReport, introSelected, payAsBusiness, existingB2BInvoiceReport, undefined, undefined, activePolicy); } else { - payMoneyRequest(type, chatReport, iouReport, introSelected); + payMoneyRequest(type, chatReport, iouReport, introSelected, undefined, true, activePolicy); } } }, - [isDelegateAccessRestricted, iouReport, chatReport, showDelegateNoAccessModal, startAnimation, introSelected, existingB2BInvoiceReport], + [isDelegateAccessRestricted, iouReport, chatReport, showDelegateNoAccessModal, startAnimation, introSelected, existingB2BInvoiceReport, activePolicy], ); const confirmApproval = () => { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 4c195aa410bc..905c6d5eb1a6 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -894,12 +894,6 @@ Onyx.connect({ }, }); -let activePolicyID: OnyxEntry; -Onyx.connect({ - key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (value) => (activePolicyID = value), -}); - let personalDetailsList: OnyxEntry; Onyx.connect({ key: ONYXKEYS.PERSONAL_DETAILS_LIST, @@ -9439,23 +9433,35 @@ function getReportFromHoldRequestsOnyxData( }; } -function getPayMoneyRequestParams( - initialChatReport: OnyxTypes.Report, - iouReport: OnyxEntry, - recipient: Participant, - paymentMethodType: PaymentMethodType, - full: boolean, - payAsBusiness?: boolean, - bankAccountID?: number, - paymentPolicyID?: string | undefined, - lastUsedPaymentMethod?: OnyxTypes.LastPaymentMethodType, - existingB2BInvoiceReport?: OnyxEntry, -): PayMoneyRequestData { +function getPayMoneyRequestParams({ + initialChatReport, + iouReport, + recipient, + paymentMethodType, + full, + payAsBusiness, + bankAccountID, + paymentPolicyID, + lastUsedPaymentMethod, + existingB2BInvoiceReport, + activePolicy, +}: { + initialChatReport: OnyxTypes.Report; + iouReport: OnyxEntry; + recipient: Participant; + paymentMethodType: PaymentMethodType; + full: boolean; + payAsBusiness?: boolean; + bankAccountID?: number; + paymentPolicyID?: string | undefined; + lastUsedPaymentMethod?: OnyxTypes.LastPaymentMethodType; + existingB2BInvoiceReport?: OnyxEntry; + activePolicy?: OnyxEntry; +}): PayMoneyRequestData { const isInvoiceReport = isInvoiceReportReportUtils(iouReport); // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850 // eslint-disable-next-line deprecation/deprecation - const activePolicy = getPolicy(activePolicyID); - let payerPolicyID = activePolicyID; + let payerPolicyID = activePolicy?.id; let chatReport = initialChatReport; let policyParams = {}; const optimisticData: OnyxUpdate[] = []; @@ -9491,7 +9497,7 @@ function getPayMoneyRequestParams( optimisticData.push(...policyOptimisticData, {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, value: payerPolicyID}); successData.push(...policySuccessData); - failureData.push(...policyFailureData, {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, value: activePolicyID ?? null}); + failureData.push(...policyFailureData, {onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, value: activePolicy?.id ?? null}); } if (isIndividualInvoiceRoom(chatReport) && payAsBusiness && existingB2BInvoiceReport) { @@ -10870,6 +10876,7 @@ function payMoneyRequest( introSelected: OnyxEntry, paymentPolicyID?: string, full = true, + activePolicy?: OnyxEntry, ) { if (chatReport.policyID && shouldRestrictUserBillableActions(chatReport.policyID)) { Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(chatReport.policyID)); @@ -10880,7 +10887,15 @@ function payMoneyRequest( completePaymentOnboarding(paymentSelected, introSelected); const recipient = {accountID: iouReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID}; - const {params, optimisticData, successData, failureData} = getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentType, full, undefined, undefined, paymentPolicyID); + const {params, optimisticData, successData, failureData} = getPayMoneyRequestParams({ + initialChatReport: chatReport, + iouReport, + recipient, + paymentMethodType: paymentType, + full, + paymentPolicyID, + activePolicy, + }); // For now, we need to call the PayMoneyRequestWithWallet API since PayMoneyRequest was not updated to work with // Expensify Wallets. @@ -10900,6 +10915,7 @@ function payInvoice( existingB2BInvoiceReport?: OnyxEntry, methodID?: number, paymentMethod?: PaymentMethod, + activePolicy?: OnyxTypes.Policy, ) { const recipient = {accountID: invoiceReport?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID}; const { @@ -10918,7 +10934,17 @@ function payInvoice( ownerEmail, policyName, }, - } = getPayMoneyRequestParams(chatReport, invoiceReport, recipient, paymentMethodType, true, payAsBusiness, methodID, undefined, undefined, existingB2BInvoiceReport); + } = getPayMoneyRequestParams({ + initialChatReport: chatReport, + iouReport: invoiceReport, + recipient, + paymentMethodType, + full: true, + payAsBusiness, + bankAccountID: methodID, + existingB2BInvoiceReport, + activePolicy, + }); const paymentSelected = paymentMethodType === CONST.IOU.PAYMENT_TYPE.VBBA ? CONST.IOU.PAYMENT_SELECTED.BBA : CONST.IOU.PAYMENT_SELECTED.PBA; completePaymentOnboarding(paymentSelected, introSelected);