From 2618a3532cb3a0b5e93483dd6ed2e9c92ab4b530 Mon Sep 17 00:00:00 2001
From: Aldo Canepa <aldo@expensify.com>
Date: Tue, 26 Mar 2024 11:09:12 -0700
Subject: [PATCH 1/5] Remove optimistic CLOSED actions on success

---
 src/libs/actions/Policy.ts | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts
index 3efba3e54dcd..f107d19991a5 100644
--- a/src/libs/actions/Policy.ts
+++ b/src/libs/actions/Policy.ts
@@ -316,6 +316,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
     }
 
     const filteredPolicies = Object.values(allPolicies).filter((policy): policy is Policy => policy?.id !== policyID);
+    const successData: OnyxUpdate[] = [];
     const optimisticData: OnyxUpdate[] = [
         {
             onyxMethod: Onyx.METHOD.MERGE,
@@ -376,6 +377,11 @@ function deleteWorkspace(policyID: string, policyName: string) {
             key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
             value: optimisticReportActions,
         });
+        successData.push({
+            onyxMethod: Onyx.METHOD.MERGE,
+            key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
+            value: Object.fromEntries(Object.entries(optimisticReportActions).map(([reportActionID]) => [reportActionID, null])),
+        })
     });
 
     // Restore the old report stateNum and statusNum
@@ -406,7 +412,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
 
     const params: DeleteWorkspaceParams = {policyID};
 
-    API.write(WRITE_COMMANDS.DELETE_WORKSPACE, params, {optimisticData, failureData});
+    API.write(WRITE_COMMANDS.DELETE_WORKSPACE, params, {optimisticData, successData, failureData});
 
     // Reset the lastAccessedWorkspacePolicyID
     if (policyID === lastAccessedWorkspacePolicyID) {

From 7ecd3feff13de9387165deb5f1f9956c0fce229c Mon Sep 17 00:00:00 2001
From: Aldo Canepa <aldo@expensify.com>
Date: Tue, 26 Mar 2024 15:31:37 -0700
Subject: [PATCH 2/5] Use finallyData instead of optimisticData

---
 src/libs/actions/Policy.ts | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts
index f107d19991a5..59398e9e7c5b 100644
--- a/src/libs/actions/Policy.ts
+++ b/src/libs/actions/Policy.ts
@@ -316,7 +316,6 @@ function deleteWorkspace(policyID: string, policyName: string) {
     }
 
     const filteredPolicies = Object.values(allPolicies).filter((policy): policy is Policy => policy?.id !== policyID);
-    const successData: OnyxUpdate[] = [];
     const optimisticData: OnyxUpdate[] = [
         {
             onyxMethod: Onyx.METHOD.MERGE,
@@ -343,6 +342,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
     const reportsToArchive = Object.values(allReports ?? {}).filter(
         (report) => report?.policyID === policyID && (ReportUtils.isChatRoom(report) || ReportUtils.isPolicyExpenseChat(report) || ReportUtils.isTaskReport(report)),
     );
+    const finallyData: OnyxUpdate[] = [];
     reportsToArchive.forEach((report) => {
         const {reportID, ownerAccountID} = report ?? {};
         optimisticData.push({
@@ -370,17 +370,19 @@ function deleteWorkspace(policyID: string, policyName: string) {
             emailClosingReport = allPersonalDetails?.[ownerAccountID]?.login ?? '';
         }
         const optimisticClosedReportAction = ReportUtils.buildOptimisticClosedReportAction(emailClosingReport, policyName, CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED);
-        const optimisticReportActions: Record<string, ReportAction> = {};
-        optimisticReportActions[optimisticClosedReportAction.reportActionID] = optimisticClosedReportAction as ReportAction;
         optimisticData.push({
             onyxMethod: Onyx.METHOD.MERGE,
             key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
-            value: optimisticReportActions,
+            value: {
+                [optimisticClosedReportAction.reportActionID]: optimisticClosedReportAction as ReportAction,
+            },
         });
-        successData.push({
+        finallyData.push({
             onyxMethod: Onyx.METHOD.MERGE,
             key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
-            value: Object.fromEntries(Object.entries(optimisticReportActions).map(([reportActionID]) => [reportActionID, null])),
+            value: {
+                [optimisticClosedReportAction.reportActionID]: null,
+            },
         })
     });
 

From 5b04afe79e2914d00fb597f3449a554a80af300c Mon Sep 17 00:00:00 2001
From: Aldo Canepa <aldo@expensify.com>
Date: Tue, 26 Mar 2024 15:36:15 -0700
Subject: [PATCH 3/5] Fix variable name

---
 src/libs/actions/Policy.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts
index 59398e9e7c5b..b89254f168ff 100644
--- a/src/libs/actions/Policy.ts
+++ b/src/libs/actions/Policy.ts
@@ -414,7 +414,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
 
     const params: DeleteWorkspaceParams = {policyID};
 
-    API.write(WRITE_COMMANDS.DELETE_WORKSPACE, params, {optimisticData, successData, failureData});
+    API.write(WRITE_COMMANDS.DELETE_WORKSPACE, params, {optimisticData, finallyData, failureData});
 
     // Reset the lastAccessedWorkspacePolicyID
     if (policyID === lastAccessedWorkspacePolicyID) {

From 33915d77ebb354647223805d9b170c10cc96780d Mon Sep 17 00:00:00 2001
From: Aldo Canepa <aldo@expensify.com>
Date: Tue, 26 Mar 2024 15:42:24 -0700
Subject: [PATCH 4/5] Prettier

---
 src/libs/actions/Policy.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts
index b89254f168ff..f571d037890f 100644
--- a/src/libs/actions/Policy.ts
+++ b/src/libs/actions/Policy.ts
@@ -383,7 +383,7 @@ function deleteWorkspace(policyID: string, policyName: string) {
             value: {
                 [optimisticClosedReportAction.reportActionID]: null,
             },
-        })
+        });
     });
 
     // Restore the old report stateNum and statusNum

From 704e854ed97aa3b13c0423a278c2ded4bcbf205d Mon Sep 17 00:00:00 2001
From: Aldo Canepa <aldo@expensify.com>
Date: Tue, 26 Mar 2024 16:25:15 -0700
Subject: [PATCH 5/5] Add comment

---
 src/libs/actions/Policy.ts | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts
index f571d037890f..45fc139b6742 100644
--- a/src/libs/actions/Policy.ts
+++ b/src/libs/actions/Policy.ts
@@ -377,6 +377,9 @@ function deleteWorkspace(policyID: string, policyName: string) {
                 [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}`,
