diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 9f4628c472c8..2f795604d811 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -2467,16 +2467,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 c07865a07db4..8ec3c1396139 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -13853,15 +13853,18 @@ 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; 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 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 3067e1b00fdd..d8b80aa100c8 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', @@ -1553,6 +1553,141 @@ describe('TransactionUtils', () => { }); }); + 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'); + }); + }); + describe('getConvertedAmount', () => { it('should return the absolute amount if transaction is not from expense report, tracked expense and allowNegative is false', () => { const transaction = generateTransaction({