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
37 changes: 37 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3775,6 +3775,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 @@ -10489,11 +10510,27 @@ function hasExportError(reportActions: OnyxEntry<ReportActions> | ReportAction[]
return Object.values(reportActions).some((action) => isIntegrationMessageAction(action));
}

function doesReportContainRequestsFromMultipleUsers(iouReport: OnyxEntry<Report>): boolean {
const transactions = getReportTransactions(iouReport?.reportID);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
return isIOUReport(iouReport) && transactions.some((transaction) => (transaction?.modifiedAmount || transaction?.amount) < 0);
}

/**
* 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)) {
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);
}
Comment on lines +10524 to +10533

@luacmartins luacmartins Jul 16, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI isWorkspaceEligibleForReportChange is supposed to check if the user has access to a paid policy. We have a separate method canEditReportPolicy that I think this logic is better suited for

return isPaidGroupPolicyPolicyUtils(newPolicy) && (isPolicyMember(submitterEmail, newPolicy?.id) || isPolicyAdmin(newPolicy?.id, policies));
}

Expand Down
17 changes: 8 additions & 9 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,14 +668,15 @@ describe('PolicyUtils', () => {
expect(result).toBe(false);
});

it('returns true if policy is paid group policy and submitter is a member of the new policy', async () => {
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),
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
employeeList: {
[currentUserLogin]: {email: currentUserLogin, role: CONST.POLICY.ROLE.USER},
[currentUserLogin]: {email: currentUserLogin, role: CONST.POLICY.ROLE.ADMIN},
},
};
const policies = {[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]: newPolicy};
Expand All @@ -684,22 +685,20 @@ describe('PolicyUtils', () => {
...createRandomReport(0),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
ownerAccountID: currentUserAccountID,
managerID: approverAccountID,
ownerAccountID: approverAccountID,
managerID: currentUserAccountID,
};

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

it('returns true if policy is paid group policy and submitter is not a member but current user is admin', async () => {
const currentUserLogin = adminEmail;

it('returns false if the manager is not the payer of the new policy', async () => {
const newPolicy = {
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
role: CONST.POLICY.ROLE.ADMIN,
employeeList: {
[currentUserLogin]: {email: currentUserLogin, role: CONST.POLICY.ROLE.ADMIN},
[approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.USER},
},
};
const policies = {[`${ONYXKEYS.COLLECTION.POLICY}${newPolicy.id}`]: newPolicy};
Expand All @@ -713,7 +712,7 @@ describe('PolicyUtils', () => {
};

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

Expand Down
40 changes: 5 additions & 35 deletions tests/unit/ReportSecondaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,12 @@ describe('getSecondaryAction', () => {
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.HOLD)).toBe(true);
});

it('includes CHANGE_WORKSPACE option for submitted IOU report and submitter being a member 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,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
managerID: MANAGER_ACCOUNT_ID,
ownerAccountID: MANAGER_ACCOUNT_ID,
managerID: EMPLOYEE_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
} as unknown as Report;
Expand All @@ -465,8 +465,9 @@ describe('getSecondaryAction', () => {
const policy = {
id: POLICY_ID,
type: CONST.POLICY.TYPE.TEAM,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL,
employeeList: {
[EMPLOYEE_EMAIL]: {email: EMPLOYEE_EMAIL, role: CONST.POLICY.ROLE.USER},
[EMPLOYEE_EMAIL]: {email: EMPLOYEE_EMAIL, role: CONST.POLICY.ROLE.ADMIN},
[MANAGER_EMAIL]: {email: MANAGER_EMAIL, role: CONST.POLICY.ROLE.USER},
},
} as unknown as Policy;
Expand All @@ -480,37 +481,6 @@ describe('getSecondaryAction', () => {
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.CHANGE_WORKSPACE)).toBe(true);
});

it('includes CHANGE_WORKSPACE option for IOU and the submitter is not a member but current user is admin', async () => {
const report = {
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.IOU,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
managerID: MANAGER_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
} as unknown as Report;
const personalDetails = {
[ADMIN_ACCOUNT_ID]: {login: ADMIN_EMAIL},
[MANAGER_ACCOUNT_ID]: {login: MANAGER_EMAIL},
};
const policy = {
id: POLICY_ID,
type: CONST.POLICY.TYPE.TEAM,
role: CONST.POLICY.ROLE.ADMIN,
employeeList: {
[ADMIN_EMAIL]: {email: ADMIN_EMAIL, role: CONST.POLICY.ROLE.ADMIN},
},
} as unknown as Policy;
const policies = {[`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`]: policy};
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report);
await Onyx.merge(ONYXKEYS.SESSION, {email: EMPLOYEE_EMAIL, accountID: EMPLOYEE_ACCOUNT_ID});
await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails);

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

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