diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 1e3fc5297193..02d316b3424c 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -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, @@ -4315,4 +4326,5 @@ export { parseReportRouteParams, getReimbursementQueuedActionMessage, getPersonalDetailsForAccountID, + getRoom, }; diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index 9b33ff9b086e..bf064d8bf6d8 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -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 * @@ -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, @@ -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. @@ -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', @@ -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); @@ -460,6 +529,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID) }, ...newPersonalDetailsOnyxData.optimisticData, ...membersChats.onyxOptimisticData, + ...announceRoomMembers.onyxOptimisticData, ]; const successData = [ @@ -507,6 +577,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID) }, ...newPersonalDetailsOnyxData.failureData, ...membersChats.onyxFailureData, + ...announceRoomMembers.onyxFailureData, ]; const params = {