Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4124,6 +4124,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 @@ -4281,4 +4292,5 @@ export {
shouldUseFullTitleToDisplay,
parseReportRouteParams,
getReimbursementQueuedActionMessage,
getRoom,
};
71 changes: 71 additions & 0 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,69 @@ function hasActiveFreePolicy(policies) {
return true;
}

/**
* 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

announceReport can be undefined when focus mode is active which will crash the app at line 287 causing #32682

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 @@ -233,6 +296,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 @@ -254,6 +319,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 @@ -305,6 +371,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 @@ -420,6 +487,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 @@ -433,6 +502,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID)
},
...newPersonalDetailsOnyxData.optimisticData,
...membersChats.onyxOptimisticData,
...announceRoomMembers.onyxOptimisticData,
];

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

const params = {
Expand Down