From 311870d8ff4e4a9e9e19fa7a3dafb74301ca2a8c Mon Sep 17 00:00:00 2001 From: Tsaqif Date: Fri, 6 Oct 2023 15:15:06 +0700 Subject: [PATCH 1/3] add optimistic data to archive workspace chats when remove members of policies Signed-off-by: Tsaqif --- src/libs/ReportUtils.js | 14 ++++++++++++++ src/libs/actions/Policy.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 7d3e1a2b7098..113aac066515 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -3539,6 +3539,19 @@ function getPolicyExpenseChatReportIDByOwner(policyOwner) { return expenseChat.reportID; } +/** + * @param {String} policyID + * @param {Array} accountIDs + * @returns {Array} + */ +function getWorkspaceChats(policyID, accountIDs) { + return _.filter(allReports, (report) => + isPolicyExpenseChat(report) && + lodashGet(report, 'policyID', '') === policyID && + _.contains(accountIDs, lodashGet(report, 'ownerAccountID', '')) + ) +} + /* * @param {Object|null} report * @returns {Boolean} @@ -3895,6 +3908,7 @@ export { isDM, getPolicy, getPolicyExpenseChatReportIDByOwner, + getWorkspaceChats, shouldDisableSettings, shouldDisableRename, hasSingleParticipant, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index fcce909c5582..b6fd208a2052 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -196,14 +196,37 @@ function removeMembers(accountIDs, policyID) { if (accountIDs.length === 0) { return; } + const membersListKey = `${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policyID}`; + const policy = ReportUtils.getPolicy(policyID); + const workspaceChats = ReportUtils.getWorkspaceChats(policyID, accountIDs); + const optimisticClosedReportActions = _.map(workspaceChats, () => + ReportUtils.buildOptimisticClosedReportAction(sessionEmail, policy.name, CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY)); + const optimisticData = [ { onyxMethod: Onyx.METHOD.MERGE, key: membersListKey, value: _.object(accountIDs, Array(accountIDs.length).fill({pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE})), }, + ..._.map(workspaceChats, (report) => ({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, + value: { + statusNum: CONST.REPORT.STATUS.CLOSED, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + oldPolicyName: policy.name, + hasDraft: false, + } + })), + ..._.map(optimisticClosedReportActions, (reportAction, index) => ({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChats[index].reportID}`, + value: {[reportAction.reportActionID]: reportAction}, + } + )), ]; + const successData = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -217,6 +240,16 @@ function removeMembers(accountIDs, policyID) { key: membersListKey, value: _.object(accountIDs, Array(accountIDs.length).fill({errors: ErrorUtils.getMicroSecondOnyxError('workspace.people.error.genericRemove')})), }, + ..._.map(workspaceChats, ({reportID, stateNum, statusNum, hasDraft, oldPolicyName}) => ({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: { + stateNum, + statusNum, + hasDraft, + oldPolicyName, + }, + })), ]; API.write( 'DeleteMembersFromWorkspace', From 328e1df14ce6c499425353207acacff6869f1929 Mon Sep 17 00:00:00 2001 From: Tsaqif Date: Fri, 6 Oct 2023 15:54:30 +0700 Subject: [PATCH 2/3] run prettier Signed-off-by: Tsaqif --- src/libs/ReportUtils.js | 6 +----- src/libs/actions/Policy.js | 14 +++++++------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 113aac066515..eda70845a9f2 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -3545,11 +3545,7 @@ function getPolicyExpenseChatReportIDByOwner(policyOwner) { * @returns {Array} */ function getWorkspaceChats(policyID, accountIDs) { - return _.filter(allReports, (report) => - isPolicyExpenseChat(report) && - lodashGet(report, 'policyID', '') === policyID && - _.contains(accountIDs, lodashGet(report, 'ownerAccountID', '')) - ) + return _.filter(allReports, (report) => isPolicyExpenseChat(report) && lodashGet(report, 'policyID', '') === policyID && _.contains(accountIDs, lodashGet(report, 'ownerAccountID', ''))); } /* diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index b6fd208a2052..8fd0ff73d334 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -201,7 +201,8 @@ function removeMembers(accountIDs, policyID) { const policy = ReportUtils.getPolicy(policyID); const workspaceChats = ReportUtils.getWorkspaceChats(policyID, accountIDs); const optimisticClosedReportActions = _.map(workspaceChats, () => - ReportUtils.buildOptimisticClosedReportAction(sessionEmail, policy.name, CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY)); + ReportUtils.buildOptimisticClosedReportAction(sessionEmail, policy.name, CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY), + ); const optimisticData = [ { @@ -217,14 +218,13 @@ function removeMembers(accountIDs, policyID) { stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, oldPolicyName: policy.name, hasDraft: false, - } + }, })), ..._.map(optimisticClosedReportActions, (reportAction, index) => ({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChats[index].reportID}`, - value: {[reportAction.reportActionID]: reportAction}, - } - )), + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChats[index].reportID}`, + value: {[reportAction.reportActionID]: reportAction}, + })), ]; const successData = [ From a88ac00f90c8dd6c8ee7648c22ca066825fd0e11 Mon Sep 17 00:00:00 2001 From: Tsaqif Date: Mon, 9 Oct 2023 20:34:33 +0700 Subject: [PATCH 3/3] add failure data for report actions and add default value for oldPolicy failure data Signed-off-by: Tsaqif --- src/libs/actions/Policy.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 8fd0ff73d334..6a54af6849c2 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -240,7 +240,7 @@ function removeMembers(accountIDs, policyID) { key: membersListKey, value: _.object(accountIDs, Array(accountIDs.length).fill({errors: ErrorUtils.getMicroSecondOnyxError('workspace.people.error.genericRemove')})), }, - ..._.map(workspaceChats, ({reportID, stateNum, statusNum, hasDraft, oldPolicyName}) => ({ + ..._.map(workspaceChats, ({reportID, stateNum, statusNum, hasDraft, oldPolicyName = null}) => ({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { @@ -250,6 +250,11 @@ function removeMembers(accountIDs, policyID) { oldPolicyName, }, })), + ..._.map(optimisticClosedReportActions, (reportAction, index) => ({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChats[index].reportID}`, + value: {[reportAction.reportActionID]: null}, + })), ]; API.write( 'DeleteMembersFromWorkspace',