Skip to content

Commit

Permalink
Merge pull request #28987 from tsa321/archwchats
Browse files Browse the repository at this point in the history
Add optimistic data to archive workspace chats when remove members
  • Loading branch information
mountiny authored Oct 9, 2023
2 parents 93338e3 + a88ac00 commit 38f1516
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -3945,6 +3954,7 @@ export {
isDM,
getPolicy,
getPolicyExpenseChatReportIDByOwner,
getWorkspaceChats,
shouldDisableSettings,
shouldDisableRename,
hasSingleParticipant,
Expand Down
38 changes: 38 additions & 0 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit 38f1516

Please sign in to comment.