From 4d72e1cf9d7660c832e5e1b1584390220030eaf4 Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Mon, 27 Mar 2023 10:48:44 +0100 Subject: [PATCH 1/5] fix: check if is policy owner when creating report preview for policy expense chats --- src/libs/OptionsListUtils.js | 7 ++++++- src/libs/ReportUtils.js | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index 2b91b20462a4..a6bd68b939e5 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -494,9 +494,14 @@ function getOptions(reports, personalDetails, { reportMapForLogins[logins[0]] = report; } const isSearchingSomeonesPolicyExpenseChat = !report.isOwnPolicyExpenseChat && searchValue !== ''; + + // Checks to see if the current user is the admin of the policy, if so the policy + // name preview will be shown. + const isPolicyChatAdmin = ReportUtils.isPolicyExpenseChatAdmin(report, policies); + allReportOptions.push(createOption(logins, personalDetails, report, reportActions, { showChatPreviewLine, - forcePolicyNamePreview: isPolicyExpenseChat ? isSearchingSomeonesPolicyExpenseChat : forcePolicyNamePreview, + forcePolicyNamePreview: isPolicyExpenseChat ? isSearchingSomeonesPolicyExpenseChat || isPolicyChatAdmin : forcePolicyNamePreview, })); }); diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index bb07c7d25e34..aa52eb699e18 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -318,6 +318,27 @@ function getPolicyName(report, policies) { || Localize.translateLocal('workspace.common.unavailable'); } +/** + * Checks if the current user is the admin of the policy related to an + * admin expense chat. + * @param {Object} report + * @param {String} report.policyID + * @param {Object} policies must have Onyxkey prefix (i.e 'policy_') for keys + * @returns {Boolean} + */ +function isPolicyExpenseChatAdmin(report, policies) { + if (!isPolicyExpenseChat(report)) { + return false; + } + + const policy = policies[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; + if (!policy) { + return false; + } + + return policy.role === CONST.POLICY.ROLE.ADMIN; +} + /** * Get either the policyName or domainName the chat is tied to * @param {Object} report @@ -1666,6 +1687,7 @@ export { getPolicyName, getPolicyType, isArchivedRoom, + isPolicyExpenseChatAdmin, isPublicRoom, isConciergeChatReport, hasAutomatedExpensifyEmails, From 97cc8e5cc834e6bd4ac4654db2efe64d3deb8b8f Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Mon, 27 Mar 2023 15:09:15 +0100 Subject: [PATCH 2/5] fix: apply policy expense fix to sidebar LHN --- src/libs/SidebarUtils.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libs/SidebarUtils.js b/src/libs/SidebarUtils.js index 3e6fd500c98e..66ba573cbfcf 100644 --- a/src/libs/SidebarUtils.js +++ b/src/libs/SidebarUtils.js @@ -275,7 +275,10 @@ function getOptionData(reportID) { } if (result.isChatRoom || result.isPolicyExpenseChat) { - result.alternateText = lastMessageText || subtitle; + // Checks to see if the current user is the admin of the policy if in a policy expense chat, + // if so the policy name preview will be shown. + const isPolicyChatAdmin = result.isPolicyExpenseChat && ReportUtils.isPolicyExpenseChatAdmin(report, policies); + result.alternateText = isPolicyChatAdmin ? subtitle : lastMessageText || subtitle; } else { if (hasMultipleParticipants && !lastMessageText) { // Here we get the beginning of chat history message and append the display name for each user, adding pronouns if there are any. From c2c1a0edc50f1a70807dc542a7a0a40ad963c9d7 Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Mon, 27 Mar 2023 17:25:21 +0100 Subject: [PATCH 3/5] refactor: use lodash and fix test --- src/libs/ReportUtils.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index aa52eb699e18..3d2772460d47 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -331,12 +331,11 @@ function isPolicyExpenseChatAdmin(report, policies) { return false; } - const policy = policies[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; - if (!policy) { - return false; - } + const policyRole = lodashGet(policies, [ + `${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'role', + ]); - return policy.role === CONST.POLICY.ROLE.ADMIN; + return policyRole === CONST.POLICY.ROLE.ADMIN; } /** From 739b79135086578e69b040e5244de94d91fe7f33 Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Tue, 28 Mar 2023 08:52:56 +0100 Subject: [PATCH 4/5] refactor: add ( --- src/libs/OptionsListUtils.js | 4 +++- src/libs/SidebarUtils.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index a6bd68b939e5..a911dc5d73ae 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -501,7 +501,9 @@ function getOptions(reports, personalDetails, { allReportOptions.push(createOption(logins, personalDetails, report, reportActions, { showChatPreviewLine, - forcePolicyNamePreview: isPolicyExpenseChat ? isSearchingSomeonesPolicyExpenseChat || isPolicyChatAdmin : forcePolicyNamePreview, + forcePolicyNamePreview: isPolicyExpenseChat + ? (isSearchingSomeonesPolicyExpenseChat || isPolicyChatAdmin) + : forcePolicyNamePreview, })); }); diff --git a/src/libs/SidebarUtils.js b/src/libs/SidebarUtils.js index 66ba573cbfcf..05d4a5c9fdd7 100644 --- a/src/libs/SidebarUtils.js +++ b/src/libs/SidebarUtils.js @@ -278,7 +278,7 @@ function getOptionData(reportID) { // Checks to see if the current user is the admin of the policy if in a policy expense chat, // if so the policy name preview will be shown. const isPolicyChatAdmin = result.isPolicyExpenseChat && ReportUtils.isPolicyExpenseChatAdmin(report, policies); - result.alternateText = isPolicyChatAdmin ? subtitle : lastMessageText || subtitle; + result.alternateText = isPolicyChatAdmin ? subtitle : (lastMessageText || subtitle); } else { if (hasMultipleParticipants && !lastMessageText) { // Here we get the beginning of chat history message and append the display name for each user, adding pronouns if there are any. From 629311ed9edb700d8dd86f7958e91f19fff93578 Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Tue, 28 Mar 2023 13:56:46 +0100 Subject: [PATCH 5/5] fix: comments --- src/libs/ReportUtils.js | 5 ++--- src/libs/SidebarUtils.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 3d2772460d47..1ac6d2a63364 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -319,11 +319,10 @@ function getPolicyName(report, policies) { } /** - * Checks if the current user is the admin of the policy related to an - * admin expense chat. + * Checks if the current user is the admin of the policy given the policy expense chat. * @param {Object} report * @param {String} report.policyID - * @param {Object} policies must have Onyxkey prefix (i.e 'policy_') for keys + * @param {Object} policies must have OnyxKey prefix (i.e 'policy_') for keys * @returns {Boolean} */ function isPolicyExpenseChatAdmin(report, policies) { diff --git a/src/libs/SidebarUtils.js b/src/libs/SidebarUtils.js index 05d4a5c9fdd7..b40a35ff9b21 100644 --- a/src/libs/SidebarUtils.js +++ b/src/libs/SidebarUtils.js @@ -275,7 +275,7 @@ function getOptionData(reportID) { } if (result.isChatRoom || result.isPolicyExpenseChat) { - // Checks to see if the current user is the admin of the policy if in a policy expense chat, + // Checks to see if the current user is the admin of the policy tied to the policy expense chat, // if so the policy name preview will be shown. const isPolicyChatAdmin = result.isPolicyExpenseChat && ReportUtils.isPolicyExpenseChatAdmin(report, policies); result.alternateText = isPolicyChatAdmin ? subtitle : (lastMessageText || subtitle);