Skip to content

Commit

Permalink
Use isDM check instead of policy owner
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetlath committed Oct 31, 2023
1 parent d9559b8 commit 8bc920f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 39 deletions.
57 changes: 19 additions & 38 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 [];
}

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

0 comments on commit 8bc920f

Please sign in to comment.