From e0c0dfc0042749d4202049175a775c3ff9663644 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Sat, 14 Feb 2026 12:30:15 +0800 Subject: [PATCH 1/7] replace with connectWithoutView --- src/libs/actions/Report/DeleteReport.ts | 66 +++++++++++++++++++++++++ src/libs/actions/Report/index.ts | 42 +--------------- src/libs/actions/ReportActions.ts | 2 +- tests/actions/IOUTest.ts | 3 +- 4 files changed, 70 insertions(+), 43 deletions(-) create mode 100644 src/libs/actions/Report/DeleteReport.ts diff --git a/src/libs/actions/Report/DeleteReport.ts b/src/libs/actions/Report/DeleteReport.ts new file mode 100644 index 000000000000..60e55c2e0092 --- /dev/null +++ b/src/libs/actions/Report/DeleteReport.ts @@ -0,0 +1,66 @@ +import Onyx from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; +import Log from '@libs/Log'; +import {getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; + +// We use connectWithoutView because deleteReport doesn't affect the UI rendering +// and this avoids unnecessary re-rendering because we recursively delete the report and its children +// which requires us to subscribe to the whole report and report actions collection. +let allReports: OnyxCollection; +Onyx.connectWithoutView({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => (allReports = value), +}); + +let allReportActions: OnyxCollection; +Onyx.connectWithoutView({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (value) => (allReportActions = value), +}); + +/** Deletes a report, along with its reportActions, any linked reports, and any linked IOU report. */ +function deleteReport(reportID: string | undefined, shouldDeleteChildReports = false) { + if (!reportID) { + Log.warn('[Report] deleteReport called with no reportID'); + return; + } + const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; + const onyxData: Record = { + [`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]: null, + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]: null, + }; + + // Delete linked transactions + const reportActionsForReport = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]; + + const transactionIDs = Object.values(reportActionsForReport ?? {}) + .filter((reportAction): reportAction is ReportAction => isMoneyRequestAction(reportAction)) + .map((reportAction) => getOriginalMessage(reportAction)?.IOUTransactionID); + + for (const transactionID of [...new Set(transactionIDs)]) { + onyxData[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = null; + } + + Onyx.multiSet(onyxData); + + if (shouldDeleteChildReports) { + for (const reportAction of Object.values(reportActionsForReport ?? {})) { + if (!reportAction.childReportID) { + continue; + } + deleteReport(reportAction.childReportID, shouldDeleteChildReports); + } + } + + // Delete linked IOU report + if (report?.iouReportID) { + deleteReport(report.iouReportID, shouldDeleteChildReports); + } +} + +export default deleteReport; diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index dd0ad4dd74c8..cad7efccfe6a 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -219,6 +219,7 @@ import type {Message, ReportActions} from '@src/types/onyx/ReportAction'; import type {FileObject} from '@src/types/utils/Attachment'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import type {Dimensions} from '@src/types/utils/Layout'; +import deleteReport from './DeleteReport'; type SubscriberCallback = (isFromCurrentUser: boolean, reportAction: ReportAction | undefined) => void; @@ -3357,46 +3358,6 @@ function addPolicyReport(policyReport: OptimisticChatReport) { Navigation.dismissModalWithReport({reportID: policyReport.reportID}); } -/** Deletes a report, along with its reportActions, any linked reports, and any linked IOU report. */ -function deleteReport(reportID: string | undefined, shouldDeleteChildReports = false) { - if (!reportID) { - Log.warn('[Report] deleteReport called with no reportID'); - return; - } - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - const onyxData: Record = { - [`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]: null, - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]: null, - }; - - // Delete linked transactions - const reportActionsForReport = allReportActions?.[reportID]; - - const transactionIDs = Object.values(reportActionsForReport ?? {}) - .filter((reportAction): reportAction is ReportAction => ReportActionsUtils.isMoneyRequestAction(reportAction)) - .map((reportAction) => ReportActionsUtils.getOriginalMessage(reportAction)?.IOUTransactionID); - - for (const transactionID of [...new Set(transactionIDs)]) { - onyxData[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = null; - } - - Onyx.multiSet(onyxData); - - if (shouldDeleteChildReports) { - for (const reportAction of Object.values(reportActionsForReport ?? {})) { - if (!reportAction.childReportID) { - continue; - } - deleteReport(reportAction.childReportID, shouldDeleteChildReports); - } - } - - // Delete linked IOU report - if (report?.iouReportID) { - deleteReport(report.iouReportID, shouldDeleteChildReports); - } -} - /** * @param reportID The reportID of the policy report (workspace room) */ @@ -6640,7 +6601,6 @@ export { clearReportFieldKeyErrors, completeOnboarding, createNewReport, - deleteReport, deleteReportActionDraft, deleteReportComment, deleteReportField, diff --git a/src/libs/actions/ReportActions.ts b/src/libs/actions/ReportActions.ts index 739e87d7625c..f856a0d40589 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ReportActions.ts @@ -1,12 +1,12 @@ import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; +import deleteReport from '@libs/actions/Report/DeleteReport'; import {getLinkedTransactionID, getReportAction, getReportActionMessage, isCreatedTaskReportAction} from '@libs/ReportActionsUtils'; import {getOriginalReportID} from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; import type ReportAction from '@src/types/onyx/ReportAction'; -import {deleteReport} from './Report'; type IgnoreDirection = 'parent' | 'child'; diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 55dfdaac81cf..0fcb7b1592bb 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -56,7 +56,8 @@ import {getSendInvoiceInformation} from '@libs/actions/IOU/SendInvoice'; import {completeSplitBill, splitBill, startSplitBill, updateSplitTransactionsFromSplitExpensesFlow} from '@libs/actions/IOU/Split'; import initOnyxDerivedValues from '@libs/actions/OnyxDerived'; import {createWorkspace, deleteWorkspace, generatePolicyID, setWorkspaceApprovalMode} from '@libs/actions/Policy/Policy'; -import {addComment, createNewReport, deleteReport, notifyNewAction, openReport} from '@libs/actions/Report'; +import {addComment, createNewReport, notifyNewAction, openReport} from '@libs/actions/Report'; +import deleteReport from '@libs/actions/Report/DeleteReport'; import {clearAllRelatedReportActionErrors} from '@libs/actions/ReportActions'; import {subscribeToUserEvents} from '@libs/actions/User'; import type {ApiCommand} from '@libs/API/types'; From e9e63333a79bec491333fe544c4a43fad7b6ee48 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Sat, 14 Feb 2026 13:48:14 +0800 Subject: [PATCH 2/7] lint --- src/libs/actions/Report/DeleteReport.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report/DeleteReport.ts b/src/libs/actions/Report/DeleteReport.ts index 60e55c2e0092..2a5024e35136 100644 --- a/src/libs/actions/Report/DeleteReport.ts +++ b/src/libs/actions/Report/DeleteReport.ts @@ -1,10 +1,10 @@ import Onyx from 'react-native-onyx'; -import type {OnyxCollection} from 'react-native-onyx'; +import type { OnyxCollection } from 'react-native-onyx'; import Log from '@libs/Log'; -import {getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils'; -import CONST from '@src/CONST'; +import { getOriginalMessage, isMoneyRequestAction } from '@libs/ReportActionsUtils'; +import type CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; +import type { Report, ReportAction, ReportActions } from '@src/types/onyx'; // We use connectWithoutView because deleteReport doesn't affect the UI rendering // and this avoids unnecessary re-rendering because we recursively delete the report and its children From d092763c1535c373fa34a78389c2338c97909805 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Sat, 14 Feb 2026 13:48:48 +0800 Subject: [PATCH 3/7] lint --- src/libs/actions/ReportActions.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/ReportActions.ts b/src/libs/actions/ReportActions.ts index f856a0d40589..476a887daa52 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ReportActions.ts @@ -1,8 +1,8 @@ -import type {OnyxCollection} from 'react-native-onyx'; +import type { OnyxCollection } from 'react-native-onyx'; import Onyx from 'react-native-onyx'; -import deleteReport from '@libs/actions/Report/DeleteReport'; -import {getLinkedTransactionID, getReportAction, getReportActionMessage, isCreatedTaskReportAction} from '@libs/ReportActionsUtils'; -import {getOriginalReportID} from '@libs/ReportUtils'; +import deleteReport from './Report/DeleteReport'; +import { getLinkedTransactionID, getReportAction, getReportActionMessage, isCreatedTaskReportAction } from '@libs/ReportActionsUtils'; +import { getOriginalReportID } from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; @@ -105,7 +105,7 @@ function clearAllRelatedReportActionErrors(reportID: string | undefined, reportA } } -export type {IgnoreDirection}; +export type { IgnoreDirection }; export { // eslint-disable-next-line import/prefer-default-export clearAllRelatedReportActionErrors, From 59e41f12a603fa479b4814adc72dc380ce2da4d9 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Sat, 14 Feb 2026 14:20:42 +0800 Subject: [PATCH 4/7] prettier --- src/libs/actions/Report/DeleteReport.ts | 6 +++--- src/libs/actions/ReportActions.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/Report/DeleteReport.ts b/src/libs/actions/Report/DeleteReport.ts index 2a5024e35136..22b334189607 100644 --- a/src/libs/actions/Report/DeleteReport.ts +++ b/src/libs/actions/Report/DeleteReport.ts @@ -1,10 +1,10 @@ import Onyx from 'react-native-onyx'; -import type { OnyxCollection } from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; import Log from '@libs/Log'; -import { getOriginalMessage, isMoneyRequestAction } from '@libs/ReportActionsUtils'; +import {getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils'; import type CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type { Report, ReportAction, ReportActions } from '@src/types/onyx'; +import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; // We use connectWithoutView because deleteReport doesn't affect the UI rendering // and this avoids unnecessary re-rendering because we recursively delete the report and its children diff --git a/src/libs/actions/ReportActions.ts b/src/libs/actions/ReportActions.ts index 476a887daa52..4b2d50272ba4 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ReportActions.ts @@ -1,12 +1,12 @@ -import type { OnyxCollection } from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; -import deleteReport from './Report/DeleteReport'; -import { getLinkedTransactionID, getReportAction, getReportActionMessage, isCreatedTaskReportAction } from '@libs/ReportActionsUtils'; -import { getOriginalReportID } from '@libs/ReportUtils'; +import {getLinkedTransactionID, getReportAction, getReportActionMessage, isCreatedTaskReportAction} from '@libs/ReportActionsUtils'; +import {getOriginalReportID} from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; import type ReportAction from '@src/types/onyx/ReportAction'; +import deleteReport from './Report/DeleteReport'; type IgnoreDirection = 'parent' | 'child'; @@ -105,7 +105,7 @@ function clearAllRelatedReportActionErrors(reportID: string | undefined, reportA } } -export type { IgnoreDirection }; +export type {IgnoreDirection}; export { // eslint-disable-next-line import/prefer-default-export clearAllRelatedReportActionErrors, From e681c5e09bf38edba5496b9e25da7a558c4d6b82 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Sat, 14 Feb 2026 16:43:08 +0800 Subject: [PATCH 5/7] add test --- tests/unit/DeleteReportTest.ts | 114 +++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/unit/DeleteReportTest.ts diff --git a/tests/unit/DeleteReportTest.ts b/tests/unit/DeleteReportTest.ts new file mode 100644 index 000000000000..6a1633816d91 --- /dev/null +++ b/tests/unit/DeleteReportTest.ts @@ -0,0 +1,114 @@ +import Onyx from 'react-native-onyx'; +import deleteReport from '@libs/actions/Report/DeleteReport'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import {ReportAction} from '@src/types/onyx'; +import createRandomReportAction from '../utils/collections/reportActions'; +import {createRandomReport} from '../utils/collections/reports'; +import createRandomTransaction from '../utils/collections/transaction'; +import getOnyxValue from '../utils/getOnyxValue'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +// Mock Log to avoid noise in tests +jest.mock('@libs/Log'); + +describe('actions/Report/DeleteReport', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + }); + }); + + beforeEach(() => { + return Onyx.clear(); + }); + + it('should delete a report, its actions, and linked transactions', async () => { + // Given a report with an IOU action and a linked transaction + const reportID = '1'; + const transactionID = '123'; + const report = createRandomReport(1, undefined); + const transaction = createRandomTransaction(Number(transactionID)); + const reportAction: ReportAction = { + ...createRandomReportAction(100), + actionName: CONST.REPORT.ACTIONS.TYPE.IOU, + previousMessage: [], + message: [{text: '', html: 'iou', type: 'COMMENT'}], + originalMessage: { + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + IOUTransactionID: transaction.transactionID, + IOUDetails: { + amount: transaction.amount, + currency: transaction.currency, + comment: '', + }, + }, + }; + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { + [reportAction.reportActionID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction); + + // When we delete the report + deleteReport(reportID); + + await waitForBatchedUpdates(); + + // Then the report, its actions, and the linked transaction should be deleted + expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`)).toBeUndefined(); + expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`)).toBeUndefined(); + expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`)).toBeUndefined(); + }); + + it('should delete child reports recursively when shouldDeleteChildReports is true', async () => { + // Given a parent report with a child report + const parentReportID = '1'; + const childReportID = '2'; + const parentReport = createRandomReport(Number(parentReportID), undefined); + const childReport = createRandomReport(Number(childReportID), undefined); + const parentAction = { + ...createRandomReportAction(100), + childReportID, + }; + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`, childReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, { + [parentAction.reportActionID]: parentAction, + }); + + // When we delete the parent report with shouldDeleteChildReports set to true + deleteReport(parentReportID, true); + + await waitForBatchedUpdates(); + + // Then both the parent and child reports should be deleted + expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`)).toBeUndefined(); + expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`)).toBeUndefined(); + }); + + it('should delete linked IOU reports recursively', async () => { + // Given a report with a linked IOU report + const reportID = '1'; + const iouReportID = '2'; + const report = { + ...createRandomReport(Number(reportID), undefined), + iouReportID, + }; + const iouReport = createRandomReport(Number(iouReportID), undefined); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport); + + // When we delete the report + deleteReport(reportID); + + await waitForBatchedUpdates(); + + // Then both reports should be deleted + expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`)).toBeUndefined(); + expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`)).toBeUndefined(); + }); +}); From 37f777963ccf609a6175dcb5ec35f2c06fe6330f Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 18 Feb 2026 11:54:57 +0800 Subject: [PATCH 6/7] lint --- tests/unit/DeleteReportTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/DeleteReportTest.ts b/tests/unit/DeleteReportTest.ts index 6a1633816d91..adeeb4b275c8 100644 --- a/tests/unit/DeleteReportTest.ts +++ b/tests/unit/DeleteReportTest.ts @@ -2,7 +2,7 @@ import Onyx from 'react-native-onyx'; import deleteReport from '@libs/actions/Report/DeleteReport'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import {ReportAction} from '@src/types/onyx'; +import type {ReportAction} from '@src/types/onyx'; import createRandomReportAction from '../utils/collections/reportActions'; import {createRandomReport} from '../utils/collections/reports'; import createRandomTransaction from '../utils/collections/transaction'; From 40e0df07ad41c0a0220b8b5db15691e3fb8f1de1 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Thu, 4 Jun 2026 11:51:58 +0800 Subject: [PATCH 7/7] import it correctly --- tests/actions/IOU/RequestMoneyTest.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/actions/IOU/RequestMoneyTest.ts b/tests/actions/IOU/RequestMoneyTest.ts index 6041c90d8dbf..ce19c65f88df 100644 --- a/tests/actions/IOU/RequestMoneyTest.ts +++ b/tests/actions/IOU/RequestMoneyTest.ts @@ -6,7 +6,8 @@ import type {SearchQueryJSON, SearchStatus} from '@components/Search/types'; import {clearAllRelatedReportActionErrors} from '@libs/actions/ClearReportActionErrors'; import {requestMoney, trackExpense} from '@libs/actions/IOU/TrackExpense'; import initOnyxDerivedValues from '@libs/actions/OnyxDerived'; -import {deleteReport, notifyNewAction} from '@libs/actions/Report'; +import {notifyNewAction} from '@libs/actions/Report'; +import deleteReport from '@libs/actions/Report/DeleteReport'; import {subscribeToUserEvents} from '@libs/actions/User'; import type {ApiCommand} from '@libs/API/types'; import {WRITE_COMMANDS} from '@libs/API/types';