From a529d102100b8824153cbcedaad461cfa704f4be Mon Sep 17 00:00:00 2001 From: MobileMage Date: Sat, 10 Jan 2026 07:40:55 +0100 Subject: [PATCH 1/2] fix: preserve split child transactions when report is deleted When a report containing a split expense child was deleted, the getChildTransactions function excluded the child transaction because it required the report to exist. This caused incorrect isReverseSplitOperation determination, which reverted the split and cleared split metadata. The fix adds an optional includeOrphaned parameter to getChildTransactions: - Default (false): excludes orphaned transactions (reportID '0') for processing - When true: includes all children for accurate counting In updateSplitTransactions, we compare counts to detect orphaned children and prevent incorrect split reversal. Fixed Issues: #78933 --- src/libs/TransactionUtils/index.ts | 19 ++-- src/libs/actions/IOU/index.ts | 5 +- tests/unit/TransactionUtilsTest.ts | 141 ++++++++++++++++++++++++++++- 3 files changed, 154 insertions(+), 11 deletions(-) diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 174ac9f46516..c8a788efb911 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -2416,16 +2416,21 @@ function isTransactionPendingDelete(transaction: OnyxEntry): boolea } /** - * Retrieves all “child” transactions associated with a given original transaction + * Retrieves all "child" transactions associated with a given original transaction. + * By default excludes orphaned transactions (reportID '0'). Use includeOrphaned=true for counting. */ -function getChildTransactions(transactions: OnyxCollection, reports: OnyxCollection, originalTransactionID: string | undefined) { +function getChildTransactions(transactions: OnyxCollection, reports: OnyxCollection, originalTransactionID: string | undefined, includeOrphaned = false) { return Object.values(transactions ?? {}).filter((currentTransaction) => { + const isSplitChild = currentTransaction?.comment?.originalTransactionID === originalTransactionID; + if (!isSplitChild || currentTransaction?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) { + return false; + } + const isOrphaned = currentTransaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID; + if (isOrphaned) { + return includeOrphaned; + } const currentReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${currentTransaction?.reportID}`]; - return ( - currentTransaction?.comment?.originalTransactionID === originalTransactionID && - !!currentReport && - currentTransaction?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE - ); + return !!currentReport || currentTransaction?.comment?.source === CONST.IOU.TYPE.SPLIT; }); } diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index d0851fc965a6..69b41d18cf3a 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -13960,7 +13960,10 @@ function updateSplitTransactions({ const isCreationOfSplits = originalChildTransactions.length === 0; const hasEditableSplitExpensesLeft = splitExpenses.some((expense) => (expense.statusNum ?? 0) < CONST.REPORT.STATUS_NUM.SUBMITTED); - const isReverseSplitOperation = splitExpenses.length === 1 && originalChildTransactions.length > 0 && hasEditableSplitExpensesLeft; + // Don't revert split if there are orphaned children (reportID '0') - they're still part of the split + const allChildTransactions = getChildTransactions(allTransactionsList, allReportsList, originalTransactionID, true); + const isReverseSplitOperation = + splitExpenses.length === 1 && originalChildTransactions.length > 0 && hasEditableSplitExpensesLeft && allChildTransactions.length === originalChildTransactions.length; let changesInReportTotal = 0; // Validate custom unit rate before proceeding with split diff --git a/tests/unit/TransactionUtilsTest.ts b/tests/unit/TransactionUtilsTest.ts index 845fc55798e1..ac2556d0abb4 100644 --- a/tests/unit/TransactionUtilsTest.ts +++ b/tests/unit/TransactionUtilsTest.ts @@ -1,4 +1,5 @@ import Onyx from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; import DateUtils from '@libs/DateUtils'; import {shouldShowBrokenConnectionViolation, shouldShowBrokenConnectionViolationForMultipleTransactions} from '@libs/TransactionUtils'; import CONST from '@src/CONST'; @@ -6,7 +7,6 @@ import IntlStore from '@src/languages/IntlStore'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Attendee} from '@src/types/onyx/IOU'; import type {CustomUnit, Rate} from '@src/types/onyx/Policy'; -import type {ReportCollectionDataSet} from '@src/types/onyx/Report'; import type {TransactionCustomUnit} from '@src/types/onyx/Transaction'; import * as TransactionUtils from '../../src/libs/TransactionUtils'; import type {Policy, Report, Transaction} from '../../src/types/onyx'; @@ -70,12 +70,12 @@ const secondUserOpenReport = { stateNum: CONST.REPORT.STATE_NUM.OPEN, statusNum: CONST.REPORT.STATUS_NUM.OPEN, }; -const reportCollectionDataSet: ReportCollectionDataSet = { +const reportCollectionDataSet = { [`${ONYXKEYS.COLLECTION.REPORT}${FAKE_OPEN_REPORT_ID}`]: openReport, [`${ONYXKEYS.COLLECTION.REPORT}${FAKE_PROCESSING_REPORT_ID}`]: processingReport, [`${ONYXKEYS.COLLECTION.REPORT}${FAKE_APPROVED_REPORT_ID}`]: approvedReport, [`${ONYXKEYS.COLLECTION.REPORT}${FAKE_OPEN_REPORT_SECOND_USER_ID}`]: secondUserOpenReport, -}; +} as OnyxCollection; const defaultDistanceRatePolicyID1: Record = { customUnitRateID1: { currency: 'USD', @@ -1552,4 +1552,139 @@ describe('TransactionUtils', () => { expect(TransactionUtils.shouldShowExpenseBreakdown(transactions)).toBe(true); }); }); + + describe('getChildTransactions', () => { + const originalTransactionID = 'original-123'; + + it('should return child transactions that have a valid report', () => { + const childTransaction = generateTransaction({ + transactionID: 'child-1', + reportID: FAKE_OPEN_REPORT_ID, + comment: { + originalTransactionID, + source: CONST.IOU.TYPE.SPLIT, + }, + }); + + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}child-1`]: childTransaction, + }; + + const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID); + + expect(result).toHaveLength(1); + expect(result.at(0)?.transactionID).toBe('child-1'); + }); + + it('should return split child transactions even if their report was deleted', () => { + const childTransaction = generateTransaction({ + transactionID: 'child-2', + reportID: 'deleted-report-id', + comment: { + originalTransactionID, + source: CONST.IOU.TYPE.SPLIT, + }, + }); + + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}child-2`]: childTransaction, + }; + + // Report doesn't exist in reportCollectionDataSet + const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID); + + expect(result).toHaveLength(1); + expect(result.at(0)?.transactionID).toBe('child-2'); + }); + + it('should exclude orphaned transactions with reportID "0" from processing', () => { + const orphanedTransaction = generateTransaction({ + transactionID: 'orphaned-1', + reportID: '0', + comment: { + originalTransactionID, + source: CONST.IOU.TYPE.SPLIT, + }, + }); + + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}orphaned-1`]: orphanedTransaction, + }; + + // Orphaned split children should be excluded from getChildTransactions for processing + const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID); + + expect(result).toHaveLength(0); + }); + + it('should exclude transactions with pendingAction DELETE', () => { + const deletingTransaction = generateTransaction({ + transactionID: 'deleting-1', + reportID: FAKE_OPEN_REPORT_ID, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + comment: { + originalTransactionID, + source: CONST.IOU.TYPE.SPLIT, + }, + }); + + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}deleting-1`]: deletingTransaction, + }; + + const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID); + + expect(result).toHaveLength(0); + }); + + it('should only return transactions matching the originalTransactionID', () => { + const matchingChild = generateTransaction({ + transactionID: 'matching-1', + reportID: FAKE_OPEN_REPORT_ID, + comment: { + originalTransactionID, + source: CONST.IOU.TYPE.SPLIT, + }, + }); + + const nonMatchingChild = generateTransaction({ + transactionID: 'non-matching-1', + reportID: FAKE_OPEN_REPORT_ID, + comment: { + originalTransactionID: 'different-original-id', + source: CONST.IOU.TYPE.SPLIT, + }, + }); + + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}matching-1`]: matchingChild, + [`${ONYXKEYS.COLLECTION.TRANSACTION}non-matching-1`]: nonMatchingChild, + }; + + const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID); + + expect(result).toHaveLength(1); + expect(result.at(0)?.transactionID).toBe('matching-1'); + }); + + it('should include orphaned transactions when includeOrphaned=true', () => { + const orphanedTransaction = generateTransaction({ + transactionID: 'orphaned-1', + reportID: '0', + comment: { + originalTransactionID, + source: CONST.IOU.TYPE.SPLIT, + }, + }); + + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}orphaned-1`]: orphanedTransaction, + }; + + const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID, true); + + expect(result).toHaveLength(1); + expect(result.at(0)?.transactionID).toBe('orphaned-1'); + }); + }); }); From 27bf9f163107f860ab271de29cfae19eec3dd4e6 Mon Sep 17 00:00:00 2001 From: MobileMage Date: Sun, 11 Jan 2026 08:29:24 +0100 Subject: [PATCH 2/2] perf: iterate over transactions once instead of twice --- src/libs/actions/IOU/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 69b41d18cf3a..bde9c7b47c82 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -13952,8 +13952,9 @@ function updateSplitTransactions({ const participants = getMoneyRequestParticipantsFromReport(expenseReport, currentUserPersonalDetails.accountID); const splitExpenses = transactionData?.splitExpenses ?? []; - // List of all child transactions that have been created after split - const originalChildTransactions = getChildTransactions(allTransactionsList, allReportsList, originalTransactionID); + // Get all children once (including orphaned), then filter for non-orphaned + const allChildTransactions = getChildTransactions(allTransactionsList, allReportsList, originalTransactionID, true); + const originalChildTransactions = allChildTransactions.filter((tx) => tx?.reportID !== CONST.REPORT.UNREPORTED_REPORT_ID); const processedChildTransactionIDs: string[] = []; const splitExpensesTotal = transactionData?.splitExpensesTotal ?? 0; @@ -13961,7 +13962,6 @@ function updateSplitTransactions({ const isCreationOfSplits = originalChildTransactions.length === 0; const hasEditableSplitExpensesLeft = splitExpenses.some((expense) => (expense.statusNum ?? 0) < CONST.REPORT.STATUS_NUM.SUBMITTED); // Don't revert split if there are orphaned children (reportID '0') - they're still part of the split - const allChildTransactions = getChildTransactions(allTransactionsList, allReportsList, originalTransactionID, true); const isReverseSplitOperation = splitExpenses.length === 1 && originalChildTransactions.length > 0 && hasEditableSplitExpensesLeft && allChildTransactions.length === originalChildTransactions.length;