diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 746ecb7e20a4..591656b5c06a 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -3580,6 +3580,15 @@ 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} @@ -3945,6 +3954,7 @@ export { isDM, getPolicy, getPolicyExpenseChatReportIDByOwner, + getWorkspaceChats, shouldDisableSettings, shouldDisableRename, hasSingleParticipant, diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 3e5e91a74488..1a73b148e100 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -204,14 +204,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, @@ -225,6 +248,21 @@ 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 = null}) => ({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: { + stateNum, + statusNum, + hasDraft, + oldPolicyName, + }, + })), + ..._.map(optimisticClosedReportActions, (reportAction, index) => ({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChats[index].reportID}`, + value: {[reportAction.reportActionID]: null}, + })), ]; API.write( 'DeleteMembersFromWorkspace',