Skip to content

Commit

Permalink
abstract canCreateTaskInReport logic into function
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetlath committed Oct 30, 2023
1 parent 9b1e0e0 commit 481c5df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
34 changes: 30 additions & 4 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 [];
}

Expand Down Expand Up @@ -4150,6 +4174,8 @@ export {
getPolicyType,
isArchivedRoom,
isExpensifyOnlyParticipantInReport,
doExpensifyAccountsOwnPolicy,
canCreateTaskInReport,
isPolicyExpenseChatAdmin,
isPolicyAdmin,
isPublicRoom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/tasks/TaskShareDestinationSelectorModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 481c5df

Please sign in to comment.