Skip to content

Commit a5d9074

Browse files
Patch Your spend "Awaiting approval" total when splitting a submitted expense offline
Splitting a SUBMITTED expense changed the report's reimbursable total but never patched the cached "Awaiting approval" snapshot, so the Home total stayed stale offline. Add getYourSpendSnapshotSplitUpdates and wire it into updateSplitTransactions using the existing report-total delta.
1 parent 52e56dd commit a5d9074

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/libs/actions/IOU/SplitTransactionUpdate.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {addPendingNewTransactionIDs} from './PendingNewTransactions';
6666
import {getDeleteTrackExpenseInformation} from './TrackExpense';
6767
import {getUpdateMoneyRequestParams} from './UpdateMoneyRequest';
6868
import type {UpdateMoneyRequestDataKeys} from './UpdateMoneyRequest';
69+
import {getYourSpendSnapshotSplitUpdates} from './YourSpendSnapshotUpdate';
6970

7071
type UpdateSplitTransactionsParams = {
7172
allTransactionsList: OnyxCollection<OnyxTypes.Transaction>;
@@ -1787,6 +1788,20 @@ function updateSplitTransactions({
17871788
}
17881789
}
17891790

1791+
// A split keeps a SUBMITTED report in the "Awaiting approval" section but changes its reimbursable total
1792+
// (e.g. split to a lower amount). Home reads that total from the cached snapshot, which is only refreshed
1793+
// online, so patch the same-currency delta optimistically. `changesInReportTotal` is the signed report-total
1794+
// delta (new split total minus the pre-split total) in the transaction currency.
1795+
const yourSpendSplitUpdates = getYourSpendSnapshotSplitUpdates({
1796+
iouReport: expenseReport,
1797+
originalTransaction,
1798+
reimbursableDiff: changesInReportTotal,
1799+
currentUserAccountID: currentUserPersonalDetails.accountID,
1800+
});
1801+
onyxData.optimisticData?.push(...yourSpendSplitUpdates.optimisticData);
1802+
onyxData.successData?.push(...yourSpendSplitUpdates.successData);
1803+
onyxData.failureData?.push(...yourSpendSplitUpdates.failureData);
1804+
17901805
if (isReverseSplitOperation) {
17911806
const parameters = {
17921807
...splits.at(0),

src/libs/actions/IOU/YourSpendSnapshotUpdate.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,43 @@ function getYourSpendSnapshotTotalUpdates({transaction, updatedTransaction, iouR
411411
return result;
412412
}
413413

414-
export {getYourSpendSnapshotReportMoveUpdates, getYourSpendSnapshotTotalUpdates, getYourSpendSnapshotTransactionRemovalUpdates, transactionMatchesAwaitingApprovalQuery};
414+
type GetYourSpendSnapshotSplitUpdatesParams = {
415+
iouReport: OnyxEntry<Report>;
416+
originalTransaction: OnyxEntry<Transaction>;
417+
// Signed change to the report's reimbursable total, in the transaction/report currency
418+
// (new split total minus the pre-split total). Negative when the expense is split to a lower amount.
419+
reimbursableDiff: number;
420+
currentUserAccountID: number;
421+
};
422+
423+
/**
424+
* Optimistically patches the "Awaiting approval" snapshot total when a SUBMITTED expense is split and the resulting
425+
* reimbursable total changes (e.g. split to a lower amount). The report stays in the awaiting-approval section, so this
426+
* is a same-section amount delta rather than a state move. Home reads totals from `snapshot.search.total`, which is only
427+
* refreshed online. Mirrors the amount-edit path: same-currency only (the currency guard in
428+
* `buildSnapshotTotalUpdatesForHash` skips a mismatch, since FX conversion isn't available offline).
429+
*/
430+
function getYourSpendSnapshotSplitUpdates({iouReport, originalTransaction, reimbursableDiff, currentUserAccountID}: GetYourSpendSnapshotSplitUpdatesParams): YourSpendSnapshotOnyxData {
431+
const result: YourSpendSnapshotOnyxData = {optimisticData: [], successData: [], failureData: []};
432+
if (!iouReport || !originalTransaction || reimbursableDiff === 0) {
433+
return result;
434+
}
435+
436+
const paidGroupPolicyIDs = getPaidGroupPolicyIDs();
437+
if (!transactionMatchesAwaitingApprovalQuery(iouReport, originalTransaction, currentUserAccountID, paidGroupPolicyIDs)) {
438+
return result;
439+
}
440+
441+
const currency = getCurrency(originalTransaction);
442+
const approvalQueryJSON = buildSearchQueryJSON(buildAwaitingApprovalQuery(currentUserAccountID, paidGroupPolicyIDs));
443+
mergeYourSpendSnapshotOnyxData(result, buildSnapshotTotalUpdatesForHash(approvalQueryJSON?.hash, reimbursableDiff, currency));
444+
return result;
445+
}
446+
447+
export {
448+
getYourSpendSnapshotReportMoveUpdates,
449+
getYourSpendSnapshotSplitUpdates,
450+
getYourSpendSnapshotTotalUpdates,
451+
getYourSpendSnapshotTransactionRemovalUpdates,
452+
transactionMatchesAwaitingApprovalQuery,
453+
};

0 commit comments

Comments
 (0)