diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index f15eb3ee001e..89fc66930869 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5244,23 +5244,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 { - newParticipantList.sort(); +function getPolicyExpenseChat(ownerAccountID: number, policyID: string): OnyxEntry { 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) => { + // 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 ); } @@ -6757,7 +6751,6 @@ export { getAvailableReportFields, getBankAccountRoute, getChatByParticipants, - getChatByParticipantsAndPolicy, getChatRoomSubtitle, getChildReportNotificationPreference, getCommentLength, @@ -6789,6 +6782,7 @@ export { getPendingChatMembers, getPersonalDetailsForAccountID, getPolicyDescriptionText, + getPolicyExpenseChat, getPolicyName, getPolicyType, getReimbursementDeQueuedActionMessage, diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 9206aa2fb0b3..533808d1ab9b 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -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) {