Skip to content

Commit 25e359c

Browse files
committed
Optimistically update original report totals when moving held expenses to a new hold report
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.
1 parent 897ef5c commit 25e359c

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/libs/actions/IOU/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4686,6 +4686,14 @@ function getReportFromHoldRequestsOnyxData({
46864686

46874687
const isApprovalEnabled = policy ? policy.approvalMode && policy.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL : false;
46884688

4689+
// After the held transactions are reassigned to the new hold report, the original iouReport
4690+
// only contains the previously-unheld transactions. Its total/nonReimbursableTotal must be
4691+
// updated optimistically so that offline consumers (e.g. the Pay button label computed from
4692+
// report.total via getMoneyRequestSpendBreakdown) reflect the new remaining amount instead
4693+
// of the stale pre-move total. unheldTotal is intentionally left unchanged because it already
4694+
// equals the new total once all remaining transactions on this report are unheld.
4695+
const shouldUpdateOriginalReportTotals = holdTransactions.length > 0 && iouReport?.unheldTotal !== undefined;
4696+
46894697
const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS | typeof ONYXKEYS.COLLECTION.TRANSACTION>> = [
46904698
{
46914699
onyxMethod: Onyx.METHOD.MERGE,
@@ -4745,6 +4753,17 @@ function getReportFromHoldRequestsOnyxData({
47454753
},
47464754
];
47474755

4756+
if (shouldUpdateOriginalReportTotals) {
4757+
optimisticData.push({
4758+
onyxMethod: Onyx.METHOD.MERGE,
4759+
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`,
4760+
value: {
4761+
total: iouReport?.unheldTotal ?? 0,
4762+
nonReimbursableTotal: iouReport?.unheldNonReimbursableTotal ?? 0,
4763+
},
4764+
});
4765+
}
4766+
47484767
const bringReportActionsBack: Record<string, OnyxTypes.ReportAction> = {};
47494768
for (const reportAction of holdReportActions) {
47504769
bringReportActionsBack[reportAction.reportActionID] = reportAction;
@@ -4815,6 +4834,17 @@ function getReportFromHoldRequestsOnyxData({
48154834
},
48164835
];
48174836

4837+
if (shouldUpdateOriginalReportTotals) {
4838+
failureData.push({
4839+
onyxMethod: Onyx.METHOD.MERGE,
4840+
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`,
4841+
value: {
4842+
total: iouReport?.total,
4843+
nonReimbursableTotal: iouReport?.nonReimbursableTotal,
4844+
},
4845+
});
4846+
}
4847+
48184848
// Copy submission/approval actions to the new report
48194849
const [copiedActionsOptimistic, copiedActionsSuccess, copiedActionsFailure, optimisticReportActionCopyIDs] = getDuplicateActionsForPartialReport(
48204850
iouReport?.reportID,

0 commit comments

Comments
 (0)