From 8bc920faa72cb7cc0ceb8bb974331fdaee97dc37 Mon Sep 17 00:00:00 2001
From: Puneet Lath <puneet@expensify.com>
Date: Tue, 31 Oct 2023 14:07:44 -0400
Subject: [PATCH] Use isDM check instead of policy owner

---
 src/libs/ReportUtils.js                       | 57 +++++++------------
 .../AttachmentPickerWithMenuItems.js          |  1 -
 2 files changed, 19 insertions(+), 39 deletions(-)

diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js
index 65fe0b0846c2..27200ff4347c 100644
--- a/src/libs/ReportUtils.js
+++ b/src/libs/ReportUtils.js
@@ -475,6 +475,16 @@ function isChatThread(report) {
     return isThread(report) && report.type === CONST.REPORT.TYPE.CHAT;
 }
 
+/**
+ * Returns true if report is a DM/Group DM chat.
+ *
+ * @param {Object} report
+ * @returns {Boolean}
+ */
+function isDM(report) {
+    return !getChatType(report);
+}
+
 /**
  * Only returns true if this is our main 1:1 DM report with Concierge
  *
@@ -504,37 +514,17 @@ function shouldDisableDetailPage(report) {
     return false;
 }
 
-/**
- * Returns true if this report has only one participant and it's an Expensify account.
- * @param {Object} report
- * @returns {Boolean}
- */
-function isExpensifyOnlyParticipantInReport(report) {
-    const reportParticipants = _.without(lodashGet(report, 'participantAccountIDs', []), currentUserAccountID);
-    return reportParticipants.length === 1 && _.some(reportParticipants, (accountID) => _.contains(CONST.EXPENSIFY_ACCOUNT_IDS, accountID));
-}
-
-/**
- * Checks if the policy that the report is on is owned by one of the special Expensify accounts
- *
- * @param {Object} report
- * @returns {Boolean}
- */
-function doExpensifyAccountsOwnPolicy(report) {
-    const policyID = lodashGet(report, 'policyID', '');
-    const policyOwnerAccountID = lodashGet(allPolicies, [`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, 'ownerAccountID'], 0);
-    return CONST.EXPENSIFY_ACCOUNT_IDS.includes(policyOwnerAccountID);
-}
-
 /**
  * Returns whether a given report can have tasks created in it.
+ * We only prevent the task option if it's a DM/group-DM and the other users are all special Expensify accounts
+ *
  * @param {Object} report
  * @returns {Boolean}
  */
 function canCreateTaskInReport(report) {
-    // Tasks cannot be created in DMs with special Expensify accounts but they can be created in policy rooms that are owned by them.
-    // So we check if Expensify accounts are the only other participant and also whether or not we are in a room owned by them.
-    if (isExpensifyOnlyParticipantInReport(report) && !doExpensifyAccountsOwnPolicy(report)) {
+    const otherReportParticipants = _.without(lodashGet(report, 'participantAccountIDs', []), currentUserAccountID);
+    const areExpensifyAccountsOnlyOtherParticipants = _.every(otherReportParticipants, (accountID) => _.contains(CONST.EXPENSIFY_ACCOUNT_IDS, accountID));
+    if (areExpensifyAccountsOnlyOtherParticipants && isDM(report)) {
         return false;
     }
 
@@ -673,16 +663,6 @@ function isPolicyAdmin(policyID, policies) {
     return policyRole === CONST.POLICY.ROLE.ADMIN;
 }
 
-/**
- * Returns true if report is a DM/Group DM chat.
- *
- * @param {Object} report
- * @returns {Boolean}
- */
-function isDM(report) {
-    return !getChatType(report);
-}
-
 /**
  * Returns true if report has a single participant.
  *
@@ -3679,7 +3659,10 @@ function getMoneyRequestOptions(report, reportParticipants) {
 
     // We don't allow IOU actions if an Expensify account is a participant of the report, unless the policy that the report is on is owned by an Expensify account
     const doParticipantsIncludeExpensifyAccounts = lodashIntersection(reportParticipants, CONST.EXPENSIFY_ACCOUNT_IDS).length > 0;
-    if (doParticipantsIncludeExpensifyAccounts && !doExpensifyAccountsOwnPolicy(report)) {
+    const policyID = lodashGet(report, 'policyID', '');
+    const policyOwnerAccountID = lodashGet(allPolicies, [`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, 'ownerAccountID'], 0);
+    const isPolicyOwnedByExpensifyAccounts = CONST.EXPENSIFY_ACCOUNT_IDS.includes(policyOwnerAccountID);
+    if (doParticipantsIncludeExpensifyAccounts && !isPolicyOwnedByExpensifyAccounts) {
         return [];
     }
 
@@ -4173,8 +4156,6 @@ export {
     getPolicyName,
     getPolicyType,
     isArchivedRoom,
-    isExpensifyOnlyParticipantInReport,
-    doExpensifyAccountsOwnPolicy,
     canCreateTaskInReport,
     isPolicyExpenseChatAdmin,
     isPolicyAdmin,
diff --git a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js
index e2266de95880..a31e718933ea 100644
--- a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js
+++ b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js
@@ -151,7 +151,6 @@ function AttachmentPickerWithMenuItems({
      * @returns {Boolean}
      */
     const taskOption = useMemo(() => {
-        // We only prevent the task option from showing if it's a DM and the other user is an Expensify default email
         if (!Permissions.canUseTasks(betas) || !ReportUtils.canCreateTaskInReport(report)) {
             return [];
         }