From 481c5df19e6e36a7d7996bc65bd826d3260ed9d1 Mon Sep 17 00:00:00 2001 From: Puneet Lath Date: Mon, 30 Oct 2023 17:40:32 -0400 Subject: [PATCH] abstract canCreateTaskInReport logic into function --- src/libs/ReportUtils.js | 34 ++++++++++++++++--- .../AttachmentPickerWithMenuItems.js | 2 +- .../TaskShareDestinationSelectorModal.js | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index dbfa70943633..9be5ca65f5dc 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -514,6 +514,33 @@ function isExpensifyOnlyParticipantInReport(report) { 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. + * @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)) { + return false; + } + + return true; +} + /** * Returns true if there are any Expensify accounts (i.e. with domain 'expensify.com') in the set of accountIDs * by cross-referencing the accountIDs with personalDetails. @@ -3652,10 +3679,7 @@ 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; - const policyID = lodashGet(report, 'policyID', ''); - const policyOwnerAccountID = lodashGet(allPolicies, `${ONYXKEYS.COLLECTION.POLICY}${policyID}.ownerAccountID`, 0); - const doExpensifyAccountsOwnPolicy = CONST.EXPENSIFY_ACCOUNT_IDS.includes(policyOwnerAccountID); - if (doParticipantsIncludeExpensifyAccounts && !doExpensifyAccountsOwnPolicy) { + if (doParticipantsIncludeExpensifyAccounts && !doExpensifyAccountsOwnPolicy(report)) { return []; } @@ -4150,6 +4174,8 @@ export { getPolicyType, isArchivedRoom, isExpensifyOnlyParticipantInReport, + doExpensifyAccountsOwnPolicy, + canCreateTaskInReport, isPolicyExpenseChatAdmin, isPolicyAdmin, isPublicRoom, diff --git a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js index f29b727f0008..33316e657f02 100644 --- a/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js +++ b/src/pages/home/report/ReportActionCompose/AttachmentPickerWithMenuItems.js @@ -152,7 +152,7 @@ function AttachmentPickerWithMenuItems({ */ 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.isPolicyExpenseChat(report) && ReportUtils.isExpensifyOnlyParticipantInReport(report))) { + if (!Permissions.canUseTasks(betas) || (!ReportUtils.canCreateTaskInReport(report))) { return []; } diff --git a/src/pages/tasks/TaskShareDestinationSelectorModal.js b/src/pages/tasks/TaskShareDestinationSelectorModal.js index fde02c2a4108..0765f435e3ca 100644 --- a/src/pages/tasks/TaskShareDestinationSelectorModal.js +++ b/src/pages/tasks/TaskShareDestinationSelectorModal.js @@ -53,7 +53,7 @@ function TaskShareDestinationSelectorModal(props) { _.keys(props.reports).forEach((reportKey) => { if ( ReportUtils.shouldDisableWriteActions(props.reports[reportKey]) || - ReportUtils.isExpensifyOnlyParticipantInReport(props.reports[reportKey]) || + !ReportUtils.canCreateTaskInReport(props.reports[reportKey]) || ReportUtils.isCanceledTaskReport(props.reports[reportKey]) ) { return;