Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/libs/ReportSecondaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {ExportTemplate, Policy, Report, ReportAction, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx';
import {isApprover as isApproverUtils} from './actions/Policy/Member';
import {getCurrentUserAccountID, getCurrentUserEmail} from './actions/Report';
import {getLoginByAccountID} from './PersonalDetailsUtils';
import {
arePaymentsEnabled as arePaymentsEnabledUtils,
getConnectedIntegration,
Expand All @@ -25,6 +26,7 @@ import {
canEditReportPolicy,
canHoldUnholdReportAction,
canRejectReportAction,
doesReportContainRequestsFromMultipleUsers,
getTransactionDetails,
hasOnlyHeldExpenses,
hasOnlyNonReimbursableTransactions,
Expand Down Expand Up @@ -434,7 +436,13 @@ function isHoldActionForTransaction(report: Report, reportTransaction: Transacti
}

function isChangeWorkspaceAction(report: Report, policies: OnyxCollection<Policy>, reportActions?: ReportAction[]): boolean {
const availablePolicies = Object.values(policies ?? {}).filter((newPolicy) => isWorkspaceEligibleForReportChange(newPolicy, report, policies));
// We can't move the iou report to the workspace if both users from the iou report create the expense
if (isIOUReportUtils(report) && doesReportContainRequestsFromMultipleUsers(report)) {
return false;
}

const submitterEmail = getLoginByAccountID(report?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID);
const availablePolicies = Object.values(policies ?? {}).filter((newPolicy) => isWorkspaceEligibleForReportChange(submitterEmail, newPolicy));
let hasAvailablePolicies = availablePolicies.length > 1;
if (!hasAvailablePolicies && availablePolicies.length === 1) {
hasAvailablePolicies = !report.policyID || report.policyID !== availablePolicies?.at(0)?.id;
Expand Down
41 changes: 4 additions & 37 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ import {
getAccountIDsByLogins,
getDisplayNameOrDefault,
getEffectiveDisplayName,
getLoginByAccountID,
getLoginsByAccountIDs,
getPersonalDetailByEmail,
getPersonalDetailsByIDs,
Expand All @@ -140,7 +139,6 @@ import {
isPaidGroupPolicy as isPaidGroupPolicyPolicyUtils,
isPolicyAdmin as isPolicyAdminPolicyUtils,
isPolicyAuditor,
isPolicyMember,
isPolicyOwner,
isSubmitAndClose,
shouldShowPolicy,
Expand Down Expand Up @@ -4092,27 +4090,6 @@ function getTransactionCommentObject(transaction: OnyxEntry<Transaction>): Comme
};
}

function isWorkspacePayer(memberLogin: string, policy: OnyxEntry<Policy>): boolean {
const isAdmin = policy?.employeeList?.[memberLogin]?.role === CONST.POLICY.ROLE.ADMIN;
if (isPaidGroupPolicyPolicyUtils(policy)) {
if (policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES) {
// If we get here without a reimburser only admin is the payer.
if (!policy?.achAccount?.reimburser) {
return isAdmin;
}

// If we are the reimburser then we are the payer.
const isReimburser = memberLogin === policy?.achAccount?.reimburser;
return isReimburser;
}
Comment thread
Burhan-Rashid marked this conversation as resolved.
Resolved
if (policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL) {
return isAdmin;
}
return false;
}
return false;
}

/**
* Can only edit if:
*
Expand Down Expand Up @@ -11236,23 +11213,12 @@ function doesReportContainRequestsFromMultipleUsers(iouReport: OnyxEntry<Report>
/**
* Determines whether the report can be moved to the workspace.
*/
function isWorkspaceEligibleForReportChange(newPolicy: OnyxEntry<Policy>, report: OnyxEntry<Report>, policies: OnyxCollection<Policy>): boolean {
const submitterEmail = getLoginByAccountID(report?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID);
const managerLogin = getLoginByAccountID(report?.managerID ?? CONST.DEFAULT_NUMBER_ID);
// We can't move the iou report to the workspace if both users from the iou report create the expense
if (doesReportContainRequestsFromMultipleUsers(report)) {
function isWorkspaceEligibleForReportChange(submitterEmail: string | undefined, newPolicy: OnyxEntry<Policy>): boolean {
if (!submitterEmail || !newPolicy?.isPolicyExpenseChatEnabled) {
return false;
}

if (!newPolicy?.isPolicyExpenseChatEnabled) {
return false;
}

// We can only move the iou report to the workspace if the manager is the payer of the new policy
if (isIOUReport(report)) {
return isPaidGroupPolicyPolicyUtils(newPolicy) && isWorkspacePayer(managerLogin ?? '', newPolicy);
}
return isPaidGroupPolicyPolicyUtils(newPolicy) && (isPolicyMember(newPolicy, submitterEmail) || isPolicyAdmin(newPolicy?.id, policies));
return isPaidGroupPolicyPolicyUtils(newPolicy) && !!newPolicy.role;
}

function getApprovalChain(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Report>): string[] {
Expand Down Expand Up @@ -12133,6 +12099,7 @@ export {
getMovedActionMessage,
excludeParticipantsForDisplay,
getReportName,
doesReportContainRequestsFromMultipleUsers,
};
export type {
Ancestor,
Expand Down
5 changes: 4 additions & 1 deletion src/pages/ReportChangeWorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {ReportChangeWorkspaceNavigatorParamList} from '@libs/Navigation/typ
import {getLoginByAccountID} from '@libs/PersonalDetailsUtils';
import {isPolicyAdmin, isPolicyMember} from '@libs/PolicyUtils';
import {isExpenseReport, isIOUReport, isMoneyRequestReport, isMoneyRequestReportPendingDeletion, isWorkspaceEligibleForReportChange} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import NotFoundPage from './ErrorPage/NotFoundPage';
Expand Down Expand Up @@ -67,14 +68,16 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
[session?.email, route.params, report, reportID, reportNextStep, policies, formatPhoneNumber, isReportArchived],
);

const submitterEmail = getLoginByAccountID(report?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID);

const {sections, shouldShowNoResultsFoundMessage, shouldShowSearchInput} = useWorkspaceList({
policies,
currentUserLogin: session?.email,
shouldShowPendingDeletePolicy: false,
selectedPolicyIDs: report.policyID ? [report.policyID] : undefined,
searchTerm: debouncedSearchTerm,
localeCompare,
additionalFilter: (newPolicy) => isWorkspaceEligibleForReportChange(newPolicy, report, policies),
additionalFilter: (newPolicy) => isWorkspaceEligibleForReportChange(submitterEmail, newPolicy),
});

if (!isMoneyRequestReport(report) || isMoneyRequestReportPendingDeletion(report)) {
Expand Down
49 changes: 8 additions & 41 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ describe('PolicyUtils', () => {

it('returns false if policy is not paid group policy', async () => {
const currentUserLogin = employeeEmail;
const currentUserAccountID = employeeAccountID;

const newPolicy = {
...createRandomPolicy(1, CONST.POLICY.TYPE.PERSONAL),
Expand All @@ -674,23 +673,14 @@ describe('PolicyUtils', () => {
[currentUserLogin]: {email: currentUserLogin, role: CONST.POLICY.ROLE.USER},
},
};
const policies = {[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]: newPolicy};
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
const report = {
...createRandomReport(0),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
ownerAccountID: currentUserAccountID,
managerID: approverAccountID,
};

const result = isWorkspaceEligibleForReportChange(newPolicy, report, policies);
const result = isWorkspaceEligibleForReportChange(currentUserLogin, newPolicy);
expect(result).toBe(false);
});

it('returns true if policy is paid group policy and the manger is the payer', async () => {
const currentUserLogin = employeeEmail;
const currentUserAccountID = employeeAccountID;

const newPolicy = {
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
Expand All @@ -700,21 +690,15 @@ describe('PolicyUtils', () => {
[currentUserLogin]: {email: currentUserLogin, role: CONST.POLICY.ROLE.ADMIN},
},
};
const policies = {[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]: newPolicy};
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
const report = {
...createRandomReport(0),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
ownerAccountID: approverAccountID,
managerID: currentUserAccountID,
};

const result = isWorkspaceEligibleForReportChange(newPolicy, report, policies);
const result = isWorkspaceEligibleForReportChange(currentUserLogin, newPolicy);
expect(result).toBe(true);
});

it('returns false if the manager is not the payer of the new policy', async () => {
it('returns true if the manager is not the payer of the new policy', async () => {
const currentUserLogin = employeeEmail;

const newPolicy = {
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
isPolicyExpenseChatEnabled: true,
Expand All @@ -723,23 +707,14 @@ describe('PolicyUtils', () => {
[approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.USER},
},
};
const policies = {[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]: newPolicy};
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
const report = {
...createRandomReport(0),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
ownerAccountID: employeeAccountID,
managerID: approverAccountID,
};

const result = isWorkspaceEligibleForReportChange(newPolicy, report, policies);
expect(result).toBe(false);
const result = isWorkspaceEligibleForReportChange(currentUserLogin, newPolicy);
expect(result).toBe(true);
});

it('returns false if policies are not policyExpenseChatEnabled', async () => {
const currentUserLogin = employeeEmail;
const currentUserAccountID = employeeAccountID;

const newPolicy = {
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
Expand All @@ -749,17 +724,9 @@ describe('PolicyUtils', () => {
[currentUserLogin]: {email: currentUserLogin, role: CONST.POLICY.ROLE.ADMIN},
},
};
const policies = {[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]: newPolicy};
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`, newPolicy);
const report = {
...createRandomReport(0),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
ownerAccountID: approverAccountID,
managerID: currentUserAccountID,
};

const result = isWorkspaceEligibleForReportChange(newPolicy, report, policies);
const result = isWorkspaceEligibleForReportChange(currentUserLogin, newPolicy);
expect(result).toBe(false);
});
});
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/ReportSecondaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ describe('getSecondaryAction', () => {
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.HOLD)).toBe(true);
});

it('includes CHANGE_WORKSPACE option for submitted IOU report and manager being the payer of the new policy', async () => {
it('does not include CHANGE_WORKSPACE option for submitted IOU report and manager being the payer of the new policy', async () => {
const report = {
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.IOU,
Expand Down Expand Up @@ -617,7 +617,7 @@ describe('getSecondaryAction', () => {
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails);

const result = getSecondaryReportActions({report, chatReport, reportTransactions: [], violations: {}, policy, policies});
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CHANGE_WORKSPACE)).toBe(true);
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CHANGE_WORKSPACE)).toBe(false);
});

it('includes CHANGE_WORKSPACE option for open expense report submitter', async () => {
Expand Down Expand Up @@ -659,6 +659,7 @@ describe('getSecondaryAction', () => {
id: OLD_POLICY_ID,
type: CONST.POLICY.TYPE.TEAM,
approver: MANAGER_EMAIL,
role: CONST.POLICY.ROLE.USER,
employeeList: {
[MANAGER_EMAIL]: {email: MANAGER_EMAIL, role: CONST.POLICY.ROLE.USER},
[EMPLOYEE_EMAIL]: {email: EMPLOYEE_EMAIL, role: CONST.POLICY.ROLE.USER},
Expand All @@ -669,6 +670,7 @@ describe('getSecondaryAction', () => {
id: POLICY_ID,
type: CONST.POLICY.TYPE.TEAM,
isPolicyExpenseChatEnabled: true,
role: CONST.POLICY.ROLE.USER,
employeeList: {
[MANAGER_EMAIL]: {email: MANAGER_EMAIL, role: CONST.POLICY.ROLE.USER},
[EMPLOYEE_EMAIL]: {email: EMPLOYEE_EMAIL, role: CONST.POLICY.ROLE.USER},
Expand Down Expand Up @@ -713,6 +715,7 @@ describe('getSecondaryAction', () => {
id: OLD_POLICY_ID,
type: CONST.POLICY.TYPE.TEAM,
approver: APPROVER_EMAIL,
role: CONST.POLICY.ROLE.USER,
employeeList: {
[APPROVER_EMAIL]: {email: APPROVER_EMAIL, role: CONST.POLICY.ROLE.USER},
[EMPLOYEE_EMAIL]: {email: EMPLOYEE_EMAIL, role: CONST.POLICY.ROLE.USER},
Expand All @@ -739,6 +742,7 @@ describe('getSecondaryAction', () => {
type: CONST.POLICY.TYPE.TEAM,
approver: APPROVER_EMAIL,
isPolicyExpenseChatEnabled: true,
role: CONST.POLICY.ROLE.USER,
employeeList: {
[APPROVER_EMAIL]: {email: APPROVER_EMAIL, role: CONST.POLICY.ROLE.USER},
[EMPLOYEE_EMAIL]: {email: EMPLOYEE_EMAIL, role: CONST.POLICY.ROLE.USER},
Expand Down