Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/libs/MoneyRequestReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ function getReportIDForTransaction(transactionItem: TransactionListItemType, IOU
* Filters all available transactions and returns the ones that belong to not removed action and not removed parent action.
*/
function getAllNonDeletedTransactions(transactions: OnyxCollection<Transaction>, reportActions: ReportAction[], isOffline = false, includeOrphanedTransactions = false) {
// Keeps the first action per expense, the one getIOUActionForTransactionID would return.
const iouActionByTransactionID = new Map<string, ReportAction>();
for (const reportAction of reportActions) {
const transactionID = isMoneyRequestAction(reportAction) ? getOriginalMessage(reportAction)?.IOUTransactionID : undefined;
if (transactionID !== undefined && !iouActionByTransactionID.has(transactionID)) {

@roryabraham roryabraham Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Do we need this check?

Suggested change
if (transactionID !== undefined && !iouActionByTransactionID.has(transactionID)) {
if (transactionID !== undefined) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, getIOUActionForTransactionID returns the first match, so this keeps the same one. Without it the last action wins, and the new test with a deleted and a live action fails.

iouActionByTransactionID.set(transactionID, reportAction);
}
}

const nonDeletedTransactions = Object.values(transactions ?? {}).filter((transaction): transaction is Transaction => {
if (!transaction) {
return false;
Expand All @@ -132,7 +141,7 @@ function getAllNonDeletedTransactions(transactions: OnyxCollection<Transaction>,
return true;
}

const action = getIOUActionForTransactionID(reportActions, transaction.transactionID);
const action = iouActionByTransactionID.get(transaction.transactionID);
if (!action && includeOrphanedTransactions) {
return true;
}
Expand Down
66 changes: 66 additions & 0 deletions tests/perf-local/MoneyRequestReportUtils.perf-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Measures getAllNonDeletedTransactions as an expense report grows. Opening a report runs it in several hooks,
* so its cost per call is paid many times on every open.
*/
import {getAllNonDeletedTransactions} from '@libs/MoneyRequestReportUtils';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReportAction, Transaction} from '@src/types/onyx';

import type {OnyxCollection} from 'react-native-onyx';

import {measureFunction} from 'reassure';

import createRandomReportAction from '../utils/collections/reportActions';
import createRandomTransaction from '../utils/collections/transaction';

const REPORT_ID = '1';
const EXPENSE_COUNTS = [50, 100, 250, 500, 1000];

/** One expense action and one comment per expense, like a report people have talked about. */
function buildReport(expenseCount: number): {transactions: OnyxCollection<Transaction>; reportActions: ReportAction[]} {
const transactions: OnyxCollection<Transaction> = {};
const reportActions: ReportAction[] = [];
for (let index = 1; index <= expenseCount; index++) {
const transactionID = String(index);
transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = {
...createRandomTransaction(index),
transactionID,
reportID: REPORT_ID,
pendingAction: null,
errorFields: undefined,
};
reportActions.push({
...createRandomReportAction(index),
reportActionID: `iou${index}`,
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
pendingAction: null,
message: [{type: 'COMMENT', html: '$1.00 expense', text: '$1.00 expense'}],
// eslint-disable-next-line @typescript-eslint/no-deprecated -- getOriginalMessage reads the expense's IOUTransactionID from here
originalMessage: {IOUTransactionID: transactionID, IOUReportID: REPORT_ID, type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, amount: 100, currency: CONST.CURRENCY.USD},
});
reportActions.push({
...createRandomReportAction(expenseCount + index),
reportActionID: `comment${index}`,
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
pendingAction: null,
message: [{type: 'COMMENT', html: 'comment', text: 'comment'}],
});
}
return {transactions, reportActions};
}

describe('getAllNonDeletedTransactions', () => {
for (const expenseCount of EXPENSE_COUNTS) {
test(`[MoneyRequestReportUtils] getAllNonDeletedTransactions on a report with ${expenseCount} expenses`, async () => {
// Given a report where every expense has its action and a comment
const {transactions, reportActions} = buildReport(expenseCount);
expect(getAllNonDeletedTransactions(transactions, reportActions, false, true)).toHaveLength(expenseCount);

// When the report's expenses are read, like on every update while the report opens
await measureFunction(() => getAllNonDeletedTransactions(transactions, reportActions, false, true));
// Then reassure measures one call, which should grow only linearly with the expenses
});
}
});
15 changes: 15 additions & 0 deletions tests/unit/libs/MoneyRequestReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ describe('getAllNonDeletedTransactions', () => {
expect(result.map((t) => t.transactionID)).toContain(transactionR14932.transactionID);
expect(result.map((t) => t.transactionID)).toContain(orphanedTransaction.transactionID);
});

test('should decide by the first action of an expense when several actions point to it', () => {
// Given an expense with a deleted action and a live action, as getIOUActionForTransactionID returns the first one
const deletedAction = {...actionR14932, reportActionID: 'deleted1', originalMessage: {...originalMessageR14932, deleted: '2026-08-25 16:46:25.669'}};
const liveAction = {...actionR14932, reportActionID: 'live1'};
const transactions = {[transactionR14932.transactionID]: transactionR14932};

// When the deleted action comes first, then when the live action comes first
const deletedFirst = getAllNonDeletedTransactions(transactions, [deletedAction, liveAction]);
const liveFirst = getAllNonDeletedTransactions(transactions, [liveAction, deletedAction]);

// Then only the order of the actions decides whether the expense stays on the report
expect(deletedFirst).toHaveLength(0);
expect(liveFirst.map((t) => t.transactionID)).toEqual([transactionR14932.transactionID]);
});
});

describe('getAllNonDeletedTransactions - Expensify Card pending/posted deduplication', () => {
Expand Down
Loading