Skip to content

Commit

Permalink
Merge pull request #42500 from Expensify/alberto-policyChat
Browse files Browse the repository at this point in the history
Correctly find Worspace Expense chats when inviting users
  • Loading branch information
puneetlath authored May 27, 2024
2 parents 13772bd + 5fe21ab commit dd454ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
20 changes: 7 additions & 13 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5245,23 +5245,17 @@ function getInvoiceChatByParticipants(policyID: string, receiverID: string | num
}

/**
* Attempts to find a report in onyx with the provided list of participants in given policy
* Attempts to find a policy expense report in onyx that is owned by ownerAccountID in a given policy
*/
function getChatByParticipantsAndPolicy(newParticipantList: number[], policyID: string): OnyxEntry<Report> {
newParticipantList.sort();
function getPolicyExpenseChat(ownerAccountID: number, policyID: string): OnyxEntry<Report> {
return (
Object.values(allReports ?? {}).find((report) => {
const participantAccountIDs = Object.keys(report?.participants ?? {});

// If the report has been deleted, or there are no participants (like an empty #admins room) then skip it
if (!report || participantAccountIDs.length === 0) {
Object.values(allReports ?? {}).find((report: OnyxEntry<Report>) => {
// If the report has been deleted, then skip it
if (!report) {
return false;
}

const sortedParticipantsAccountIDs = participantAccountIDs.map(Number).sort();

// Only return the room if it has all the participants and is not a policy room
return report.policyID === policyID && newParticipantList.every((newParticipant) => sortedParticipantsAccountIDs.includes(newParticipant));
return report.policyID === policyID && isPolicyExpenseChat(report) && report.ownerAccountID === ownerAccountID;
}) ?? null
);
}
Expand Down Expand Up @@ -6758,7 +6752,6 @@ export {
getAvailableReportFields,
getBankAccountRoute,
getChatByParticipants,
getChatByParticipantsAndPolicy,
getChatRoomSubtitle,
getChildReportNotificationPreference,
getCommentLength,
Expand Down Expand Up @@ -6790,6 +6783,7 @@ export {
getPendingChatMembers,
getPersonalDetailsForAccountID,
getPolicyDescriptionText,
getPolicyExpenseChat,
getPolicyName,
getPolicyType,
getReimbursementDeQueuedActionMessage,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ function createPolicyExpenseChats(policyID: string, invitedEmailsToAccountIDs: I
const cleanAccountID = Number(accountID);
const login = PhoneNumber.addSMSDomainIfPhoneNumber(email);

const oldChat = ReportUtils.getChatByParticipantsAndPolicy([sessionAccountID, cleanAccountID], policyID);
const oldChat = ReportUtils.getPolicyExpenseChat(cleanAccountID, policyID);

// If the chat already exists, we don't want to create a new one - just make sure it's not archived
if (oldChat) {
Expand Down

0 comments on commit dd454ba

Please sign in to comment.