diff --git a/src/libs/actions/IOU/Hold.ts b/src/libs/actions/IOU/Hold.ts index 8fcc3248472f..61c3ac69c465 100644 --- a/src/libs/actions/IOU/Hold.ts +++ b/src/libs/actions/IOU/Hold.ts @@ -818,6 +818,11 @@ function getReportFromHoldRequestsOnyxData({ const isApprovalEnabled = policy ? policy.approvalMode && policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL : false; + // Held transactions just moved out, leaving total/nonReimbursableTotal stale on this report — + // offline consumers (e.g. the Pay button) would read the wrong amount until server reconciles. + // unheldTotal stays as-is: every remaining transaction is unheld, so it already equals the new total. + const shouldUpdateOriginalReportTotals = holdTransactions.length > 0 && iouReport?.unheldTotal !== undefined; + const optimisticData: Array> = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -871,6 +876,17 @@ function getReportFromHoldRequestsOnyxData({ }, ]; + if (shouldUpdateOriginalReportTotals) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: { + total: iouReport?.unheldTotal ?? 0, + nonReimbursableTotal: iouReport?.unheldNonReimbursableTotal ?? 0, + }, + }); + } + const bringReportActionsBack: Record = {}; for (const reportAction of holdReportActions) { bringReportActionsBack[reportAction.reportActionID] = reportAction; @@ -936,6 +952,17 @@ function getReportFromHoldRequestsOnyxData({ }, ]; + if (shouldUpdateOriginalReportTotals) { + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, + value: { + total: iouReport?.total, + nonReimbursableTotal: iouReport?.nonReimbursableTotal, + }, + }); + } + // Copy submission/approval actions to the new report const [copiedActionsOptimistic, copiedActionsSuccess, copiedActionsFailure, optimisticReportActionCopyIDs] = getDuplicateActionsForPartialReport( iouReport?.reportID, diff --git a/tests/actions/IOUTest/HoldTest.ts b/tests/actions/IOUTest/HoldTest.ts index 274f0cea99d8..a14a2b3f2cf1 100644 --- a/tests/actions/IOUTest/HoldTest.ts +++ b/tests/actions/IOUTest/HoldTest.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import Onyx from 'react-native-onyx'; import type {OnyxEntry, OnyxInputValue} from 'react-native-onyx'; -import {putOnHold, putTransactionsOnHold, unholdRequest} from '@libs/actions/IOU/Hold'; +import {getReportFromHoldRequestsOnyxData, putOnHold, putTransactionsOnHold, unholdRequest} from '@libs/actions/IOU/Hold'; import initOnyxDerivedValues from '@libs/actions/OnyxDerived'; import {getMicroSecondOnyxErrorWithTranslationKey} from '@libs/ErrorUtils'; import type * as PolicyUtils from '@libs/PolicyUtils'; @@ -564,4 +564,169 @@ describe('actions/IOU/Hold', () => { }); }); }); + + describe('getReportFromHoldRequestsOnyxData', () => { + const buildHeldTransaction = (reportID: string, amount: number) => { + const transaction = buildOptimisticTransaction({ + transactionParams: {amount, currency: 'USD', reportID}, + }); + return { + ...transaction, + comment: {...transaction.comment, hold: 'hold-reason'}, + }; + }; + + const buildScenario = (overrides: {total: number; nonReimbursableTotal: number; unheldTotal?: number; unheldNonReimbursableTotal?: number; heldAmount?: number}) => { + const baseIouReport = buildOptimisticIOUReport(1, 2, overrides.total, '99', 'USD'); + const iouReport: Report = { + ...baseIouReport, + total: overrides.total, + nonReimbursableTotal: overrides.nonReimbursableTotal, + unheldTotal: overrides.unheldTotal, + unheldNonReimbursableTotal: overrides.unheldNonReimbursableTotal, + }; + const chatReport: Report = { + reportID: '99', + iouReportID: iouReport.reportID, + lastVisibleActionCreated: '2026-01-01 00:00:00.000', + } as Report; + + const heldAmount = overrides.heldAmount ?? 0; + const heldTransaction = heldAmount > 0 ? buildHeldTransaction(iouReport.reportID, heldAmount) : undefined; + const heldIouAction = heldTransaction + ? buildOptimisticIOUReportAction({ + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + amount: heldTransaction.amount, + currency: 'USD', + comment: '', + participants: [], + transactionID: heldTransaction.transactionID, + }) + : undefined; + + const reportCollection: ReportCollectionDataSet = { + [`${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`]: iouReport, + [`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`]: chatReport, + }; + const transactionCollection: TransactionCollectionDataSet = heldTransaction ? {[`${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`]: heldTransaction} : {}; + const actionCollection: ReportActionsCollectionDataSet = heldIouAction + ? { + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`]: { + [heldIouAction.reportActionID]: heldIouAction, + }, + } + : {}; + + return {chatReport, iouReport, reportCollection, transactionCollection, actionCollection}; + }; + + test('should optimistically update original report total to unheldTotal when held transactions are moved out', () => { + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: 200, + unheldNonReimbursableTotal: 30, + heldAmount: 100, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) + .then(() => { + const result = getReportFromHoldRequestsOnyxData({ + chatReport, + iouReport, + recipient: {accountID: 1}, + policy: undefined, + betas: [], + }); + const totalsUpdate = result.optimisticData.find((entry) => entry.onyxMethod === Onyx.METHOD.MERGE && entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`); + expect(totalsUpdate).toBeDefined(); + expect(totalsUpdate?.value).toEqual({total: 200, nonReimbursableTotal: 30}); + }); + }); + + test('should include matching failureData entry to restore original totals on rollback', () => { + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: 200, + unheldNonReimbursableTotal: 30, + heldAmount: 100, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) + .then(() => { + const result = getReportFromHoldRequestsOnyxData({ + chatReport, + iouReport, + recipient: {accountID: 1}, + policy: undefined, + betas: [], + }); + const restorationEntries = result.failureData.filter( + (entry) => entry.onyxMethod === Onyx.METHOD.MERGE && entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + ); + const totalsRestore = restorationEntries.find((entry) => { + const value = entry.value as Partial | undefined; + return value?.total !== undefined || value?.nonReimbursableTotal !== undefined; + }); + expect(totalsRestore?.value).toEqual({total: 300, nonReimbursableTotal: 50}); + }); + }); + + test('should not push a totals update when no held transactions exist', () => { + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: 300, + unheldNonReimbursableTotal: 50, + heldAmount: 0, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) + .then(() => { + const result = getReportFromHoldRequestsOnyxData({ + chatReport, + iouReport, + recipient: {accountID: 1}, + policy: undefined, + betas: [], + }); + const totalsUpdates = result.optimisticData.filter((entry) => { + const value = entry.value as Partial | undefined; + return entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}` && (value?.total !== undefined || value?.nonReimbursableTotal !== undefined); + }); + expect(totalsUpdates).toEqual([]); + }); + }); + + test('should not push a totals update when iouReport.unheldTotal is undefined', () => { + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: undefined, + unheldNonReimbursableTotal: undefined, + heldAmount: 100, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) + .then(() => { + const result = getReportFromHoldRequestsOnyxData({ + chatReport, + iouReport, + recipient: {accountID: 1}, + policy: undefined, + betas: [], + }); + const totalsUpdates = result.optimisticData.filter((entry) => { + const value = entry.value as Partial | undefined; + return entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}` && (value?.total !== undefined || value?.nonReimbursableTotal !== undefined); + }); + expect(totalsUpdates).toEqual([]); + }); + }); + }); });