Skip to content
Closed
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: 1 addition & 9 deletions src/libs/ReportSecondaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -27,7 +26,6 @@ import {
canEditReportPolicy,
canHoldUnholdReportAction,
canRejectReportAction,
doesReportContainRequestsFromMultipleUsers,
getTransactionDetails,
hasOnlyHeldExpenses,
hasOnlyNonReimbursableTransactions,
Expand Down Expand Up @@ -436,13 +434,7 @@ function isHoldActionForTransaction(report: Report, reportTransaction: Transacti
}

function isChangeWorkspaceAction(report: Report, policies: OnyxCollection<Policy>, reportActions?: ReportAction[]): boolean {
// 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));
const availablePolicies = Object.values(policies ?? {}).filter((newPolicy) => isWorkspaceEligibleForReportChange(newPolicy, report));
let hasAvailablePolicies = availablePolicies.length > 1;
if (!hasAvailablePolicies && availablePolicies.length === 1) {
hasAvailablePolicies = !report.policyID || report.policyID !== availablePolicies?.at(0)?.id;
Expand Down
37 changes: 34 additions & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ import {
getAccountIDsByLogins,
getDisplayNameOrDefault,
getEffectiveDisplayName,
getLoginByAccountID,
getLoginsByAccountIDs,
getPersonalDetailByEmail,
getPersonalDetailsByIDs,
Expand Down Expand Up @@ -4146,6 +4147,27 @@ 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;
}
if (policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL) {
return isAdmin;
}
return false;
}
return false;
}

/**
* Can only edit if:
*
Expand Down Expand Up @@ -11333,11 +11355,21 @@ function doesReportContainRequestsFromMultipleUsers(iouReport: OnyxEntry<Report>
/**
* Determines whether the report can be moved to the workspace.
*/
function isWorkspaceEligibleForReportChange(submitterEmail: string | undefined, newPolicy: OnyxEntry<Policy>): boolean {
if (!submitterEmail || !newPolicy?.isPolicyExpenseChatEnabled) {
function isWorkspaceEligibleForReportChange(newPolicy: OnyxEntry<Policy>, report: OnyxEntry<Report>): boolean {
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)) {
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) && !!newPolicy.role;
}

Expand Down Expand Up @@ -12195,7 +12227,6 @@ export {
getMovedActionMessage,
excludeParticipantsForDisplay,
getReportName,
doesReportContainRequestsFromMultipleUsers,
};
export type {
Ancestor,
Expand Down
5 changes: 1 addition & 4 deletions src/pages/ReportChangeWorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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,16 +66,14 @@ function ReportChangeWorkspacePage({report, route}: ReportChangeWorkspacePagePro
[route.params, report, reportID, reportNextStep, policies, formatPhoneNumber, isReportLastVisibleArchived],
);

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(submitterEmail, newPolicy),
additionalFilter: (newPolicy) => isWorkspaceEligibleForReportChange(newPolicy, report),
});

if (!isMoneyRequestReport(report) || isMoneyRequestReportPendingDeletion(report)) {
Expand Down
45 changes: 37 additions & 8 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ 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 @@ -743,13 +744,21 @@ describe('PolicyUtils', () => {
},
};
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(currentUserLogin, newPolicy);
const result = isWorkspaceEligibleForReportChange(newPolicy, report);
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 @@ -760,14 +769,19 @@ describe('PolicyUtils', () => {
},
};
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(currentUserLogin, newPolicy);
const result = isWorkspaceEligibleForReportChange(newPolicy, report);
expect(result).toBe(true);
});

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

it('returns false if the manager is not the payer of the new policy', async () => {
const newPolicy = {
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
isPolicyExpenseChatEnabled: true,
Expand All @@ -777,13 +791,21 @@ describe('PolicyUtils', () => {
},
};
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(currentUserLogin, newPolicy);
expect(result).toBe(true);
const result = isWorkspaceEligibleForReportChange(newPolicy, report);
expect(result).toBe(false);
});

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 @@ -794,8 +816,15 @@ describe('PolicyUtils', () => {
},
};
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(currentUserLogin, newPolicy);
const result = isWorkspaceEligibleForReportChange(newPolicy, report);
expect(result).toBe(false);
});
});
Expand Down
4 changes: 2 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('does not include CHANGE_WORKSPACE option for submitted IOU report and manager being the payer of the new policy', async () => {
it('includes 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(false);
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CHANGE_WORKSPACE)).toBe(true);
});

it('includes CHANGE_WORKSPACE option for open expense report submitter', async () => {
Expand Down