From 79acb6e985ab52edf9ec905631ebdb14a3d9d1b2 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 16 Jun 2026 20:00:17 +0200 Subject: [PATCH 01/22] extract reports out of changeTransactionsReport, yet to fix useUndeleteTransactions --- src/components/AddExistingExpenseFooter.tsx | 3 + .../useChangeTransactionsReportReports.ts | 61 +++++++++++++++++++ src/hooks/useUndeleteTransactions.ts | 3 + src/libs/actions/Transaction.ts | 33 ++++------ ...DynamicNewReportWorkspaceSelectionPage.tsx | 4 ++ .../Search/SearchTransactionsChangeReport.tsx | 5 ++ .../iou/request/step/IOURequestEditReport.tsx | 5 ++ .../hooks/useReportSelectionActions.ts | 5 ++ .../request/step/IOURequestStepUpgrade.tsx | 5 ++ tests/actions/TransactionTest.ts | 10 +++ tests/unit/TransactionTest.ts | 50 ++++++++++++++- 11 files changed, 162 insertions(+), 22 deletions(-) create mode 100644 src/hooks/useChangeTransactionsReportReports.ts diff --git a/src/components/AddExistingExpenseFooter.tsx b/src/components/AddExistingExpenseFooter.tsx index 6777ef1269d1..9ab09b3e5910 100644 --- a/src/components/AddExistingExpenseFooter.tsx +++ b/src/components/AddExistingExpenseFooter.tsx @@ -1,5 +1,6 @@ import React from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import useChangeTransactionsReportReports from '@hooks/useChangeTransactionsReportReports'; import useLocalize from '@hooks/useLocalize'; import useOnyx from '@hooks/useOnyx'; import usePermissions from '@hooks/usePermissions'; @@ -59,6 +60,7 @@ function AddExistingExpenseFooter({selectedIds, report, reportToConfirm, reportN const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.chatReportID}`); const [policyTagList] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policy?.id}`); const [chatReportPolicyTagList] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${chatReport?.policyID}`); + const reports = useChangeTransactionsReportReports([...selectedIds], selectedTransactions, reportToConfirm); const handleConfirm = () => { if (selectedIds.size === 0) { @@ -96,6 +98,7 @@ function AddExistingExpenseFooter({selectedIds, report, reportToConfirm, reportN allTransactions: selectedTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); } }, diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts new file mode 100644 index 000000000000..fe7824232af2 --- /dev/null +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -0,0 +1,61 @@ +import {useCallback} from 'react'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import {getIOUActionForReportID} from '@libs/ReportActionsUtils'; +import {findSelfDMReportID} from '@libs/ReportUtils'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Report, Transaction} from '@src/types/onyx'; +import {getEmptyObject} from '@src/types/utils/EmptyObject'; +import useOnyx from './useOnyx'; + +/** Resolve the REPORT subset that `changeTransactionsReport` looks up — keyed like the full collection so the action can do `reports?.[\`${KEY}${id}\`]`. */ +function useChangeTransactionsReportReports(transactionIDs: string[], allTransactions: OnyxCollection, newReport: OnyxEntry): OnyxCollection { + const newReportID = newReport?.reportID; + const reportsSelector = useCallback( + (allReports: OnyxCollection): OnyxCollection => { + const ids = new Set(); + const selfDMReportID = findSelfDMReportID(); + if (selfDMReportID) { + ids.add(selfDMReportID); + } + if (newReportID) { + ids.add(newReportID); + } + for (const transactionID of transactionIDs) { + const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; + if (!transaction) { + continue; + } + if (transaction.reportID) { + ids.add(transaction.reportID); + } + // The action picks the IOU action from either source depending on whether the expense is unreported. + const sources = [transaction.reportID, selfDMReportID].filter((id): id is string => !!id); + for (const source of sources) { + const iouAction = getIOUActionForReportID(source, transaction.transactionID); + if (iouAction?.childReportID) { + ids.add(iouAction.childReportID); + } + if (iouAction?.reportActionID) { + ids.add(iouAction.reportActionID); + } + } + } + const result: Record = {}; + for (const id of ids) { + const key = `${ONYXKEYS.COLLECTION.REPORT}${id}` as const; + const report = allReports?.[key]; + if (report) { + result[key] = report; + } + } + return result as OnyxCollection; + }, + [transactionIDs, allTransactions, newReportID], + ); + + const [reports = getEmptyObject>>()] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: reportsSelector}, [reportsSelector]); + + return reports; +} + +export default useChangeTransactionsReportReports; diff --git a/src/hooks/useUndeleteTransactions.ts b/src/hooks/useUndeleteTransactions.ts index 8ad31027621b..fbbf7bdc2c36 100644 --- a/src/hooks/useUndeleteTransactions.ts +++ b/src/hooks/useUndeleteTransactions.ts @@ -14,6 +14,8 @@ function useUndeleteTransactions() { const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${personalPolicyID}`); const [policyTagList] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policy?.id}`); const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); + // Transactions are only known at click time, so we can't pre-resolve the REPORT subset at render — full collection it is. + const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); return (transactions: Transaction[]) => { const transactionIDs = transactions.map((transaction) => transaction.transactionID); @@ -28,6 +30,7 @@ function useUndeleteTransactions() { allTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); }; } diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index 747d16391977..e6ea58b4f0e2 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -83,18 +83,6 @@ import type {SearchDataTypes} from '@src/types/onyx/SearchResults'; import type {Waypoint, WaypointCollection} from '@src/types/onyx/Transaction'; import type TransactionState from '@src/types/utils/TransactionStateType'; -let allReports: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (value) => { - if (!value) { - return; - } - allReports = value; - }, -}); - let allTransactionViolations: TransactionViolations = []; Onyx.connect({ key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, @@ -839,6 +827,8 @@ type ChangeTransactionsReportProps = { allTransactions: OnyxCollection; policyTagList: OnyxEntry; allTransactionViolation?: OnyxCollection; + /** Subset of the REPORT collection containing every report this call may look up (resolved by `useChangeTransactionsReportReports`). */ + reports: OnyxCollection; }; function changeTransactionsReport({ @@ -853,6 +843,7 @@ function changeTransactionsReport({ allTransactions, policyTagList, allTransactionViolation = {}, + reports, }: ChangeTransactionsReportProps) { const reportID = newReport?.reportID ?? CONST.REPORT.UNREPORTED_REPORT_ID; @@ -1053,7 +1044,7 @@ function changeTransactionsReport({ transactionsMoved = true; const oldReportID = isUnreportedExpense ? CONST.REPORT.UNREPORTED_REPORT_ID : transaction.reportID; - const oldReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldReportID}`]; + const oldReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldReportID}`]; const sourceCurrency = oldReport?.currency; const shouldClearAmount = shouldClearConvertedAmount(transaction, sourceCurrency, destinationCurrency); @@ -1335,7 +1326,7 @@ function changeTransactionsReport({ if (targetReportID) { const targetReportKey = `${ONYXKEYS.COLLECTION.REPORT}${targetReportID}`; const targetReport = - allReports?.[targetReportKey] ?? (targetReportID === newReport?.reportID ? newReport : undefined) ?? (targetReportID === selfDMReport?.reportID ? selfDMReport : undefined); + reports?.[targetReportKey] ?? (targetReportID === newReport?.reportID ? newReport : undefined) ?? (targetReportID === selfDMReport?.reportID ? selfDMReport : undefined); const targetReportCurrencies = getTargetReportCurrencies(targetReportID); const targetReportTransactionCount = updatedReportTransactionCounts[targetReportID] ?? targetReport?.transactionCount ?? 0; updatedReportTransactionCounts[targetReportID] = targetReportTransactionCount + 1; @@ -1433,7 +1424,7 @@ function changeTransactionsReport({ } const shouldRemoveOtherParticipants = !isManagedCardTransaction(transaction); - const childReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${newIOUAction.childReportID}`]; + const childReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${newIOUAction.childReportID}`]; if (childReport) { const participants = childReport.participants; // 5. Optimistically update the transaction thread and all threads in the transaction thread @@ -1456,7 +1447,7 @@ function changeTransactionsReport({ value: { parentReportID: isUnreportedExpense ? selfDMReportID : oldReportID, parentReportActionID: oldIOUAction.reportActionID, - policyID: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldIOUAction.reportActionID}`]?.policyID, + policyID: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldIOUAction.reportActionID}`]?.policyID, }, }); } @@ -1611,7 +1602,7 @@ function changeTransactionsReport({ failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`, - value: {total: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.total}, + value: {total: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.total}, }); } for (const [reportIDToUpdate, transactionCount] of Object.entries(updatedReportTransactionCounts)) { @@ -1623,7 +1614,7 @@ function changeTransactionsReport({ failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`, - value: {transactionCount: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.transactionCount}, + value: {transactionCount: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.transactionCount}, }); } @@ -1637,7 +1628,7 @@ function changeTransactionsReport({ failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`, - value: {nonReimbursableTotal: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.nonReimbursableTotal}, + value: {nonReimbursableTotal: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.nonReimbursableTotal}, }); } @@ -1652,7 +1643,7 @@ function changeTransactionsReport({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`, value: { - unheldNonReimbursableTotal: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.unheldNonReimbursableTotal, + unheldNonReimbursableTotal: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.unheldNonReimbursableTotal, }, }); } @@ -1696,7 +1687,7 @@ function changeTransactionsReport({ for (const affectedReportID of affectedReportIDs) { const affectedReport = - allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${affectedReportID}`] ?? + reports?.[`${ONYXKEYS.COLLECTION.REPORT}${affectedReportID}`] ?? (affectedReportID === newReport?.reportID ? newReport : undefined) ?? (affectedReportID === selfDMReport?.reportID ? selfDMReport : undefined); diff --git a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx index 769ce6b08d63..3517bff10a8e 100644 --- a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx +++ b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx @@ -10,6 +10,7 @@ import SelectionList from '@components/SelectionList'; import UserListItem from '@components/SelectionList/ListItem/UserListItem'; import type {ListItem} from '@components/SelectionList/types'; import Text from '@components/Text'; +import useChangeTransactionsReportReports from '@hooks/useChangeTransactionsReportReports'; import useCreateEmptyReportConfirmation from '@hooks/useCreateEmptyReportConfirmation'; import useCreateNewReport from '@hooks/useCreateNewReport'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; @@ -75,6 +76,8 @@ function DynamicNewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelec const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION); const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); + const transactionIDsForReports = Object.keys(selectedTransactions).length ? Object.keys(selectedTransactions) : selectedTransactionIDs; + const reports = useChangeTransactionsReportReports(transactionIDsForReports, allTransactions, undefined); const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP); const shouldShowLoadingIndicator = isLoadingApp && !isOffline; @@ -126,6 +129,7 @@ function DynamicNewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelec allTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); // eslint-disable-next-line rulesdir/no-default-id-values diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index c00ab3bad851..5670ae56c47a 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -6,6 +6,7 @@ import Onyx from 'react-native-onyx'; import {usePersonalDetails, useSession} from '@components/OnyxListItemProvider'; import {useSearchResultsContext, useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext'; import type {ListItem} from '@components/SelectionList/types'; +import useChangeTransactionsReportReports from '@hooks/useChangeTransactionsReportReports'; import useConditionalCreateEmptyReportConfirmation from '@hooks/useConditionalCreateEmptyReportConfirmation'; import useHasPerDiemTransactions from '@hooks/useHasPerDiemTransactions'; import useOnyx from '@hooks/useOnyx'; @@ -58,6 +59,7 @@ function SearchTransactionsChangeReport() { const [allPolicyTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS); const hasPerDiemTransactions = useHasPerDiemTransactions(selectedTransactionsKeys); const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); + const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, transactions, undefined); const {isBetaEnabled} = usePermissions(); const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); const session = useSession(); @@ -164,6 +166,7 @@ function SearchTransactionsChangeReport() { allTransactions: transactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); clearSelectedTransactions(); }); @@ -241,6 +244,7 @@ function SearchTransactionsChangeReport() { allTransactions: transactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); InteractionManager.runAfterInteractions(() => { clearSelectedTransactions(); @@ -263,6 +267,7 @@ function SearchTransactionsChangeReport() { allTransactions: transactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); clearSelectedTransactions(); Navigation.goBack(); diff --git a/src/pages/iou/request/step/IOURequestEditReport.tsx b/src/pages/iou/request/step/IOURequestEditReport.tsx index 62d1c97f59b0..b6dc9b8ada29 100644 --- a/src/pages/iou/request/step/IOURequestEditReport.tsx +++ b/src/pages/iou/request/step/IOURequestEditReport.tsx @@ -3,6 +3,7 @@ import type {OnyxEntry} from 'react-native-onyx'; import {usePersonalDetails} from '@components/OnyxListItemProvider'; import {useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext'; import type {ListItem} from '@components/SelectionList/types'; +import useChangeTransactionsReportReports from '@hooks/useChangeTransactionsReportReports'; import useConditionalCreateEmptyReportConfirmation from '@hooks/useConditionalCreateEmptyReportConfirmation'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import useHasPerDiemTransactions from '@hooks/useHasPerDiemTransactions'; @@ -73,6 +74,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined; const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION); const [betas] = useOnyx(ONYXKEYS.BETAS); + const reports = useChangeTransactionsReportReports(transactionIDs, allTransactions, selectedReport); const selectReport = (item: TransactionGroupListItem, report?: OnyxEntry) => { if (transactionIDs.length === 0 || item.value === reportID) { Navigation.dismissToSuperWideRHP(); @@ -81,6 +83,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { const newReport = report ?? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${item.value}`]; const policyTagList = item?.policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${item.policyID}`] : {}; + const reportsForCall = newReport?.reportID ? {[`${ONYXKEYS.COLLECTION.REPORT}${newReport.reportID}`]: newReport, ...reports} : reports; setNavigationActionToMicrotaskQueue(() => { changeTransactionsReport({ @@ -95,6 +98,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { allTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports: reportsForCall, }); turnOffMobileSelectionMode(); clearSelectedTransactions(true); @@ -117,6 +121,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) { allTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); if (shouldTurnOffSelectionMode) { turnOffMobileSelectionMode(); diff --git a/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts b/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts index af0f69af2343..bb61cdc2e748 100644 --- a/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts +++ b/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts @@ -3,6 +3,7 @@ import {InteractionManager} from 'react-native'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {useSearchSelectionActions} from '@components/Search/SearchContext'; import type {ListItem} from '@components/SelectionList/types'; +import useChangeTransactionsReportReports from '@hooks/useChangeTransactionsReportReports'; import useOnyx from '@hooks/useOnyx'; import usePermissions from '@hooks/usePermissions'; import {setCustomUnitID, setCustomUnitRateID} from '@libs/actions/IOU/MoneyRequest'; @@ -106,6 +107,7 @@ function useReportSelectionActions({ const {removeTransaction} = useSearchSelectionActions(); const {isBetaEnabled} = usePermissions(); const isNewManualExpenseFlowEnabled = isBetaEnabled(CONST.BETAS.NEW_MANUAL_EXPENSE_FLOW); + const reports = useChangeTransactionsReportReports(transaction?.transactionID ? [transaction.transactionID] : [], allTransactions, undefined); const buildParticipants = (report: OnyxEntry) => [ { @@ -190,6 +192,7 @@ function useReportSelectionActions({ if (isEditing) { const policyTagList = item?.policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${item.policyID}`] : {}; + const reportsForCall = report?.reportID ? {[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`]: report, ...reports} : reports; changeTransactionsReport({ transactionIDs: [transaction.transactionID], isASAPSubmitBetaEnabled, @@ -202,6 +205,7 @@ function useReportSelectionActions({ allTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports: reportsForCall, }); removeTransaction(transaction.transactionID); } @@ -225,6 +229,7 @@ function useReportSelectionActions({ allTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); removeTransaction(transaction.transactionID); }); diff --git a/src/pages/iou/request/step/IOURequestStepUpgrade.tsx b/src/pages/iou/request/step/IOURequestStepUpgrade.tsx index 2614106b1ea3..95c47ffbe937 100644 --- a/src/pages/iou/request/step/IOURequestStepUpgrade.tsx +++ b/src/pages/iou/request/step/IOURequestStepUpgrade.tsx @@ -10,6 +10,7 @@ import {useSearchSelectionActions, useSearchSelectionContext} from '@components/ import WorkspaceConfirmationForm from '@components/WorkspaceConfirmationForm'; import type {WorkspaceConfirmationSubmitFunctionParams} from '@components/WorkspaceConfirmationForm'; import useActivePolicy from '@hooks/useActivePolicy'; +import useChangeTransactionsReportReports from '@hooks/useChangeTransactionsReportReports'; import useCreateNewReport from '@hooks/useCreateNewReport'; import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; import useHasActiveAdminPolicies from '@hooks/useHasActiveAdminPolicies'; @@ -109,6 +110,8 @@ function IOURequestStepUpgrade({ const {isBetaEnabled} = usePermissions(); const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); + // The destination is an optimistic report created at click time, so it isn't yet in Onyx. + const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, allTransactions, undefined); const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? ''); const ownerAccountID = selectedReport?.ownerAccountID ?? currentUserPersonalDetails.accountID; @@ -152,6 +155,7 @@ function IOURequestStepUpgrade({ allTransactions, policyTagList, allTransactionViolation: transactionViolations, + reports, }); clearSelectedTransactions(); @@ -249,6 +253,7 @@ function IOURequestStepUpgrade({ allPolicyTags, createReportForCurrentUser, transactionViolations, + reports, ]); const participant = transaction?.participants?.[0]; diff --git a/tests/actions/TransactionTest.ts b/tests/actions/TransactionTest.ts index aa5696bf54e8..637f2c722d6d 100644 --- a/tests/actions/TransactionTest.ts +++ b/tests/actions/TransactionTest.ts @@ -272,6 +272,15 @@ describe('actions/Transaction', () => { const policyTagList = (await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${mockPolicy.id}`)) ?? {}; + let reports: OnyxCollection; + await getOnyxData({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => { + reports = value; + }, + }); + changeTransactionsReport({ transactionIDs: [transaction?.transactionID], isASAPSubmitBetaEnabled: false, @@ -281,6 +290,7 @@ describe('actions/Transaction', () => { policy: mockPolicy, allTransactions, policyTagList, + reports, }); let updatedTransaction: OnyxEntry; diff --git a/tests/unit/TransactionTest.ts b/tests/unit/TransactionTest.ts index a111a54f0ae6..ce35467b8eb1 100644 --- a/tests/unit/TransactionTest.ts +++ b/tests/unit/TransactionTest.ts @@ -1,5 +1,5 @@ import {act, renderHook, waitFor} from '@testing-library/react-native'; -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'; import type {ValueOf} from 'type-fest'; @@ -113,6 +113,20 @@ describe('Transaction', () => { }); describe('changeTransactionsReport', () => { + let reports: OnyxCollection; + + async function loadReports() { + await TestHelper.getOnyxData({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => { + reports = value; + }, + }); + } + + beforeEach(loadReports); + function createIOUAction(transaction: Transaction, reportID = transaction.reportID, type: ValueOf = CONST.IOU.REPORT_ACTION_TYPE.CREATE) { return { reportActionID: rand64(), @@ -151,6 +165,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); const reportActions = await new Promise>((resolve) => { @@ -188,6 +203,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); const reportActions = await new Promise>((resolve) => { @@ -239,6 +255,7 @@ describe('Transaction', () => { reportNextStep: mockReportNextStep, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -292,6 +309,7 @@ describe('Transaction', () => { reportNextStep: mockReportNextStep, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -333,6 +351,7 @@ describe('Transaction', () => { reportNextStep: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -377,6 +396,7 @@ describe('Transaction', () => { [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, }; + await loadReports(); changeTransactionsReport({ transactionIDs: [transaction.transactionID], isASAPSubmitBetaEnabled: false, @@ -386,6 +406,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -431,6 +452,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -473,6 +495,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -521,6 +544,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); const report = await new Promise>((resolve) => { @@ -569,6 +593,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); const report = await new Promise>((resolve) => { @@ -624,6 +649,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); const report = await new Promise>((resolve) => { @@ -669,6 +695,7 @@ describe('Transaction', () => { [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, }; + await loadReports(); changeTransactionsReport({ transactionIDs: [transaction.transactionID], isASAPSubmitBetaEnabled: false, @@ -678,6 +705,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); const report = await new Promise>((resolve) => { @@ -716,6 +744,7 @@ describe('Transaction', () => { const allTransactions = { [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, }; + await loadReports(); changeTransactionsReport({ transactionIDs: [transaction.transactionID], isASAPSubmitBetaEnabled: false, @@ -725,6 +754,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -774,6 +804,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -858,6 +889,7 @@ describe('Transaction', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${destinationExpenseReport.reportID}`, destinationExpenseReport); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${sourceExpenseReport.reportID}`, sourceIOUActions); + await loadReports(); changeTransactionsReport({ transactionIDs: [usdTransaction.transactionID, movedBgnTransaction.transactionID], isASAPSubmitBetaEnabled: false, @@ -867,6 +899,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -932,6 +965,7 @@ describe('Transaction', () => { policyCategories, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -975,6 +1009,7 @@ describe('Transaction', () => { [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, }; + await loadReports(); changeTransactionsReport({ transactionIDs: [transaction.transactionID], isASAPSubmitBetaEnabled: false, @@ -986,6 +1021,7 @@ describe('Transaction', () => { policyCategories: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1041,6 +1077,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1080,6 +1117,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1114,6 +1152,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1161,6 +1200,7 @@ describe('Transaction', () => { policy: undefined, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1220,6 +1260,7 @@ describe('Transaction', () => { policy, allTransactions, policyTagList, + reports, }); await waitForBatchedUpdates(); @@ -1266,6 +1307,7 @@ describe('Transaction', () => { allTransactions, policyTagList: undefined, allTransactionViolation: {[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]: [receiptNoticeViolation]}, + reports, }); await waitForBatchedUpdates(); @@ -1327,6 +1369,7 @@ describe('Transaction', () => { policy, allTransactions, policyTagList, + reports, }); await waitForBatchedUpdates(); @@ -1374,6 +1417,7 @@ describe('Transaction', () => { policy, allTransactions, policyTagList: {}, + reports, }); await waitForBatchedUpdates(); @@ -1449,6 +1493,7 @@ describe('Transaction', () => { policy, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1533,6 +1578,7 @@ describe('Transaction', () => { policy, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1608,6 +1654,7 @@ describe('Transaction', () => { policy, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); @@ -1684,6 +1731,7 @@ describe('Transaction', () => { policy, allTransactions, policyTagList: undefined, + reports, }); await waitForBatchedUpdates(); From e5a7cc16a699e05b39d96cf5f177febddff90f4c Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Mon, 22 Jun 2026 09:17:19 +0200 Subject: [PATCH 02/22] fix tests --- tests/unit/TransactionTest.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/TransactionTest.ts b/tests/unit/TransactionTest.ts index 0237ee82daf5..30959ac19443 100644 --- a/tests/unit/TransactionTest.ts +++ b/tests/unit/TransactionTest.ts @@ -804,6 +804,7 @@ describe('Transaction', () => { const allTransactions = { [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, }; + await loadReports(); changeTransactionsReport({ transactionIDs: [transaction.transactionID], isASAPSubmitBetaEnabled: false, @@ -870,6 +871,7 @@ describe('Transaction', () => { [`${ONYXKEYS.COLLECTION.TRANSACTION}${firstTransaction.transactionID}`]: firstTransaction, [`${ONYXKEYS.COLLECTION.TRANSACTION}${secondTransaction.transactionID}`]: secondTransaction, }; + await loadReports(); changeTransactionsReport({ transactionIDs: [firstTransaction.transactionID, secondTransaction.transactionID], isASAPSubmitBetaEnabled: false, From e22feef9d8f5b091225f92b8f6ef0f29d4e59ecb Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Mon, 22 Jun 2026 13:33:18 +0200 Subject: [PATCH 03/22] include codex suggestion --- src/pages/Search/SearchTransactionsChangeReport.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index 5670ae56c47a..6d73bd99fa56 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -232,6 +232,7 @@ function SearchTransactionsChangeReport() { const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${item.value}`]; const destinationReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${item.value}`]; const policyTagList = item?.policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${item.policyID}`] : {}; + const reportsForCall = destinationReport?.reportID ? {[`${ONYXKEYS.COLLECTION.REPORT}${destinationReport.reportID}`]: destinationReport, ...reports} : reports; changeTransactionsReport({ transactionIDs: selectedTransactionsKeys, isASAPSubmitBetaEnabled, @@ -244,7 +245,7 @@ function SearchTransactionsChangeReport() { allTransactions: transactions, policyTagList, allTransactionViolation: transactionViolations, - reports, + reports: reportsForCall, }); InteractionManager.runAfterInteractions(() => { clearSelectedTransactions(); From cfb2c23c8e11005d8f18e6d46c5547ab574ddbad Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Mon, 22 Jun 2026 14:02:36 +0200 Subject: [PATCH 04/22] remove selector from useChangeTransactionsReportReports --- .../useChangeTransactionsReportReports.ts | 83 +++++++++---------- 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts index fe7824232af2..2a9add0a9341 100644 --- a/src/hooks/useChangeTransactionsReportReports.ts +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -1,61 +1,52 @@ -import {useCallback} from 'react'; import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {getIOUActionForReportID} from '@libs/ReportActionsUtils'; import {findSelfDMReportID} from '@libs/ReportUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, Transaction} from '@src/types/onyx'; -import {getEmptyObject} from '@src/types/utils/EmptyObject'; import useOnyx from './useOnyx'; /** Resolve the REPORT subset that `changeTransactionsReport` looks up — keyed like the full collection so the action can do `reports?.[\`${KEY}${id}\`]`. */ function useChangeTransactionsReportReports(transactionIDs: string[], allTransactions: OnyxCollection, newReport: OnyxEntry): OnyxCollection { const newReportID = newReport?.reportID; - const reportsSelector = useCallback( - (allReports: OnyxCollection): OnyxCollection => { - const ids = new Set(); - const selfDMReportID = findSelfDMReportID(); - if (selfDMReportID) { - ids.add(selfDMReportID); - } - if (newReportID) { - ids.add(newReportID); - } - for (const transactionID of transactionIDs) { - const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; - if (!transaction) { - continue; - } - if (transaction.reportID) { - ids.add(transaction.reportID); - } - // The action picks the IOU action from either source depending on whether the expense is unreported. - const sources = [transaction.reportID, selfDMReportID].filter((id): id is string => !!id); - for (const source of sources) { - const iouAction = getIOUActionForReportID(source, transaction.transactionID); - if (iouAction?.childReportID) { - ids.add(iouAction.childReportID); - } - if (iouAction?.reportActionID) { - ids.add(iouAction.reportActionID); - } - } + const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); + + const ids = new Set(); + const selfDMReportID = findSelfDMReportID(); + if (selfDMReportID) { + ids.add(selfDMReportID); + } + if (newReportID) { + ids.add(newReportID); + } + for (const transactionID of transactionIDs) { + const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; + if (!transaction) { + continue; + } + if (transaction.reportID) { + ids.add(transaction.reportID); + } + // The action picks the IOU action from either source depending on whether the expense is unreported. + const sources = [transaction.reportID, selfDMReportID].filter((id): id is string => !!id); + for (const source of sources) { + const iouAction = getIOUActionForReportID(source, transaction.transactionID); + if (iouAction?.childReportID) { + ids.add(iouAction.childReportID); } - const result: Record = {}; - for (const id of ids) { - const key = `${ONYXKEYS.COLLECTION.REPORT}${id}` as const; - const report = allReports?.[key]; - if (report) { - result[key] = report; - } + if (iouAction?.reportActionID) { + ids.add(iouAction.reportActionID); } - return result as OnyxCollection; - }, - [transactionIDs, allTransactions, newReportID], - ); - - const [reports = getEmptyObject>>()] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: reportsSelector}, [reportsSelector]); - - return reports; + } + } + const result: Record = {}; + for (const id of ids) { + const key = `${ONYXKEYS.COLLECTION.REPORT}${id}` as const; + const report = allReports?.[key]; + if (report) { + result[key] = report; + } + } + return result as OnyxCollection; } export default useChangeTransactionsReportReports; From c70ce5a0bd2b3a40e4f57318520ab9f54a84910e Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Thu, 25 Jun 2026 13:47:11 +0200 Subject: [PATCH 05/22] fix tests --- src/pages/Search/SearchTransactionsChangeReport.tsx | 6 +++++- .../iou/request/step/DynamicIOURequestStepUpgradePage.tsx | 6 +++++- .../IOURequestStepReport/hooks/useReportSelectionActions.ts | 5 ++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index 95ac406c8201..7c076f1f9900 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -2,6 +2,7 @@ import React, {useEffect, useMemo} from 'react'; // eslint-disable-next-line no-restricted-imports import {InteractionManager} from 'react-native'; import Onyx from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; import {usePersonalDetails, useSession} from '@components/OnyxListItemProvider'; import {useSearchResultsContext, useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext'; import type {ListItem} from '@components/SelectionList/types'; @@ -50,7 +51,10 @@ function SearchTransactionsChangeReport() { const [allPolicyTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS); const hasPerDiemTransactions = useHasPerDiemTransactions(selectedTransactionsKeys); const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); - const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, transactions, undefined); + const transactionsCollection: OnyxCollection = Object.fromEntries( + transactions.map((transactionItem) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`, transactionItem]), + ); + const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, transactionsCollection, undefined); const {isBetaEnabled} = usePermissions(); const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); const session = useSession(); diff --git a/src/pages/iou/request/step/DynamicIOURequestStepUpgradePage.tsx b/src/pages/iou/request/step/DynamicIOURequestStepUpgradePage.tsx index 314ac7df8faf..a5da513ca03e 100644 --- a/src/pages/iou/request/step/DynamicIOURequestStepUpgradePage.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestStepUpgradePage.tsx @@ -1,5 +1,6 @@ import {hasSeenTourSelector} from '@selectors/Onboarding'; import React, {useCallback, useMemo, useRef, useState} from 'react'; +import type {OnyxCollection} from 'react-native-onyx'; import ConfirmModal from '@components/ConfirmModal'; import HeaderWithBackButton from '@components/HeaderWithBackButton'; import {usePersonalDetails} from '@components/OnyxListItemProvider'; @@ -101,7 +102,10 @@ function DynamicIOURequestStepUpgradePage({ const {isBetaEnabled} = usePermissions(); const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); // The destination is an optimistic report created at click time, so it isn't yet in Onyx. - const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, allTransactions, undefined); + const transactionsCollection: OnyxCollection = Object.fromEntries( + transactions.map((transactionItem) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`, transactionItem]), + ); + const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, transactionsCollection, undefined); const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? ''); const ownerAccountID = selectedReport?.ownerAccountID ?? currentUserPersonalDetails.accountID; diff --git a/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts b/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts index 8f555a24b4a6..8773bf629dba 100644 --- a/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts +++ b/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts @@ -106,7 +106,10 @@ function useReportSelectionActions({ const {removeTransaction} = useSearchSelectionActions(); const {isBetaEnabled} = usePermissions(); const isNewManualExpenseFlowEnabled = isBetaEnabled(CONST.BETAS.NEW_MANUAL_EXPENSE_FLOW); - const reports = useChangeTransactionsReportReports(transaction?.transactionID ? [transaction.transactionID] : [], allTransactions, undefined); + const transactionsCollection: OnyxCollection = Object.fromEntries( + transactions.map((transactionItem) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`, transactionItem]), + ); + const reports = useChangeTransactionsReportReports(transaction?.transactionID ? [transaction.transactionID] : [], transactionsCollection, undefined); const targetTransactionIDs = transaction?.transactionID ? [transaction.transactionID] : []; const targetTransactions = transaction ? [transaction] : []; From 295a314d3729db9044ae358eafec6155c832e818 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Thu, 25 Jun 2026 18:07:41 +0200 Subject: [PATCH 06/22] patch changeTransactionsReport --- Mobile-Expensify | 2 +- src/pages/DynamicNewReportWorkspaceSelectionPage.tsx | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Mobile-Expensify b/Mobile-Expensify index 08be844985dc..81df53c398be 160000 --- a/Mobile-Expensify +++ b/Mobile-Expensify @@ -1 +1 @@ -Subproject commit 08be844985dcf73ca3a60a03c624ca4646610e84 +Subproject commit 81df53c398befa32ba3c204e63949ba59cd2651a diff --git a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx index f8b2f8e6af23..b22329b67c5f 100644 --- a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx +++ b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx @@ -120,6 +120,11 @@ function DynamicNewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelec if (isMovingExpenses && (!!selectedTransactionsKeys.length || !!selectedTransactionIDs.length)) { const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${optimisticReport.reportID}`]; const policyTagList = policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] : {}; + // Include the optimistic report (totals/counts zeroed) so changeTransactionsReport can restore them if the move fails. + const reportsForCall = { + ...reports, + [`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: {...optimisticReport, transactionCount: 0, unheldNonReimbursableTotal: 0}, + }; setNavigationActionToMicrotaskQueue(() => { changeTransactionsReport({ transactionIDs, @@ -133,7 +138,7 @@ function DynamicNewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelec policyTagList, transactions, allTransactionViolation: transactionViolations, - reports, + reports: reportsForCall, }); // eslint-disable-next-line rulesdir/no-default-id-values From d3919881a8c02ff6680456dad9c35721e6ca798a Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Mon, 6 Jul 2026 14:43:15 +0200 Subject: [PATCH 07/22] format new hook with fmt --- src/hooks/useChangeTransactionsReportReports.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts index 2a9add0a9341..5703f5c372af 100644 --- a/src/hooks/useChangeTransactionsReportReports.ts +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -1,8 +1,11 @@ -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import {getIOUActionForReportID} from '@libs/ReportActionsUtils'; import {findSelfDMReportID} from '@libs/ReportUtils'; + import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, Transaction} from '@src/types/onyx'; + +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; + import useOnyx from './useOnyx'; /** Resolve the REPORT subset that `changeTransactionsReport` looks up — keyed like the full collection so the action can do `reports?.[\`${KEY}${id}\`]`. */ From c61712ec7db3159bff062914f8ab9b293f28c6ca Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Mon, 6 Jul 2026 14:53:31 +0200 Subject: [PATCH 08/22] use reports argument in new allReport usages --- src/libs/actions/Transaction.ts | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index 3576fe571231..1dfbcdfb1272 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -88,18 +88,6 @@ import {getUnixTime} from 'date-fns'; import lodashClone from 'lodash/clone'; import Onyx from 'react-native-onyx'; -let allReports: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (value) => { - if (!value) { - return; - } - allReports = value; - }, -}); - type SaveWaypointProps = { transactionID: string; index: string; @@ -1741,7 +1729,7 @@ function changeTransactionsReport({ failureData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`, - value: {reimbursableTotal: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.reimbursableTotal}, + value: {reimbursableTotal: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.reimbursableTotal}, }); } @@ -1756,7 +1744,7 @@ function changeTransactionsReport({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`, value: { - unheldReimbursableTotal: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.unheldReimbursableTotal, + unheldReimbursableTotal: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportIDToUpdate}`]?.unheldReimbursableTotal, }, }); } From b2e64afb243a218d42d0bea27aa7662550b30cb3 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Wed, 15 Jul 2026 21:28:22 +0200 Subject: [PATCH 09/22] Narrow report subscription with a useOnyx selector and drop redundant comments --- .../useChangeTransactionsReportReports.ts | 25 +++++++++++-------- src/hooks/useUndeleteTransactions.ts | 1 - src/libs/actions/Transaction.ts | 1 - ...DynamicNewReportWorkspaceSelectionPage.tsx | 1 - .../Search/SearchTransactionsChangeReport.tsx | 4 +-- .../request/step/IOURequestStepUpgrade.tsx | 1 - 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts index 5703f5c372af..37a293c1a856 100644 --- a/src/hooks/useChangeTransactionsReportReports.ts +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -11,7 +11,6 @@ import useOnyx from './useOnyx'; /** Resolve the REPORT subset that `changeTransactionsReport` looks up — keyed like the full collection so the action can do `reports?.[\`${KEY}${id}\`]`. */ function useChangeTransactionsReportReports(transactionIDs: string[], allTransactions: OnyxCollection, newReport: OnyxEntry): OnyxCollection { const newReportID = newReport?.reportID; - const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); const ids = new Set(); const selfDMReportID = findSelfDMReportID(); @@ -29,7 +28,6 @@ function useChangeTransactionsReportReports(transactionIDs: string[], allTransac if (transaction.reportID) { ids.add(transaction.reportID); } - // The action picks the IOU action from either source depending on whether the expense is unreported. const sources = [transaction.reportID, selfDMReportID].filter((id): id is string => !!id); for (const source of sources) { const iouAction = getIOUActionForReportID(source, transaction.transactionID); @@ -41,15 +39,20 @@ function useChangeTransactionsReportReports(transactionIDs: string[], allTransac } } } - const result: Record = {}; - for (const id of ids) { - const key = `${ONYXKEYS.COLLECTION.REPORT}${id}` as const; - const report = allReports?.[key]; - if (report) { - result[key] = report; - } - } - return result as OnyxCollection; + const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { + selector: (allReports) => { + const result: Record = {}; + for (const id of ids) { + const key = `${ONYXKEYS.COLLECTION.REPORT}${id}` as const; + const report = allReports?.[key]; + if (report) { + result[key] = report; + } + } + return result as OnyxCollection; + }, + }); + return reports; } export default useChangeTransactionsReportReports; diff --git a/src/hooks/useUndeleteTransactions.ts b/src/hooks/useUndeleteTransactions.ts index fec64d4737c2..05d1df377c71 100644 --- a/src/hooks/useUndeleteTransactions.ts +++ b/src/hooks/useUndeleteTransactions.ts @@ -16,7 +16,6 @@ function useUndeleteTransactions() { const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${personalPolicyID}`); const [policyTagList] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policy?.id}`); const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); - // Transactions are only known at click time, so we can't pre-resolve the REPORT subset at render — full collection it is. const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); return (transactions: Transaction[]) => { diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index 1dfbcdfb1272..c2e8899df828 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -826,7 +826,6 @@ type ChangeTransactionsReportProps = { policyTagList: OnyxEntry; transactions: Transaction[]; allTransactionViolation?: OnyxCollection; - /** Subset of the REPORT collection containing every report this call may look up (resolved by `useChangeTransactionsReportReports`). */ reports: OnyxCollection; }; diff --git a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx index b4a2ade9aaad..46de8183a39a 100644 --- a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx +++ b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx @@ -123,7 +123,6 @@ function DynamicNewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelec if (isMovingExpenses && (!!selectedTransactionsKeys.length || !!selectedTransactionIDs.length)) { const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${optimisticReport.reportID}`]; const policyTagList = policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] : {}; - // Include the optimistic report (totals/counts zeroed) so changeTransactionsReport can restore them if the move fails. const reportsForCall = { ...reports, [`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: {...optimisticReport, transactionCount: 0, unheldNonReimbursableTotal: 0}, diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index 835078bff9cf..8c309d5c4076 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -251,9 +251,7 @@ function SearchTransactionsChangeReport() { allTransactionViolation: transactionViolations, reports: reportsForCall, }); - Navigation.goBack(undefined, { - afterTransition: clearSelectedTransactions, - }); + Navigation.goBack(undefined, {afterTransition: clearSelectedTransactions}); }; const removeFromReport = () => { diff --git a/src/pages/iou/request/step/IOURequestStepUpgrade.tsx b/src/pages/iou/request/step/IOURequestStepUpgrade.tsx index b3cbc5e334de..2ccc1e068eca 100644 --- a/src/pages/iou/request/step/IOURequestStepUpgrade.tsx +++ b/src/pages/iou/request/step/IOURequestStepUpgrade.tsx @@ -105,7 +105,6 @@ function IOURequestStepUpgrade({ const {isBetaEnabled} = usePermissions(); const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); - // The destination is an optimistic report created at click time, so it isn't yet in Onyx. const transactionsCollection: OnyxCollection = Object.fromEntries( transactions.map((transactionItem) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`, transactionItem]), ); From a9772ef7d20a11af24702066625cae022cd0851a Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Wed, 15 Jul 2026 22:22:20 +0200 Subject: [PATCH 10/22] fix test --- tests/actions/TransactionTest.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/actions/TransactionTest.ts b/tests/actions/TransactionTest.ts index db66c0b4360c..13f8b60b3834 100644 --- a/tests/actions/TransactionTest.ts +++ b/tests/actions/TransactionTest.ts @@ -511,6 +511,15 @@ describe('actions/Transaction', () => { await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, transaction); await waitForBatchedUpdates(); + let reports: OnyxCollection; + await getOnyxData({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => { + reports = value; + }, + }); + changeTransactionsReport({ transactionIDs: [TRANSACTION_ID], isASAPSubmitBetaEnabled: false, @@ -521,7 +530,7 @@ describe('actions/Transaction', () => { allTransactions: {[`${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`]: transaction}, policyTagList: undefined, transactionViolations: {}, - reports: undefined, + reports, isTrackIntentUser: false, }); await waitForBatchedUpdates(); From 3ef45f8969e9c844b7ea08b6dad189d4fc81cc75 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 4 Aug 2026 12:14:57 +0200 Subject: [PATCH 11/22] fix test ts --- tests/actions/TransactionTest.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/actions/TransactionTest.ts b/tests/actions/TransactionTest.ts index afc0c59e2979..e8cfac0d194b 100644 --- a/tests/actions/TransactionTest.ts +++ b/tests/actions/TransactionTest.ts @@ -249,6 +249,7 @@ describe('actions/Transaction', () => { }); await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, callback: (transactions) => { transaction = Object.values(transactions ?? {}).find((t) => !!t); allTransactions = transactions; @@ -257,6 +258,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((r) => r?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -268,6 +270,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, callback: (allReportActions) => { selfDMReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`]; iouReportActionOnSelfDMReport = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`] ?? {}).find( @@ -328,6 +331,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, callback: (transactions) => { updatedTransaction = Object.values(transactions ?? {}).find((t) => t?.transactionID === transaction?.transactionID); }, @@ -335,6 +339,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, callback: (allReportActions) => { updatedIOUReportActionOnSelfDMReport = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`] ?? {}).find( (r) => r?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU, @@ -347,6 +352,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { updatedExpenseReport = Object.values(allReports ?? {}).find((r) => r?.reportID === expenseReport?.reportID); }, @@ -607,6 +613,7 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -645,6 +652,7 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -695,18 +703,21 @@ describe('actions/Transaction', () => { let allReportNameValuePairs: OnyxCollection; await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, @@ -786,6 +797,7 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -824,6 +836,7 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -874,18 +887,21 @@ describe('actions/Transaction', () => { let allReportNameValuePairs: OnyxCollection; await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, @@ -969,6 +985,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -1009,6 +1026,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -1066,18 +1084,21 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, @@ -1162,6 +1183,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -1203,6 +1225,7 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -1281,18 +1304,21 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, From 6c93ea30f2629c15c030e86b91a9658908380db1 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 4 Aug 2026 12:31:58 +0200 Subject: [PATCH 12/22] fix tests/actions/ReportTest.ts fail --- src/libs/actions/Report/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index 6f16a22f4bff..bdd42719950e 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -8284,7 +8284,7 @@ function mergeReports({ policyTagList: policyTagList ?? {}, transactions: transactionsToMove, allTransactionViolation, - allReports: reports, + reports, skippedReportIDs: sourceReportIDs, isTrackIntentUser, personalPolicyOutputCurrency, From d03de9a8d6ea7704d7aeaaa4d45f3c50316db561 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 4 Aug 2026 14:55:39 +0200 Subject: [PATCH 13/22] Fix tests broken by react-native-onyx API update waitForCollectionCallback no longer exists on ConnectOptions (collection vs entry is now inferred from the key type), and two changeTransactionsReport calls had a reports/allReports typo. --- tests/actions/TransactionTest.ts | 28 ---------------------------- tests/unit/TransactionTest.ts | 5 ++--- 2 files changed, 2 insertions(+), 31 deletions(-) diff --git a/tests/actions/TransactionTest.ts b/tests/actions/TransactionTest.ts index e8cfac0d194b..7a2d4ffaf4f0 100644 --- a/tests/actions/TransactionTest.ts +++ b/tests/actions/TransactionTest.ts @@ -249,7 +249,6 @@ describe('actions/Transaction', () => { }); await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, callback: (transactions) => { transaction = Object.values(transactions ?? {}).find((t) => !!t); allTransactions = transactions; @@ -258,7 +257,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((r) => r?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -270,7 +268,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, callback: (allReportActions) => { selfDMReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`]; iouReportActionOnSelfDMReport = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`] ?? {}).find( @@ -303,7 +300,6 @@ describe('actions/Transaction', () => { let reports: OnyxCollection; await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (value) => { reports = value; }, @@ -331,7 +327,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, callback: (transactions) => { updatedTransaction = Object.values(transactions ?? {}).find((t) => t?.transactionID === transaction?.transactionID); }, @@ -339,7 +334,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, callback: (allReportActions) => { updatedIOUReportActionOnSelfDMReport = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`] ?? {}).find( (r) => r?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU, @@ -352,7 +346,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { updatedExpenseReport = Object.values(allReports ?? {}).find((r) => r?.reportID === expenseReport?.reportID); }, @@ -522,7 +515,6 @@ describe('actions/Transaction', () => { let reports: OnyxCollection; await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (value) => { reports = value; }, @@ -613,7 +605,6 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -652,7 +643,6 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -703,21 +693,18 @@ describe('actions/Transaction', () => { let allReportNameValuePairs: OnyxCollection; await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, - waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, @@ -797,7 +784,6 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -836,7 +822,6 @@ describe('actions/Transaction', () => { await waitForBatchedUpdates(); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -887,21 +872,18 @@ describe('actions/Transaction', () => { let allReportNameValuePairs: OnyxCollection; await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, - waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, @@ -985,7 +967,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -1026,7 +1007,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -1084,21 +1064,18 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, - waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, @@ -1183,7 +1160,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { chatReport = Object.values(allReports ?? {}).find((report) => report?.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); }, @@ -1225,7 +1201,6 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (allReports) => { expenseReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.EXPENSE); }, @@ -1304,21 +1279,18 @@ describe('actions/Transaction', () => { await getOnyxData({ key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, callback: (value) => { allTransactions = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (value) => { allReports = value; }, }); await getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, - waitForCollectionCallback: true, callback: (value) => { allReportNameValuePairs = value; }, diff --git a/tests/unit/TransactionTest.ts b/tests/unit/TransactionTest.ts index 5eb40dab547a..63fe08ed4ff4 100644 --- a/tests/unit/TransactionTest.ts +++ b/tests/unit/TransactionTest.ts @@ -149,7 +149,6 @@ describe('Transaction', () => { async function loadReports() { await TestHelper.getOnyxData({ key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, callback: (value) => { reports = value; }, @@ -411,7 +410,7 @@ describe('Transaction', () => { [`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]: [duplicateViolation], [`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${siblingTransaction.transactionID}`]: [siblingDuplicateViolation, missingCategoryViolation], }, - allReports: undefined, + reports: undefined, isTrackIntentUser: false, }); await waitForBatchedUpdates(); @@ -490,7 +489,7 @@ describe('Transaction', () => { [`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${secondTransaction.transactionID}`]: [secondDuplicateViolation], [`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${siblingTransaction.transactionID}`]: [siblingDuplicateViolation, missingCategoryViolation], }, - allReports: undefined, + reports: undefined, isTrackIntentUser: false, }); await waitForBatchedUpdates(); From 34b48d236dddb5724c2c5cb2e6dc6cf55eee8859 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 4 Aug 2026 16:21:03 +0200 Subject: [PATCH 14/22] optimistic reports logic fix --- .../useChangeTransactionsReportReports.ts | 25 ++++++++++++------- .../Search/SearchTransactionsChangeReport.tsx | 3 ++- .../step/DynamicIOURequestStepUpgrade.tsx | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts index 37a293c1a856..873844f538ec 100644 --- a/src/hooks/useChangeTransactionsReportReports.ts +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -1,6 +1,8 @@ import {getIOUActionForReportID} from '@libs/ReportActionsUtils'; import {findSelfDMReportID} from '@libs/ReportUtils'; +import {isDeletedTransaction} from '@libs/TransactionUtils'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, Transaction} from '@src/types/onyx'; @@ -28,15 +30,20 @@ function useChangeTransactionsReportReports(transactionIDs: string[], allTransac if (transaction.reportID) { ids.add(transaction.reportID); } - const sources = [transaction.reportID, selfDMReportID].filter((id): id is string => !!id); - for (const source of sources) { - const iouAction = getIOUActionForReportID(source, transaction.transactionID); - if (iouAction?.childReportID) { - ids.add(iouAction.childReportID); - } - if (iouAction?.reportActionID) { - ids.add(iouAction.reportActionID); - } + if (isDeletedTransaction(transaction)) { + continue; + } + const isUnreportedExpense = !transaction.reportID || transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID; + const source = isUnreportedExpense ? selfDMReportID : transaction.reportID; + if (!source) { + continue; + } + const iouAction = getIOUActionForReportID(source, transaction.transactionID); + if (iouAction?.childReportID) { + ids.add(iouAction.childReportID); + } + if (iouAction?.reportActionID) { + ids.add(iouAction.reportActionID); } } const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index ba9528940804..d43f09040c85 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -169,6 +169,7 @@ function SearchTransactionsChangeReport() { ); const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${optimisticReport.reportID}`]; const policyTagList = policyForMovingExpenses?.id ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyForMovingExpenses.id}`] : {}; + const reportsForCall = {[`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: optimisticReport, ...reports}; setNavigationActionToMicrotaskQueue(() => { changeTransactionsReport({ transactionIDs: selectedTransactionsKeys, @@ -182,7 +183,7 @@ function SearchTransactionsChangeReport() { policyTagList, transactions, allTransactionViolation: transactionViolations, - reports, + reports: reportsForCall, isTrackIntentUser, personalPolicyOutputCurrency: personalPolicy?.outputCurrency, selfDMReportActions, diff --git a/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx b/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx index 0d761b1a1e9a..b4ce90168313 100644 --- a/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx @@ -148,6 +148,7 @@ function DynamicIOURequestStepUpgrade({ const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${optimisticReport.reportID}`]; const policyTagList = policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] : {}; + const reportsForCall = {[`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: optimisticReport, ...reports}; // Move ALL selected transactions to the new report changeTransactionsReport({ @@ -162,7 +163,7 @@ function DynamicIOURequestStepUpgrade({ policyTagList, transactions, allTransactionViolation: transactionViolations, - reports, + reports: reportsForCall, selfDMReportActions, isTrackIntentUser, // Expenses move to the upgraded workspace (newPolicy), whose currency drives any distance calculation, so the personal-policy currency is never read here. From e55291920cb2d76500d27fa33d7cefc113e9e5f5 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 4 Aug 2026 17:02:30 +0200 Subject: [PATCH 15/22] Include transactionCount/unheldNonReimbursableTotal in reportsForCall merges buildOptimisticEmptyReport doesn't set these two fields, so the freshly created report merged into reportsForCall was missing them, leaving failure-rollback unable to reset them (Onyx merge ignores undefined). --- src/pages/Search/SearchTransactionsChangeReport.tsx | 5 ++++- src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index d43f09040c85..5ea682d93b20 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -169,7 +169,10 @@ function SearchTransactionsChangeReport() { ); const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${optimisticReport.reportID}`]; const policyTagList = policyForMovingExpenses?.id ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyForMovingExpenses.id}`] : {}; - const reportsForCall = {[`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: optimisticReport, ...reports}; + const reportsForCall = { + ...reports, + [`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: {...optimisticReport, transactionCount: 0, unheldNonReimbursableTotal: 0}, + }; setNavigationActionToMicrotaskQueue(() => { changeTransactionsReport({ transactionIDs: selectedTransactionsKeys, diff --git a/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx b/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx index b4ce90168313..24bbefb84d76 100644 --- a/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestStepUpgrade.tsx @@ -148,7 +148,10 @@ function DynamicIOURequestStepUpgrade({ const reportNextStep = allReportNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${optimisticReport.reportID}`]; const policyTagList = policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] : {}; - const reportsForCall = {[`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: optimisticReport, ...reports}; + const reportsForCall = { + ...reports, + [`${ONYXKEYS.COLLECTION.REPORT}${optimisticReport.reportID}`]: {...optimisticReport, transactionCount: 0, unheldNonReimbursableTotal: 0}, + }; // Move ALL selected transactions to the new report changeTransactionsReport({ From efad694491adab97b8d425c4577860862dcb3920 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 4 Aug 2026 17:10:25 +0200 Subject: [PATCH 16/22] Document what useChangeTransactionsReportReports includes in its result --- src/hooks/useChangeTransactionsReportReports.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts index 873844f538ec..b0fd166f2035 100644 --- a/src/hooks/useChangeTransactionsReportReports.ts +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -10,7 +10,15 @@ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import useOnyx from './useOnyx'; -/** Resolve the REPORT subset that `changeTransactionsReport` looks up — keyed like the full collection so the action can do `reports?.[\`${KEY}${id}\`]`. */ +/** + * Resolve the REPORT subset that `changeTransactionsReport` looks up — keyed like the full collection so the action + * can do `reports?.[\`${KEY}${id}\`]`. Includes: + * - the self-DM report, if one exists + * - the destination report (`newReport`) + * - each transaction's current report + * - for non-deleted transactions, the old IOU action's transaction-thread report (`childReportID`) and its own + * report-action ID (the latter is used as a REPORT key fallback for that thread's `policyID` in `Transaction.ts`) + */ function useChangeTransactionsReportReports(transactionIDs: string[], allTransactions: OnyxCollection, newReport: OnyxEntry): OnyxCollection { const newReportID = newReport?.reportID; From f14710aef4b016fcd4428861f56b5371b908f600 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Tue, 4 Aug 2026 18:03:33 +0200 Subject: [PATCH 17/22] Add unit tests for useChangeTransactionsReportReports Covers source selection (reported vs unreported transaction), skipping the IOU action lookup for deleted transactions, and that the result is narrowed to only the computed ids. --- ...useChangeTransactionsReportReports.test.ts | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 tests/unit/hooks/useChangeTransactionsReportReports.test.ts diff --git a/tests/unit/hooks/useChangeTransactionsReportReports.test.ts b/tests/unit/hooks/useChangeTransactionsReportReports.test.ts new file mode 100644 index 000000000000..c745b4378877 --- /dev/null +++ b/tests/unit/hooks/useChangeTransactionsReportReports.test.ts @@ -0,0 +1,145 @@ +import {renderHook, waitFor} from '@testing-library/react-native'; + +import useChangeTransactionsReportReports from '@hooks/useChangeTransactionsReportReports'; + +import DateUtils from '@libs/DateUtils'; +import {rand64} from '@libs/NumberUtils'; + +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Report, ReportAction, Transaction} from '@src/types/onyx'; + +import Onyx from 'react-native-onyx'; + +import createRandomTransaction from '../../utils/collections/transaction'; +import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; + +const CURRENT_USER_ID = 1; +const SELF_DM_REPORT_ID = 'selfDM1'; +const OLD_REPORT_ID = 'oldReport1'; +const NEW_REPORT_ID = 'newReport1'; +const DESTINATION_REPORT_ID = 'destinationReport1'; +const UNRELATED_REPORT_ID = 'unrelatedReport1'; + +function generateTransaction(values: Partial = {}): Transaction { + return {...createRandomTransaction(1), transactionID: 'transaction1', ...values}; +} + +function generateIOUAction(transaction: Transaction, reportID: string, childReportID?: string): ReportAction { + return { + reportActionID: rand64(), + reportID, + childReportID, + actionName: CONST.REPORT.ACTIONS.TYPE.IOU, + actorAccountID: CURRENT_USER_ID, + created: DateUtils.getDBTime(), + originalMessage: { + IOUTransactionID: transaction.transactionID, + amount: transaction.amount, + currency: transaction.currency, + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + }, + }; +} + +describe('useChangeTransactionsReportReports', () => { + beforeAll(() => { + Onyx.init({keys: ONYXKEYS}); + }); + + beforeEach(async () => { + await Onyx.clear(); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${SELF_DM_REPORT_ID}`, {reportID: SELF_DM_REPORT_ID, chatType: CONST.REPORT.CHAT_TYPE.SELF_DM}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${UNRELATED_REPORT_ID}`, {reportID: UNRELATED_REPORT_ID}); + await waitForBatchedUpdates(); + }); + + it('always includes the self-DM report and the destination report, even with no transactions', async () => { + const {result} = renderHook(() => useChangeTransactionsReportReports([], {}, {reportID: DESTINATION_REPORT_ID} as Report)); + + await waitFor(() => { + expect(result.current).toBeDefined(); + }); + + expect(Object.keys(result.current ?? {})).toEqual(expect.arrayContaining([`${ONYXKEYS.COLLECTION.REPORT}${SELF_DM_REPORT_ID}`])); + expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}${UNRELATED_REPORT_ID}`]).toBeUndefined(); + }); + + it('looks up the IOU action under the transaction current report, not the self-DM report, for a reported transaction', async () => { + const transaction = generateTransaction({reportID: OLD_REPORT_ID}); + const realAction = generateIOUAction(transaction, OLD_REPORT_ID, 'realThread1'); + // Decoy: an IOU action for the SAME transactionID sitting under the self-DM report (e.g. stale data from + // before the transaction was ever reported). A source-selection regression that checks both reportID and + // selfDMReportID unconditionally would incorrectly pick this up too. + const decoyAction = generateIOUAction(transaction, SELF_DM_REPORT_ID, 'decoyThread1'); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${OLD_REPORT_ID}`, {[realAction.reportActionID]: realAction}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${SELF_DM_REPORT_ID}`, {[decoyAction.reportActionID]: decoyAction}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${OLD_REPORT_ID}`, {reportID: OLD_REPORT_ID}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}realThread1`, {reportID: 'realThread1'}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}decoyThread1`, {reportID: 'decoyThread1'}); + await waitForBatchedUpdates(); + + const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, {reportID: NEW_REPORT_ID} as Report)); + + await waitFor(() => { + expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}realThread1`]).toBeDefined(); + }); + + expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}decoyThread1`]).toBeUndefined(); + }); + + it('looks up the IOU action under the self-DM report for an unreported transaction', async () => { + const transaction = generateTransaction({reportID: CONST.REPORT.UNREPORTED_REPORT_ID}); + const trackAction = generateIOUAction(transaction, SELF_DM_REPORT_ID, 'trackThread1'); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${SELF_DM_REPORT_ID}`, {[trackAction.reportActionID]: trackAction}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}trackThread1`, {reportID: 'trackThread1'}); + await waitForBatchedUpdates(); + + const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, undefined)); + + await waitFor(() => { + expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}trackThread1`]).toBeDefined(); + }); + }); + + it('skips the IOU action lookup entirely for a deleted (trashed) transaction', async () => { + const transaction = generateTransaction({reportID: CONST.REPORT.TRASH_REPORT_ID}); + const staleAction = generateIOUAction(transaction, SELF_DM_REPORT_ID, 'staleThread1'); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${SELF_DM_REPORT_ID}`, {[staleAction.reportActionID]: staleAction}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}staleThread1`, {reportID: 'staleThread1'}); + await waitForBatchedUpdates(); + + const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, undefined)); + + await waitFor(() => { + expect(result.current).toBeDefined(); + }); + + expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}staleThread1`]).toBeUndefined(); + }); + + it('narrows the result to only the computed ids, excluding unrelated reports in the collection', async () => { + const transaction = generateTransaction({reportID: OLD_REPORT_ID}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${OLD_REPORT_ID}`, {reportID: OLD_REPORT_ID}); + await waitForBatchedUpdates(); + + const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, undefined)); + + await waitFor(() => { + expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}${OLD_REPORT_ID}`]).toBeDefined(); + }); + + expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}${UNRELATED_REPORT_ID}`]).toBeUndefined(); + }); +}); From 8a87669399802b6d48aa9495abe6811461cb077b Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Thu, 6 Aug 2026 12:17:56 +0200 Subject: [PATCH 18/22] Fix stale policyID key in old transaction thread rollback oldIOUAction.reportActionID was used as a REPORT key, which never matches a real report, so the restored policyID was always undefined. Use oldIOUAction.childReportID (the thread's own key) instead, and drop the now-unnecessary reportActionID entry from the hook's id set. Per DylanDylann's review on Expensify/App#93734. --- src/hooks/useChangeTransactionsReportReports.ts | 6 +----- src/libs/actions/Transaction.ts | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts index b0fd166f2035..8983948e5672 100644 --- a/src/hooks/useChangeTransactionsReportReports.ts +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -16,8 +16,7 @@ import useOnyx from './useOnyx'; * - the self-DM report, if one exists * - the destination report (`newReport`) * - each transaction's current report - * - for non-deleted transactions, the old IOU action's transaction-thread report (`childReportID`) and its own - * report-action ID (the latter is used as a REPORT key fallback for that thread's `policyID` in `Transaction.ts`) + * - for non-deleted transactions, the old IOU action's transaction-thread report (`childReportID`) */ function useChangeTransactionsReportReports(transactionIDs: string[], allTransactions: OnyxCollection, newReport: OnyxEntry): OnyxCollection { const newReportID = newReport?.reportID; @@ -50,9 +49,6 @@ function useChangeTransactionsReportReports(transactionIDs: string[], allTransac if (iouAction?.childReportID) { ids.add(iouAction.childReportID); } - if (iouAction?.reportActionID) { - ids.add(iouAction.reportActionID); - } } const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { selector: (allReports) => { diff --git a/src/libs/actions/Transaction.ts b/src/libs/actions/Transaction.ts index 115b4651a72a..4857b4bf977d 100644 --- a/src/libs/actions/Transaction.ts +++ b/src/libs/actions/Transaction.ts @@ -1575,7 +1575,7 @@ function getChangeTransactionsReportOnyxData({ value: { parentReportID: isUnreportedExpense ? selfDMReportID : oldReportID, parentReportActionID: oldIOUAction.reportActionID, - policyID: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldIOUAction.reportActionID}`]?.policyID, + policyID: reports?.[`${ONYXKEYS.COLLECTION.REPORT}${oldIOUAction.childReportID}`]?.policyID, }, }); } From fdc510c5d997233416d5113ef1a684e5e23cd277 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Thu, 6 Aug 2026 17:55:15 +0200 Subject: [PATCH 19/22] Fix TransactionTest.ts after merging main MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two reportNextStep tests never refreshed the reports snapshot after merging their own report data, so they asserted against stale data and threw before mockAPIWrite.mockRestore() ran — leaving the spy active and corrupting the two sibling-duplicate-violation tests that ran after them. Also fixes an allReports/reports typo in tests newly merged from main. --- tests/unit/TransactionTest.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/unit/TransactionTest.ts b/tests/unit/TransactionTest.ts index 1d9ed8393045..7317bcedb144 100644 --- a/tests/unit/TransactionTest.ts +++ b/tests/unit/TransactionTest.ts @@ -269,6 +269,7 @@ describe('Transaction', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${FAKE_OLD_REPORT_ID}`, {[oldIOUAction.reportActionID]: oldIOUAction}); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${FAKE_NEW_REPORT_ID}`, {nextStep: mockReportNextStep}); + await loadReports(); const report = await getReportFromUseOnyx(FAKE_NEW_REPORT_ID); const allTransactions = { @@ -319,6 +320,7 @@ describe('Transaction', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${FAKE_OLD_REPORT_ID}`, {[oldIOUAction.reportActionID]: oldIOUAction}); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${FAKE_SELF_DM_REPORT_ID}`, {nextStep: mockReportNextStep}); + await loadReports(); const report = await getReportFromUseOnyx(CONST.REPORT.UNREPORTED_REPORT_ID); const allTransactions = { @@ -2056,7 +2058,7 @@ describe('Transaction', () => { allTransactions, policyTagList: undefined, transactionViolations: {}, - allReports: undefined, + reports: undefined, isTrackIntentUser: false, jsonQuery: FAKE_JSON_QUERY, hash: FAKE_HASH, @@ -2100,7 +2102,7 @@ describe('Transaction', () => { allTransactions, policyTagList: undefined, transactionViolations: {}, - allReports: undefined, + reports: undefined, isTrackIntentUser: false, jsonQuery: FAKE_JSON_QUERY, hash: FAKE_HASH, @@ -2145,7 +2147,7 @@ describe('Transaction', () => { allTransactions: {}, policyTagList: undefined, transactionViolations: {}, - allReports: undefined, + reports: undefined, isTrackIntentUser: false, jsonQuery: FAKE_JSON_QUERY, hash: FAKE_HASH, @@ -2185,7 +2187,7 @@ describe('Transaction', () => { allTransactions, policyTagList: undefined, transactionViolations: {}, - allReports: undefined, + reports: undefined, isTrackIntentUser: false, jsonQuery: undefined, hash: FAKE_HASH, From 6693b04123aaeb97be1a01ccef39b6abef2cb37c Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Fri, 7 Aug 2026 10:07:47 +0200 Subject: [PATCH 20/22] Remove orphaned jsonQuery/hash tests from TransactionTest.ts The feature they tested (moving all matching expenses via jsonQuery + hash) was reverted from Transaction.ts on main in d51ada0d574, but the merge into this branch left the tests behind, referencing fields (jsonQuery, hash) that no longer exist on ChangeTransactionsReportProps. --- tests/unit/TransactionTest.ts | 177 ---------------------------------- 1 file changed, 177 deletions(-) diff --git a/tests/unit/TransactionTest.ts b/tests/unit/TransactionTest.ts index 7317bcedb144..315ceb9f401f 100644 --- a/tests/unit/TransactionTest.ts +++ b/tests/unit/TransactionTest.ts @@ -2033,183 +2033,6 @@ describe('Transaction', () => { // Rate should remain unchanged since it was already valid expect(updatedTransaction?.comment?.customUnit?.customUnitRateID).toBe(validRateID); }); - - describe('when all matching items are selected (jsonQuery + hash)', () => { - const FAKE_JSON_QUERY = 'type:expense status:all'; - const FAKE_HASH = 123456; - - it('sends the search jsonQuery and hash with an empty transaction list instead of the explicit transactions', async () => { - const mockAPIWrite = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve()); - - const transaction = generateTransaction({reportID: FAKE_OLD_REPORT_ID}); - await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); - const report = await getReportFromUseOnyx(FAKE_NEW_REPORT_ID); - const allTransactions = { - [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, - }; - - changeTransactionsReport({ - transactionIDs: [transaction.transactionID], - isASAPSubmitBetaEnabled: false, - accountID: CURRENT_USER_ID, - email: 'test@example.com', - newReport: report, - policy: undefined, - allTransactions, - policyTagList: undefined, - transactionViolations: {}, - reports: undefined, - isTrackIntentUser: false, - jsonQuery: FAKE_JSON_QUERY, - hash: FAKE_HASH, - }); - await waitForBatchedUpdates(); - - expect(mockAPIWrite).toHaveBeenCalled(); - - const parameters = mockAPIWrite.mock.calls.at(0)?.[1]; - - expect(parameters).toEqual( - expect.objectContaining({ - reportID: FAKE_NEW_REPORT_ID, - // The explicit transaction list must be dropped so the backend moves ALL matching expenses via the query. - transactionList: '', - transactionIDToReportActionAndThreadData: '{}', - jsonQuery: FAKE_JSON_QUERY, - hash: FAKE_HASH, - }), - ); - - mockAPIWrite.mockRestore(); - }); - - it('falls back to the unreported report ID when removing all matching expenses from a report', async () => { - const mockAPIWrite = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve()); - - const transaction = generateTransaction({reportID: FAKE_OLD_REPORT_ID}); - await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); - const allTransactions = { - [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, - }; - - changeTransactionsReport({ - transactionIDs: [transaction.transactionID], - isASAPSubmitBetaEnabled: false, - accountID: CURRENT_USER_ID, - email: 'test@example.com', - newReport: undefined, - policy: undefined, - allTransactions, - policyTagList: undefined, - transactionViolations: {}, - reports: undefined, - isTrackIntentUser: false, - jsonQuery: FAKE_JSON_QUERY, - hash: FAKE_HASH, - }); - await waitForBatchedUpdates(); - - expect(mockAPIWrite).toHaveBeenCalled(); - - const parameters = mockAPIWrite.mock.calls.at(0)?.[1]; - - expect(parameters).toEqual( - expect.objectContaining({ - reportID: CONST.REPORT.UNREPORTED_REPORT_ID, - transactionList: '', - jsonQuery: FAKE_JSON_QUERY, - hash: FAKE_HASH, - }), - ); - - mockAPIWrite.mockRestore(); - }); - - it('optimistically flags the destination report as pending and clears it after the request succeeds', async () => { - const destinationReport = { - ...createRandomReport(7, undefined), - ownerAccountID: CURRENT_USER_ID, - type: CONST.REPORT.TYPE.EXPENSE, - }; - const destinationReportKey = `${ONYXKEYS.COLLECTION.REPORT}${destinationReport.reportID}` as const; - - mockFetch.pause(); - try { - await Onyx.merge(destinationReportKey, destinationReport); - - changeTransactionsReport({ - transactionIDs: [], - isASAPSubmitBetaEnabled: false, - accountID: CURRENT_USER_ID, - email: 'test@example.com', - newReport: destinationReport, - policy: undefined, - allTransactions: {}, - policyTagList: undefined, - transactionViolations: {}, - reports: undefined, - isTrackIntentUser: false, - jsonQuery: FAKE_JSON_QUERY, - hash: FAKE_HASH, - }); - await waitForBatchedUpdates(); - - // While the request is in flight the destination report should be flagged as pending. - const pendingReport = await getOnyxValue(destinationReportKey); - expect(pendingReport?.pendingFields?.reportID).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE); - } finally { - await mockFetch.resume(); - } - await waitForBatchedUpdates(); - - // Once the request resolves the pending flag should be cleared. - const resolvedReport = await getOnyxValue(destinationReportKey); - expect(resolvedReport?.pendingFields?.reportID).toBeFalsy(); - }); - - it('uses the normal explicit-transaction path when a hash is passed without a jsonQuery', async () => { - const mockAPIWrite = jest.spyOn(require('@libs/API'), 'write').mockImplementation(() => Promise.resolve()); - - const transaction = generateTransaction({reportID: FAKE_OLD_REPORT_ID}); - await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction); - const report = await getReportFromUseOnyx(FAKE_NEW_REPORT_ID); - const allTransactions = { - [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction, - }; - - changeTransactionsReport({ - transactionIDs: [transaction.transactionID], - isASAPSubmitBetaEnabled: false, - accountID: CURRENT_USER_ID, - email: 'test@example.com', - newReport: report, - policy: undefined, - allTransactions, - policyTagList: undefined, - transactionViolations: {}, - reports: undefined, - isTrackIntentUser: false, - jsonQuery: undefined, - hash: FAKE_HASH, - }); - await waitForBatchedUpdates(); - - expect(mockAPIWrite).toHaveBeenCalled(); - - const parameters = mockAPIWrite.mock.calls.at(0)?.[1]; - - // Without a jsonQuery the explicit transaction list must be sent and no all-matching params leak through. - expect(parameters).toEqual( - expect.objectContaining({ - transactionList: transaction.transactionID, - }), - ); - expect(parameters).not.toHaveProperty('jsonQuery'); - expect(parameters).not.toHaveProperty('hash'); - - mockAPIWrite.mockRestore(); - }); - }); }); describe('getAllNonDeletedTransactions', () => { From 2a4d4adb50b8357103ef6b13cbbcde30f6001cbb Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Fri, 7 Aug 2026 16:02:23 +0200 Subject: [PATCH 21/22] Simplify useChangeTransactionsReportReports and cover UNREPORTED_REPORT_ID lookups Accept the resolved transactions and destination reportID directly instead of a transactionIDs/allTransactions pair, dropping the per-caller collection-building boilerplate. Also always include CONST.REPORT.UNREPORTED_REPORT_ID in the resolved report ids, matching what getChangeTransactionsReportOnyxData looks up for unreported transactions. --- src/components/AddExistingExpenseFooter.tsx | 9 +++------ src/hooks/useChangeTransactionsReportReports.ts | 16 ++++++---------- .../DynamicNewReportWorkspaceSelectionPage.tsx | 3 +-- .../Search/SearchTransactionsChangeReport.tsx | 7 +------ .../request/step/DynamicIOURequestEditReport.tsx | 9 +++------ .../hooks/useReportSelectionActions.ts | 5 +---- .../iou/request/step/IOURequestStepUpgrade.tsx | 7 +------ .../useChangeTransactionsReportReports.test.ts | 16 ++++++---------- 8 files changed, 22 insertions(+), 50 deletions(-) diff --git a/src/components/AddExistingExpenseFooter.tsx b/src/components/AddExistingExpenseFooter.tsx index ad3ce7692aff..478c2c8477f7 100644 --- a/src/components/AddExistingExpenseFooter.tsx +++ b/src/components/AddExistingExpenseFooter.tsx @@ -17,9 +17,9 @@ import {changeTransactionsReport} from '@userActions/Transaction'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Policy, PolicyCategories, Report, Transaction} from '@src/types/onyx'; +import type {Policy, PolicyCategories, Report} from '@src/types/onyx'; -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import React from 'react'; @@ -66,10 +66,7 @@ function AddExistingExpenseFooter({selectedIds, report, reportToConfirm, policy, const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector}); const [transactions] = useTransactionsByID([...selectedIds]); - const transactionsCollection: OnyxCollection = Object.fromEntries( - transactions.map((transaction) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction]), - ); - const reports = useChangeTransactionsReportReports([...selectedIds], transactionsCollection, reportToConfirm); + const reports = useChangeTransactionsReportReports(transactions, reportToConfirm?.reportID); const handleConfirm = () => { if (selectedIds.size === 0) { diff --git a/src/hooks/useChangeTransactionsReportReports.ts b/src/hooks/useChangeTransactionsReportReports.ts index 8983948e5672..1e0ad34affbe 100644 --- a/src/hooks/useChangeTransactionsReportReports.ts +++ b/src/hooks/useChangeTransactionsReportReports.ts @@ -6,22 +6,22 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, Transaction} from '@src/types/onyx'; -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; import useOnyx from './useOnyx'; /** * Resolve the REPORT subset that `changeTransactionsReport` looks up — keyed like the full collection so the action * can do `reports?.[\`${KEY}${id}\`]`. Includes: + * - the unreported-report sentinel, looked up for transactions moving out of personal space * - the self-DM report, if one exists - * - the destination report (`newReport`) + * - the destination report (`newReportID`) * - each transaction's current report * - for non-deleted transactions, the old IOU action's transaction-thread report (`childReportID`) */ -function useChangeTransactionsReportReports(transactionIDs: string[], allTransactions: OnyxCollection, newReport: OnyxEntry): OnyxCollection { - const newReportID = newReport?.reportID; - +function useChangeTransactionsReportReports(transactions: Transaction[], newReportID: string | undefined): OnyxCollection { const ids = new Set(); + ids.add(CONST.REPORT.UNREPORTED_REPORT_ID); const selfDMReportID = findSelfDMReportID(); if (selfDMReportID) { ids.add(selfDMReportID); @@ -29,11 +29,7 @@ function useChangeTransactionsReportReports(transactionIDs: string[], allTransac if (newReportID) { ids.add(newReportID); } - for (const transactionID of transactionIDs) { - const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; - if (!transaction) { - continue; - } + for (const transaction of transactions) { if (transaction.reportID) { ids.add(transaction.reportID); } diff --git a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx index afa6d769a5ca..232419504969 100644 --- a/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx +++ b/src/pages/DynamicNewReportWorkspaceSelectionPage.tsx @@ -89,13 +89,12 @@ function DynamicNewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelec const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID); const [selfDMReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(selfDMReportID)}`); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); - const transactionIDsForReports = Object.keys(selectedTransactions).length ? Object.keys(selectedTransactions) : selectedTransactionIDs; - const reports = useChangeTransactionsReportReports(transactionIDsForReports, allTransactions, undefined); const personalPolicy = usePersonalPolicy(); const selectedTransactionsKeys = Object.keys(selectedTransactions); const transactionIDs = selectedTransactionsKeys.length ? selectedTransactionsKeys : selectedTransactionIDs; const [transactions] = useTransactionsByID(transactionIDs); + const reports = useChangeTransactionsReportReports(transactions, undefined); const isAppLoadPending = useIsAppLoadPending(); const shouldShowLoadingIndicator = isAppLoadPending && !isOffline; diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index 720473a878db..9e12d7313677 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -27,8 +27,6 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES'; import type {PersonalDetails, Report, Transaction} from '@src/types/onyx'; -import type {OnyxCollection} from 'react-native-onyx'; - import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import React, {useEffect, useMemo} from 'react'; import Onyx from 'react-native-onyx'; @@ -63,10 +61,7 @@ function SearchTransactionsChangeReport() { const managedCardTransactionID = transactions.find((transaction) => isUnreportedManagedCardTransaction(transaction))?.transactionID; const hasUnreportedManagedCardTransactions = !!managedCardTransactionID; const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); - const transactionsCollection: OnyxCollection = Object.fromEntries( - transactions.map((transactionItem) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`, transactionItem]), - ); - const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, transactionsCollection, undefined); + const reports = useChangeTransactionsReportReports(transactions, undefined); const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector}); const {isBetaEnabled} = usePermissions(); const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); diff --git a/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx b/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx index d101603aff0f..df68fce4d5c9 100644 --- a/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx @@ -29,9 +29,9 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import type {PersonalDetails, Report, Transaction} from '@src/types/onyx'; +import type {PersonalDetails, Report} from '@src/types/onyx'; -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; import {isTrackIntentUserSelector} from '@selectors/Onboarding'; import React, {useMemo} from 'react'; @@ -89,10 +89,7 @@ function DynamicIOURequestEditReport({route}: DynamicIOURequestEditReportProps) const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, currentUserPersonalDetails.accountID ?? CONST.DEFAULT_NUMBER_ID, currentUserPersonalDetails.email ?? ''); const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined; const [betas] = useOnyx(ONYXKEYS.BETAS); - const transactionsCollection: OnyxCollection = Object.fromEntries( - transactions.map((transaction) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction]), - ); - const reports = useChangeTransactionsReportReports(transactionIDs, transactionsCollection, selectedReport); + const reports = useChangeTransactionsReportReports(transactions, selectedReport?.reportID); const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID); const [selfDMReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(selfDMReportID)}`); const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector}); diff --git a/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts b/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts index 009799d19ee4..6cf36ff90836 100644 --- a/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts +++ b/src/pages/iou/request/step/IOURequestStepReport/hooks/useReportSelectionActions.ts @@ -115,10 +115,7 @@ function useReportSelectionActions({ const {removeTransaction} = useSearchSelectionActions(); const {isBetaEnabled} = usePermissions(); const isNewManualExpenseFlowEnabled = isBetaEnabled(CONST.BETAS.NEW_MANUAL_EXPENSE_FLOW); - const transactionsCollection: OnyxCollection = Object.fromEntries( - transactions.map((transactionItem) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`, transactionItem]), - ); - const reports = useChangeTransactionsReportReports(transaction?.transactionID ? [transaction.transactionID] : [], transactionsCollection, undefined); + const reports = useChangeTransactionsReportReports(transaction ? [transaction] : [], undefined); const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector}); const targetTransactionIDs = transaction?.transactionID ? [transaction.transactionID] : []; diff --git a/src/pages/iou/request/step/IOURequestStepUpgrade.tsx b/src/pages/iou/request/step/IOURequestStepUpgrade.tsx index da3ce1af8022..d8d81f79e9a7 100644 --- a/src/pages/iou/request/step/IOURequestStepUpgrade.tsx +++ b/src/pages/iou/request/step/IOURequestStepUpgrade.tsx @@ -48,8 +48,6 @@ import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type {PersonalDetails, Transaction} from '@src/types/onyx'; -import type {OnyxCollection} from 'react-native-onyx'; - import {hasSeenTourSelector} from '@selectors/Onboarding'; import React, {useCallback, useMemo, useRef, useState} from 'react'; @@ -114,10 +112,7 @@ function IOURequestStepUpgrade({ const {isBetaEnabled} = usePermissions(); const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT); - const transactionsCollection: OnyxCollection = Object.fromEntries( - transactions.map((transactionItem) => [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionItem.transactionID}`, transactionItem]), - ); - const reports = useChangeTransactionsReportReports(selectedTransactionsKeys, transactionsCollection, undefined); + const reports = useChangeTransactionsReportReports(transactions, undefined); const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? ''); const ownerAccountID = selectedReport?.ownerAccountID ?? currentUserPersonalDetails.accountID; diff --git a/tests/unit/hooks/useChangeTransactionsReportReports.test.ts b/tests/unit/hooks/useChangeTransactionsReportReports.test.ts index c745b4378877..a399a0e22fe2 100644 --- a/tests/unit/hooks/useChangeTransactionsReportReports.test.ts +++ b/tests/unit/hooks/useChangeTransactionsReportReports.test.ts @@ -7,7 +7,7 @@ import {rand64} from '@libs/NumberUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report, ReportAction, Transaction} from '@src/types/onyx'; +import type {ReportAction, Transaction} from '@src/types/onyx'; import Onyx from 'react-native-onyx'; @@ -55,7 +55,7 @@ describe('useChangeTransactionsReportReports', () => { }); it('always includes the self-DM report and the destination report, even with no transactions', async () => { - const {result} = renderHook(() => useChangeTransactionsReportReports([], {}, {reportID: DESTINATION_REPORT_ID} as Report)); + const {result} = renderHook(() => useChangeTransactionsReportReports([], DESTINATION_REPORT_ID)); await waitFor(() => { expect(result.current).toBeDefined(); @@ -81,8 +81,7 @@ describe('useChangeTransactionsReportReports', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}decoyThread1`, {reportID: 'decoyThread1'}); await waitForBatchedUpdates(); - const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; - const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, {reportID: NEW_REPORT_ID} as Report)); + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction], NEW_REPORT_ID)); await waitFor(() => { expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}realThread1`]).toBeDefined(); @@ -100,8 +99,7 @@ describe('useChangeTransactionsReportReports', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}trackThread1`, {reportID: 'trackThread1'}); await waitForBatchedUpdates(); - const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; - const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, undefined)); + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction], undefined)); await waitFor(() => { expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}trackThread1`]).toBeDefined(); @@ -117,8 +115,7 @@ describe('useChangeTransactionsReportReports', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}staleThread1`, {reportID: 'staleThread1'}); await waitForBatchedUpdates(); - const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; - const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, undefined)); + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction], undefined)); await waitFor(() => { expect(result.current).toBeDefined(); @@ -133,8 +130,7 @@ describe('useChangeTransactionsReportReports', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${OLD_REPORT_ID}`, {reportID: OLD_REPORT_ID}); await waitForBatchedUpdates(); - const allTransactions = {[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction}; - const {result} = renderHook(() => useChangeTransactionsReportReports([transaction.transactionID], allTransactions, undefined)); + const {result} = renderHook(() => useChangeTransactionsReportReports([transaction], undefined)); await waitFor(() => { expect(result.current?.[`${ONYXKEYS.COLLECTION.REPORT}${OLD_REPORT_ID}`]).toBeDefined(); From cbd91c319a8106fcbc89467e3c5367647cae05a3 Mon Sep 17 00:00:00 2001 From: Wiktor Gut Date: Mon, 10 Aug 2026 13:49:06 +0200 Subject: [PATCH 22/22] Seed transactionCount/unheldNonReimbursableTotal on optimistic reports before moving expenses into them createNewReport's optimistic report leaves these two fields undefined, so changeTransactionsReport's failure rollback (which reads them off the reports snapshot) was a no-op on API failure, leaving the optimistic bump stuck. Matches the same zero-seeding already done in the analogous create-new-report flows. --- src/pages/iou/request/step/DynamicIOURequestEditReport.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx b/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx index 009a92a48f5a..4a996d7c78e1 100644 --- a/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx +++ b/src/pages/iou/request/step/DynamicIOURequestEditReport.tsx @@ -180,7 +180,7 @@ function DynamicIOURequestEditReport({route}: DynamicIOURequestEditReportProps) keyForList: optimisticReport.reportID, policyID: policyForMovingExpenses?.id, }, - optimisticReport, + {...optimisticReport, transactionCount: 0, unheldNonReimbursableTotal: 0}, ); };