From 19d53bb7a25dd12f3bfe15552aefbc17ce54b0c7 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Wed, 4 Feb 2026 20:00:58 +0530 Subject: [PATCH] Refactor: isolate deleteAppReport from Onyx.connect ONYXKEYS.COLLECTION.REPORT --- src/components/MoneyReportHeader.tsx | 4 ++- src/libs/actions/Report/index.ts | 36 +++++++++---------- .../PopoverReportActionContextMenu.tsx | 9 ++++- tests/actions/ReportTest.ts | 14 ++++++-- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/components/MoneyReportHeader.tsx b/src/components/MoneyReportHeader.tsx index e0f7270c6dfbd..7ef015d2d323c 100644 --- a/src/components/MoneyReportHeader.tsx +++ b/src/components/MoneyReportHeader.tsx @@ -220,6 +220,8 @@ function MoneyReportHeader({ const activePolicy = usePolicy(activePolicyID); const [integrationsExportTemplates] = useOnyx(ONYXKEYS.NVP_INTEGRATION_SERVER_EXPORT_TEMPLATES, {canBeMissing: true}); const [csvExportLayouts] = useOnyx(ONYXKEYS.NVP_CSV_EXPORT_LAYOUTS, {canBeMissing: true}); + const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID, {canBeMissing: true}); + const [selfDMReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`, {canBeMissing: true}); const expensifyIcons = useMemoizedLazyExpensifyIcons([ 'Buildings', 'Plus', @@ -1501,7 +1503,7 @@ function MoneyReportHeader({ Navigation.goBack(backToRoute); // eslint-disable-next-line @typescript-eslint/no-deprecated InteractionManager.runAfterInteractions(() => { - deleteAppReport(moneyRequestReport?.reportID, email ?? '', accountID, reportTransactions, allTransactionViolations, bankAccountList); + deleteAppReport(moneyRequestReport, selfDMReport, email ?? '', accountID, reportTransactions, allTransactionViolations, bankAccountList); }); }); }, diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index 6e8ee547568ec..0a34762351617 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -5084,17 +5084,19 @@ function clearDeleteTransactionNavigateBackUrl() { /** Deletes a report and un-reports all transactions on the report along with its reportActions, any linked reports and any linked IOU report actions. */ function deleteAppReport( - reportID: string | undefined, + report: OnyxEntry, + selfDMReport: OnyxEntry, currentUserEmailParam: string, currentUserAccountIDParam: number, reportTransactions: Record, allTransactionViolations: OnyxCollection, bankAccountList: OnyxEntry, ) { - if (!reportID) { - Log.warn('[Report] deleteReport called with no reportID'); + if (!report?.reportID) { + Log.warn('[Report] deleteAppReport called with no reportID'); return; } + const reportID = report.reportID; const optimisticData: Array< OnyxUpdate< | typeof ONYXKEYS.COLLECTION.REPORT @@ -5110,25 +5112,22 @@ function deleteAppReport( OnyxUpdate > = []; - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - - let selfDMReportID = findSelfDMReportID(); - let selfDMReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`]; + let selfDMReportID = selfDMReport?.reportID; let createdAction: ReportAction; let selfDMParameters: SelfDMParameters = {}; - if (!selfDMReport) { + if (!selfDMReportID) { const currentTime = DateUtils.getDBTime(); - selfDMReport = buildOptimisticSelfDMReport(currentTime); - selfDMReportID = selfDMReport.reportID; + const optimisticSelfDMReport = buildOptimisticSelfDMReport(currentTime); + selfDMReportID = optimisticSelfDMReport.reportID; createdAction = buildOptimisticCreatedReportAction(currentUserEmailParam ?? '', currentTime); - selfDMParameters = {reportID: selfDMReport.reportID, createdReportActionID: createdAction.reportActionID}; + selfDMParameters = {reportID: optimisticSelfDMReport.reportID, createdReportActionID: createdAction.reportActionID}; optimisticData.push( { onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`, value: { - ...selfDMReport, + ...optimisticSelfDMReport, pendingFields: { createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, @@ -5141,14 +5140,14 @@ function deleteAppReport( }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${selfDMReport.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${selfDMReportID}`, value: { isOptimisticReport: true, }, }, { onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReportID}`, value: { [createdAction.reportActionID]: createdAction, }, @@ -5158,7 +5157,7 @@ function deleteAppReport( successData.push( { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`, value: { pendingFields: { createChat: null, @@ -5167,14 +5166,14 @@ function deleteAppReport( }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${selfDMReport.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${selfDMReportID}`, value: { isOptimisticReport: false, }, }, { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReportID}`, value: { [createdAction.reportActionID]: { pendingAction: null, @@ -5383,7 +5382,6 @@ function deleteAppReport( value: null, }); - // @ts-expect-error - will be solved in https://github.com/Expensify/App/issues/73830 failureData.push({ onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, diff --git a/src/pages/inbox/report/ContextMenu/PopoverReportActionContextMenu.tsx b/src/pages/inbox/report/ContextMenu/PopoverReportActionContextMenu.tsx index b317d8900b0c6..812bbb8d37188 100644 --- a/src/pages/inbox/report/ContextMenu/PopoverReportActionContextMenu.tsx +++ b/src/pages/inbox/report/ContextMenu/PopoverReportActionContextMenu.tsx @@ -323,6 +323,11 @@ function PopoverReportActionContextMenu({ref}: PopoverReportActionContextMenuPro const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportIDRef.current}`, { canBeMissing: true, }); + const [childReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportActionRef.current?.childReportID}`, { + canBeMissing: true, + }); + const [selfDMReportID] = useOnyx(ONYXKEYS.SELF_DM_REPORT_ID, {canBeMissing: true}); + const [selfDMReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`, {canBeMissing: true}); const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`, {canBeMissing: true}); const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true}); const {currentSearchHash} = useSearchContext(); @@ -369,7 +374,7 @@ function PopoverReportActionContextMenu({ref}: PopoverReportActionContextMenuPro deleteTransactions([originalMessage.IOUTransactionID], duplicateTransactions, duplicateTransactionViolations, currentSearchHash); } } else if (isReportPreviewAction(reportAction)) { - deleteAppReport(reportAction.childReportID, email ?? '', currentUserAccountID, reportTransactions, allTransactionViolations, bankAccountList); + deleteAppReport(childReport, selfDMReport, email ?? '', currentUserAccountID, reportTransactions, allTransactionViolations, bankAccountList); } else if (reportAction) { // eslint-disable-next-line @typescript-eslint/no-deprecated InteractionManager.runAfterInteractions(() => { @@ -381,6 +386,8 @@ function PopoverReportActionContextMenu({ref}: PopoverReportActionContextMenuPro setIsDeleteCommentConfirmModalVisible(false); }, [ report, + childReport, + selfDMReport, iouReport, chatReport, duplicateTransactions, diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index 0e6132a8a9ad5..469346cf8cedb 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -2306,6 +2306,15 @@ describe('actions/Report', () => { it('should only moves CREATE or TRACK type of IOU action to self DM', async () => { // Given an expense report with CREATE, TRACK, and PAY of IOU actions const reportID = '1'; + const expenseReport: OnyxTypes.Report = { + ...createRandomReport(1, undefined), + type: CONST.REPORT.TYPE.EXPENSE, + managerID: currentUserAccountID, + ownerAccountID: currentUserAccountID, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + }; + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, expenseReport); const firstIOUAction: OnyxTypes.ReportAction = { reportActionID: '1', actionName: CONST.REPORT.ACTIONS.TYPE.IOU, @@ -2346,7 +2355,7 @@ describe('actions/Report', () => { }); // When deleting the expense report - Report.deleteAppReport(reportID, '', currentUserAccountID, {}, {}, {}); + Report.deleteAppReport(expenseReport, undefined, '', currentUserAccountID, {}, {}, {}); await waitForBatchedUpdates(); // Then only the IOU action with type of CREATE and TRACK is moved to the self DM @@ -2442,7 +2451,8 @@ describe('actions/Report', () => { // When deleting the first expense report Report.deleteAppReport( - expenseReport1.reportID, + expenseReport1, + undefined, '', currentUserAccountID, {