Skip to content

Commit

Permalink
Merge pull request #30627 from dukenv0307/fix/30496
Browse files Browse the repository at this point in the history
Fix: invite and remove member when offline in workspace
  • Loading branch information
neil-marcellini authored Nov 6, 2023
2 parents dc8fdbb + 8d5241d commit b0ca383
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4156,6 +4156,17 @@ function shouldUseFullTitleToDisplay(report) {
return isMoneyRequestReport(report) || isPolicyExpenseChat(report) || isChatRoom(report) || isChatThread(report) || isTaskReport(report);
}

/**
*
* @param {String} type
* @param {String} policyID
* @returns {Object}
*/
function getRoom(type, policyID) {
const room = _.find(allReports, (report) => report && report.policyID === policyID && report.chatType === type && !isThread(report));
return room;
}

export {
getReportParticipantsTitle,
isReportMessageAttachment,
Expand Down Expand Up @@ -4315,4 +4326,5 @@ export {
parseReportRouteParams,
getReimbursementQueuedActionMessage,
getPersonalDetailsForAccountID,
getRoom,
};
71 changes: 71 additions & 0 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,69 @@ function isAdminOfFreePolicy(policies) {
return _.some(policies, (policy) => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN);
}

/**
* Build optimistic data for adding members to the announce room
* @param {String} policyID
* @param {Array} accountIDs
* @returns {Object}
*/
function buildAnnounceRoomMembersOnyxData(policyID, accountIDs) {
const announceReport = ReportUtils.getRoom(CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE, policyID);
const announceRoomMembers = {
onyxOptimisticData: [],
onyxFailureData: [],
};

announceRoomMembers.onyxOptimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${announceReport.reportID}`,
value: {
participantAccountIDs: [...announceReport.participantAccountIDs, ...accountIDs],
},
});

announceRoomMembers.onyxFailureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${announceReport.reportID}`,
value: {
participantAccountIDs: announceReport.participantAccountIDs,
},
});
return announceRoomMembers;
}

/**
* Build optimistic data for removing users from the announce room
* @param {String} policyID
* @param {Array} accountIDs
* @returns {Object}
*/
function removeOptimisticAnnounceRoomMembers(policyID, accountIDs) {
const announceReport = ReportUtils.getRoom(CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE, policyID);
const announceRoomMembers = {
onyxOptimisticData: [],
onyxFailureData: [],
};

const remainUsers = _.difference(announceReport.participantAccountIDs, accountIDs);
announceRoomMembers.onyxOptimisticData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${announceReport.reportID}`,
value: {
participantAccountIDs: [...remainUsers],
},
});

announceRoomMembers.onyxFailureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${announceReport.reportID}`,
value: {
participantAccountIDs: announceReport.participantAccountIDs,
},
});
return announceRoomMembers;
}

/**
* Remove the passed members from the policy employeeList
*
Expand All @@ -260,6 +323,8 @@ function removeMembers(accountIDs, policyID) {
ReportUtils.buildOptimisticClosedReportAction(sessionEmail, policy.name, CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY),
);

const announceRoomMembers = removeOptimisticAnnounceRoomMembers(policyID, accountIDs);

const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -281,6 +346,7 @@ function removeMembers(accountIDs, policyID) {
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChats[index].reportID}`,
value: {[reportAction.reportActionID]: reportAction},
})),
...announceRoomMembers.onyxOptimisticData,
];

// If the policy has primaryLoginsInvited, then it displays informative messages on the members page about which primary logins were added by secondary logins.
Expand Down Expand Up @@ -332,6 +398,7 @@ function removeMembers(accountIDs, policyID) {
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${workspaceChats[index].reportID}`,
value: {[reportAction.reportActionID]: null},
})),
...announceRoomMembers.onyxFailureData,
];
API.write(
'DeleteMembersFromWorkspace',
Expand Down Expand Up @@ -447,6 +514,8 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID)
const accountIDs = _.values(invitedEmailsToAccountIDs);
const newPersonalDetailsOnyxData = PersonalDetailsUtils.getNewPersonalDetailsOnyxData(logins, accountIDs);

const announceRoomMembers = buildAnnounceRoomMembersOnyxData(policyID, accountIDs);

// create onyx data for policy expense chats for each new member
const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs);

Expand All @@ -460,6 +529,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID)
},
...newPersonalDetailsOnyxData.optimisticData,
...membersChats.onyxOptimisticData,
...announceRoomMembers.onyxOptimisticData,
];

const successData = [
Expand Down Expand Up @@ -507,6 +577,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID)
},
...newPersonalDetailsOnyxData.failureData,
...membersChats.onyxFailureData,
...announceRoomMembers.onyxFailureData,
];

const params = {
Expand Down

0 comments on commit b0ca383

Please sign in to comment.