From 25e359cf45fa3714526ab7d007593f67e9f13dbf Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Mon, 13 Apr 2026 15:59:46 +0700 Subject: [PATCH 1/4] Optimistically update original report totals when moving held expenses to a new hold report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a user partially approves or pays an expense report offline, the held transactions are reassigned to a new hold report inside getReportFromHoldRequestsOnyxData, but the original report's total and nonReimbursableTotal fields were never updated. As a result, after the optimistic merge the original report still carried its pre-move total while hasHeldExpenses returned false, causing getTotalAmountForIOUReportPreviewButton to fall through to reimbursableSpend derived from the stale total and display the wrong Pay amount. This change updates the original iouReport's total and nonReimbursableTotal to the previously-stored unheld amounts whenever the utility actually moved any held transactions, and adds a matching failure rollback. Because both the approve and pay partial flows share this utility, both inherit the fix. unheldTotal is intentionally left unchanged — after the move, every remaining transaction is unheld, so the existing unheldTotal value already equals the new total. --- src/libs/actions/IOU/index.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 6748e1a940e2..9efe4609be89 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -4686,6 +4686,14 @@ function getReportFromHoldRequestsOnyxData({ const isApprovalEnabled = policy ? policy.approvalMode && policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL : false; + // After the held transactions are reassigned to the new hold report, the original iouReport + // only contains the previously-unheld transactions. Its total/nonReimbursableTotal must be + // updated optimistically so that offline consumers (e.g. the Pay button label computed from + // report.total via getMoneyRequestSpendBreakdown) reflect the new remaining amount instead + // of the stale pre-move total. unheldTotal is intentionally left unchanged because it already + // equals the new total once all remaining transactions on this report are unheld. + const shouldUpdateOriginalReportTotals = holdTransactions.length > 0 && iouReport?.unheldTotal !== undefined; + const optimisticData: Array> = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -4745,6 +4753,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; @@ -4815,6 +4834,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, From e0976db9f02a18e11833c7501144d62b4c0b5619 Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Mon, 4 May 2026 18:26:55 +0700 Subject: [PATCH 2/4] Add unit tests for getReportFromHoldRequestsOnyxData totals update --- tests/actions/IOUTest/HoldTest.ts | 167 +++++++++++++++++++++++++++++- 1 file changed, 166 insertions(+), 1 deletion(-) diff --git a/tests/actions/IOUTest/HoldTest.ts b/tests/actions/IOUTest/HoldTest.ts index 409968aa40ca..89ef22b3d021 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 onyxData: Record = { + [`${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`]: iouReport, + [`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`]: chatReport, + }; + if (heldTransaction) { + onyxData[`${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`] = heldTransaction; + } + if (heldIouAction) { + onyxData[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`] = { + [heldIouAction.reportActionID]: heldIouAction, + }; + } + + return {chatReport, iouReport, onyxData}; + }; + + test('should optimistically update original report total to unheldTotal when held transactions are moved out', () => { + const {chatReport, iouReport, onyxData} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: 200, + unheldNonReimbursableTotal: 30, + heldAmount: 100, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet(onyxData)) + .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, onyxData} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: 200, + unheldNonReimbursableTotal: 30, + heldAmount: 100, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet(onyxData)) + .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, onyxData} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: 300, + unheldNonReimbursableTotal: 50, + heldAmount: 0, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet(onyxData)) + .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, onyxData} = buildScenario({ + total: 300, + nonReimbursableTotal: 50, + unheldTotal: undefined, + unheldNonReimbursableTotal: undefined, + heldAmount: 100, + }); + + return waitForBatchedUpdates() + .then(() => Onyx.multiSet(onyxData)) + .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([]); + }); + }); + }); }); From 524933d226b46ceac6cef347dee7cb7eae46715b Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Mon, 4 May 2026 18:33:52 +0700 Subject: [PATCH 3/4] Fix tsc strict-typing error in new Hold tests --- tests/actions/IOUTest/HoldTest.ts | 38 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/actions/IOUTest/HoldTest.ts b/tests/actions/IOUTest/HoldTest.ts index 89ef22b3d021..b7cb4d041e98 100644 --- a/tests/actions/IOUTest/HoldTest.ts +++ b/tests/actions/IOUTest/HoldTest.ts @@ -604,24 +604,24 @@ describe('actions/IOU/Hold', () => { }) : undefined; - const onyxData: Record = { + const reportCollection: ReportCollectionDataSet = { [`${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`]: iouReport, [`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`]: chatReport, }; - if (heldTransaction) { - onyxData[`${ONYXKEYS.COLLECTION.TRANSACTION}${heldTransaction.transactionID}`] = heldTransaction; - } - if (heldIouAction) { - onyxData[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`] = { - [heldIouAction.reportActionID]: heldIouAction, - }; - } - - return {chatReport, iouReport, onyxData}; + 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, onyxData} = buildScenario({ + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ total: 300, nonReimbursableTotal: 50, unheldTotal: 200, @@ -630,7 +630,7 @@ describe('actions/IOU/Hold', () => { }); return waitForBatchedUpdates() - .then(() => Onyx.multiSet(onyxData)) + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) .then(() => { const result = getReportFromHoldRequestsOnyxData({ chatReport, @@ -646,7 +646,7 @@ describe('actions/IOU/Hold', () => { }); test('should include matching failureData entry to restore original totals on rollback', () => { - const {chatReport, iouReport, onyxData} = buildScenario({ + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ total: 300, nonReimbursableTotal: 50, unheldTotal: 200, @@ -655,7 +655,7 @@ describe('actions/IOU/Hold', () => { }); return waitForBatchedUpdates() - .then(() => Onyx.multiSet(onyxData)) + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) .then(() => { const result = getReportFromHoldRequestsOnyxData({ chatReport, @@ -676,7 +676,7 @@ describe('actions/IOU/Hold', () => { }); test('should not push a totals update when no held transactions exist', () => { - const {chatReport, iouReport, onyxData} = buildScenario({ + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ total: 300, nonReimbursableTotal: 50, unheldTotal: 300, @@ -685,7 +685,7 @@ describe('actions/IOU/Hold', () => { }); return waitForBatchedUpdates() - .then(() => Onyx.multiSet(onyxData)) + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) .then(() => { const result = getReportFromHoldRequestsOnyxData({ chatReport, @@ -703,7 +703,7 @@ describe('actions/IOU/Hold', () => { }); test('should not push a totals update when iouReport.unheldTotal is undefined', () => { - const {chatReport, iouReport, onyxData} = buildScenario({ + const {chatReport, iouReport, reportCollection, transactionCollection, actionCollection} = buildScenario({ total: 300, nonReimbursableTotal: 50, unheldTotal: undefined, @@ -712,7 +712,7 @@ describe('actions/IOU/Hold', () => { }); return waitForBatchedUpdates() - .then(() => Onyx.multiSet(onyxData)) + .then(() => Onyx.multiSet({...reportCollection, ...transactionCollection, ...actionCollection})) .then(() => { const result = getReportFromHoldRequestsOnyxData({ chatReport, From 78ff4d0c8b771f07783cad3dee8776752b2f391b Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Thu, 7 May 2026 08:32:33 +0700 Subject: [PATCH 4/4] Shorten getReportFromHoldRequestsOnyxData totals-update comment per review --- src/libs/actions/IOU/Hold.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/IOU/Hold.ts b/src/libs/actions/IOU/Hold.ts index 18332c655978..9293f30b23c4 100644 --- a/src/libs/actions/IOU/Hold.ts +++ b/src/libs/actions/IOU/Hold.ts @@ -816,12 +816,9 @@ function getReportFromHoldRequestsOnyxData({ const isApprovalEnabled = policy ? policy.approvalMode && policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL : false; - // After the held transactions are reassigned to the new hold report, the original iouReport - // only contains the previously-unheld transactions. Its total/nonReimbursableTotal must be - // updated optimistically so that offline consumers (e.g. the Pay button label computed from - // report.total via getMoneyRequestSpendBreakdown) reflect the new remaining amount instead - // of the stale pre-move total. unheldTotal is intentionally left unchanged because it already - // equals the new total once all remaining transactions on this report are unheld. + // 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> = [