diff --git a/src/libs/API/parameters/DeleteWorkspaceParams.ts b/src/libs/API/parameters/DeleteWorkspaceParams.ts index c535e8123d25..9683c8a4e92d 100644 --- a/src/libs/API/parameters/DeleteWorkspaceParams.ts +++ b/src/libs/API/parameters/DeleteWorkspaceParams.ts @@ -1,5 +1,6 @@ type DeleteWorkspaceParams = { policyID: string; + reportIDToOptimisticClosedReportActionID: Record; }; export default DeleteWorkspaceParams; diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index d73e2b8c4166..3d2329302b9f 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -85,7 +85,6 @@ import type { PolicyCategory, ReimbursementAccount, Report, - ReportAction, Request, TaxRatesWithDefault, Transaction, @@ -329,8 +328,12 @@ function deleteWorkspace(policyID: string, policyName: string) { ); const finallyData: OnyxUpdate[] = []; const currentTime = DateUtils.getDBTime(); + const reportIDToOptimisticClosedReportActionID: Record = {}; reportsToArchive.forEach((report) => { - const {reportID, ownerAccountID} = report ?? {}; + if (!report) { + return; + } + const {reportID, ownerAccountID} = report; optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, @@ -361,19 +364,10 @@ function deleteWorkspace(policyID: string, policyName: string) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, value: { - [optimisticClosedReportAction.reportActionID]: optimisticClosedReportAction as ReportAction, - }, - }); - - // We are temporarily adding this workaround because 'DeleteWorkspace' doesn't - // support receiving the optimistic reportActions' ids for the moment. - finallyData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, - value: { - [optimisticClosedReportAction.reportActionID]: null, + [optimisticClosedReportAction.reportActionID]: optimisticClosedReportAction, }, }); + reportIDToOptimisticClosedReportActionID[reportID] = optimisticClosedReportAction.reportActionID; }); const policy = getPolicy(policyID); @@ -394,6 +388,15 @@ function deleteWorkspace(policyID: string, policyName: string) { }, }, ]; + Object.entries(reportIDToOptimisticClosedReportActionID).forEach(([reportID, optimisticReportActionID]) => { + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + value: { + [optimisticReportActionID]: null, + }, + }); + }); reportsToArchive.forEach((report) => { const {reportID, stateNum, statusNum, oldPolicyName} = report ?? {}; @@ -411,7 +414,7 @@ function deleteWorkspace(policyID: string, policyName: string) { }); }); - const params: DeleteWorkspaceParams = {policyID}; + const params: DeleteWorkspaceParams = {policyID, reportIDToOptimisticClosedReportActionID}; API.write(WRITE_COMMANDS.DELETE_WORKSPACE, params, {optimisticData, finallyData, failureData});