Skip to content
27 changes: 27 additions & 0 deletions src/libs/actions/IOU/Hold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.TRANSACTION>> = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -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<string, OnyxTypes.ReportAction> = {};
for (const reportAction of holdReportActions) {
bringReportActionsBack[reportAction.reportActionID] = reportAction;
Expand Down Expand Up @@ -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,
Expand Down
167 changes: 166 additions & 1 deletion tests/actions/IOUTest/HoldTest.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<Report> | 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<Report> | 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<Report> | undefined;
return entry.key === `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}` && (value?.total !== undefined || value?.nonReimbursableTotal !== undefined);
});
expect(totalsUpdates).toEqual([]);
});
});
});
});
Loading