diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index 618503e19c33..29c480dcce7d 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -40,6 +40,7 @@ import * as Link from '@userActions/Link'; import * as Transaction from '@userActions/Transaction'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; +import * as Report from '@src/libs/actions/Report'; import * as ReportActions from '@src/libs/actions/ReportActions'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; @@ -115,6 +116,9 @@ function MoneyRequestView({ const {isOffline} = useNetwork(); const {translate, toLocaleDigit} = useLocalize(); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); + const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${parentReport?.parentReportID}`, { + selector: (chatReportValue) => chatReportValue && {reportID: chatReportValue.reportID, errorFields: chatReportValue.errorFields}, + }); const parentReportAction = parentReportActions?.[report.parentReportActionID ?? '-1']; const isTrackExpense = ReportUtils.isTrackExpenseReport(report); @@ -403,11 +407,14 @@ function MoneyRequestView({ if (!transaction?.transactionID) { return; } - if ( - transaction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD && - Object.values(transaction?.errors ?? {})?.find((error) => ErrorUtils.isReceiptError(error)) - ) { - deleteTransaction(parentReport, parentReportAction); + if (transaction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) { + if (chatReport?.reportID && ReportUtils.getAddWorkspaceRoomOrChatReportErrors(chatReport)) { + Report.navigateToConciergeChatAndDeleteReport(chatReport.reportID, true, true); + return; + } + if (Object.values(transaction?.errors ?? {})?.find((error) => ErrorUtils.isReceiptError(error))) { + deleteTransaction(parentReport, parentReportAction); + } } Transaction.clearError(transaction.transactionID); ReportActions.clearAllRelatedReportActionErrors(report.reportID, parentReportAction); diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 89de7d1e55ec..473cd47b9939 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -581,7 +581,7 @@ function getAllReportTransactions(reportID?: string, transactions?: OnyxCollecti // `reportID` from the `/CreateDistanceRequest` endpoint return's number instead of string for created `transaction`. // For reference, https://github.com/Expensify/App/pull/26536#issuecomment-1703573277. // We will update this in a follow-up Issue. According to this comment: https://github.com/Expensify/App/pull/26536#issuecomment-1703591019. - const nonNullableTransactions: Transaction[] = Object.values(transactions ?? allTransactions ?? {}).filter((transaction): transaction is Transaction => transaction !== null); + const nonNullableTransactions: Transaction[] = Object.values(transactions ?? allTransactions ?? {}).filter((transaction): transaction is Transaction => !!transaction); return nonNullableTransactions.filter((transaction) => `${transaction.reportID}` === `${reportID}`); } diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 026ce45146d3..0b603416b72d 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -2145,7 +2145,7 @@ function addPolicyReport(policyReport: ReportUtils.OptimisticChatReport) { } /** Deletes a report, along with its reportActions, any linked reports, and any linked IOU report. */ -function deleteReport(reportID: string) { +function deleteReport(reportID: string, shouldDeleteChildReports = false) { const report = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; const onyxData: Record = { [`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]: null, @@ -2165,20 +2165,32 @@ function deleteReport(reportID: string) { Onyx.multiSet(onyxData); + if (shouldDeleteChildReports) { + Object.values(reportActionsForReport ?? {}).forEach((reportAction) => { + if (!reportAction.childReportID) { + return; + } + deleteReport(reportAction.childReportID, shouldDeleteChildReports); + }); + } + // Delete linked IOU report if (report?.iouReportID) { - deleteReport(report.iouReportID); + deleteReport(report.iouReportID, shouldDeleteChildReports); } } /** * @param reportID The reportID of the policy report (workspace room) */ -function navigateToConciergeChatAndDeleteReport(reportID: string) { +function navigateToConciergeChatAndDeleteReport(reportID: string, shouldPopToTop = false, shouldDeleteChildReports = false) { // Dismiss the current report screen and replace it with Concierge Chat - Navigation.goBack(); + if (shouldPopToTop) { + Navigation.setShouldPopAllStateOnUP(true); + } + Navigation.goBack(undefined, undefined, shouldPopToTop); navigateToConciergeChat(); - deleteReport(reportID); + deleteReport(reportID, shouldDeleteChildReports); } /**