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: 10 additions & 0 deletions src/libs/PolicyUtils/approvals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ function getManagerAccountEmail(policy: OnyxEntry<Policy>, ownerLogin: string |
return employee?.submitsTo ?? defaultApprover ?? '';
}

/**
* Whether the user is the report owner's workflow approver (the direct submitsTo, not a rule approver), resolved from the
* policy because the report's managerID is stale on drafts. Fails closed on an unresolved owner login or user email,
* because getManagerAccountEmail would otherwise fall back to the policy's default approver.
*/
function isWorkflowApprover(policy: OnyxEntry<Policy>, ownerLogin: string | undefined, rules: OnyxCollection<Rule>, reportTotal: number, userEmail: string | undefined): boolean {
return !!userEmail && !!ownerLogin && !isSubmitAndClose(policy) && userEmail === getManagerAccountEmail(policy, ownerLogin, rules, reportTotal);
}

function getManagerAccountID(policy: OnyxEntry<Policy>, ownerLogin: string | undefined, rules: OnyxCollection<Rule>, reportTotal = 0) {
const managerEmail = getManagerAccountEmail(policy, ownerLogin, rules, reportTotal);
return managerEmail ? (getAccountIDsByLogins([managerEmail]).at(0) ?? -1) : -1;
Expand Down Expand Up @@ -455,6 +464,7 @@ export {
evaluateApprovalWorkflowRule,
getForwardsToFromRules,
getManagerAccountEmail,
isWorkflowApprover,
getManagerAccountID,
getSubmitToEmail,
getSubmitToAccountID,
Expand Down
14 changes: 4 additions & 10 deletions src/libs/ReportPrimaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
canAdminPayReport,
canSubmitOwnReportOnPolicy,
getIntegrationAccountingMethod,
getManagerAccountID,
getSubmitToAccountID,
getValidConnectedIntegration,
hasDynamicExternalWorkflow,
Expand All @@ -23,9 +22,9 @@ import {
isPolicyAdmin as isPolicyAdminPolicyUtils,
isPolicyApprover,
isPreferredExporter,
isSubmitAndClose,
isSubmitPolicy,
isSubmitterApproveBlockedOnSubmitWorkspace,
isWorkflowApprover,
} from './PolicyUtils';
import {
getAllReportActions,
Expand Down Expand Up @@ -165,18 +164,13 @@ function isSubmitAction(
return false;
}

// Workflow approver (direct submitsTo, not rule approvers). Fail closed on unresolved ownerLogin — else falls back to policy.approver.
const isWorkflowApprover =
!isReportSubmitter &&
currentUserAccountID !== undefined &&
!!ownerLogin &&
!isSubmitAndClose(policy) &&
currentUserAccountID === getManagerAccountID(policy, ownerLogin, rules, report.total ?? 0);
// A role that cannot create expenses cannot submit its own report.
if (isReportSubmitter && !canSubmitOwnReportOnPolicy(policy, currentUserEmail)) {
return false;
}
const canBeSubmitter = isReportSubmitter || isWorkflowApprover;

const isCurrentUserWorkflowApprover = !isReportSubmitter && isWorkflowApprover(policy, ownerLogin, rules, report.total ?? 0, currentUserEmail);
const canBeSubmitter = isReportSubmitter || isCurrentUserWorkflowApprover;
return isExpenseReport && canBeSubmitter && isOpenReport && reportTransactions.length !== 0;
}

Expand Down
14 changes: 4 additions & 10 deletions src/libs/ReportSecondaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
canMemberWrite,
getConnectedIntegration,
getCorrectedAutoReportingFrequency,
getManagerAccountID,
getReimbursementChoice,
getSubmitToAccountID,
getValidConnectedIntegration,
Expand All @@ -43,6 +42,7 @@ import {
isSubmitAndClose,
isSubmitPolicy,
isSubmitterApproveBlockedOnSubmitWorkspace,
isWorkflowApprover,
} from './PolicyUtils';
import {
getAllReportActions,
Expand Down Expand Up @@ -270,16 +270,10 @@ function isSubmitAction({
const isReportSubmitter = isCurrentUserSubmitter(report);
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;

// Workflow approver (direct submitsTo, not rule approvers). Fail closed on unresolved ownerLogin — else falls back to policy.approver.
const submitToAccountID = getSubmitToAccountID(policy, report, ownerLogin, rules);
const isWorkflowApprover =
!isReportSubmitter &&
currentUserAccountID !== undefined &&
!!ownerLogin &&
!isSubmitAndClose(policy) &&
currentUserAccountID === getManagerAccountID(policy, ownerLogin, rules, report.total ?? 0);
const isCurrentUserWorkflowApprover = !isReportSubmitter && isWorkflowApprover(policy, ownerLogin, rules, report.total ?? 0, currentUserLogin);

if (!isReportSubmitter && !isAdmin && !isWorkflowApprover) {
if (!isReportSubmitter && !isAdmin && !isCurrentUserWorkflowApprover) {
return false;
}

Expand Down Expand Up @@ -308,7 +302,7 @@ function isSubmitAction({
return true;
}

if (isAdmin || isWorkflowApprover) {
if (isAdmin || isCurrentUserWorkflowApprover) {
return true;
}

Expand Down
11 changes: 3 additions & 8 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ import {
isSubmitAndClose,
isSubmitPolicy,
isSubmitterApproveBlockedOnSubmitWorkspace,
isWorkflowApprover,
resolveCurrentTaxCode,
shouldShowPolicy,
} from './PolicyUtils';
Expand Down Expand Up @@ -6140,14 +6141,11 @@ function canEditMoneyRequest(
return true;
}

// Resolve approver from policy (managerID stale on drafts). Fail closed on unresolved ownerLogin — else falls back to policy.approver.
const ownerLogin = getLoginByAccountID(moneyRequestReport?.ownerAccountID, getAllPersonalDetails());
const isWorkflowApproverEditing =
!!ownerLogin &&
isExpenseReport(moneyRequestReport) &&
isOpenReport(moneyRequestReport) &&
!isSubmitAndClose(reportPolicy) &&
deprecatedCurrentUserAccountID === getManagerAccountID(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0);
isWorkflowApprover(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0, deprecatedCurrentUserEmail);
if (isWorkflowApproverEditing) {
return true;
}
Expand Down Expand Up @@ -6335,14 +6333,11 @@ function canCurrentUserEditExpense(
const isAdmin = isExpenseReport(moneyRequestReport) && reportPolicy?.role === CONST.POLICY.ROLE.ADMIN;
const isManager = isExpenseReport(moneyRequestReport) && deprecatedCurrentUserAccountID === moneyRequestReport?.managerID;
const isRequestor = deprecatedCurrentUserAccountID === reportAction?.actorAccountID;
// Resolve approver from policy (managerID stale on drafts). Fail closed on unresolved ownerLogin — else falls back to policy.approver.
const ownerLogin = getLoginByAccountID(moneyRequestReport?.ownerAccountID, getAllPersonalDetails());
const isApprover =
!!ownerLogin &&
isExpenseReport(moneyRequestReport) &&
isOpenReport(moneyRequestReport) &&
!isSubmitAndClose(reportPolicy) &&
deprecatedCurrentUserAccountID === getManagerAccountID(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0);
isWorkflowApprover(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0, deprecatedCurrentUserEmail);

return isAdmin || isManager || isRequestor || isApprover;
}
Expand Down
72 changes: 72 additions & 0 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
getIntegrationLastSuccessfulDate,
getManagerAccountID,
getMemberAccountIDsForWorkspace,
isWorkflowApprover,
getActiveVendorMatchingIntegration,
getMatchingVendorByID,
getMatchingVendors,
Expand Down Expand Up @@ -3229,6 +3230,77 @@ describe('PolicyUtils', () => {
});
});

describe('isWorkflowApprover', () => {
const buildAdvancedPolicy = (): Policy => ({
...createRandomPolicy(0),
type: CONST.POLICY.TYPE.CORPORATE,
approvalMode: CONST.POLICY.APPROVAL_MODE.ADVANCED,
approver: approverEmail,
employeeList: {
[employeeEmail]: {email: employeeEmail, submitsTo: adminEmail},
[adminEmail]: {email: adminEmail},
},
});

it('should return true when the user is the owner submitsTo', () => {
// Given an advanced workflow where the employee submits to the admin
const policy = buildAdvancedPolicy();

// When checking whether the admin is the employee's workflow approver
const result = isWorkflowApprover(policy, employeeEmail, undefined, 0, adminEmail);

// Then it is true, because the admin is who the report is submitted to
expect(result).toBe(true);
});

it('should return false when the user is not the owner submitsTo', () => {
// Given an advanced workflow where the employee submits to the admin
const policy = buildAdvancedPolicy();

// When checking whether someone else is the employee's workflow approver
const result = isWorkflowApprover(policy, employeeEmail, undefined, 0, categoryApprover1Email);

// Then it is false, because the report isn't submitted to them
expect(result).toBe(false);
});

it('should return false when the owner login is unresolved, even for the default approver', () => {
// Given an advanced workflow whose default approver is the approver
const policy = buildAdvancedPolicy();

// When the owner's login isn't known yet
const result = isWorkflowApprover(policy, undefined, undefined, 0, approverEmail);

// Then it is false, because without the owner the manager would fall back to the default approver and
// wrongly grant approver rights to them
expect(result).toBe(false);
});

it('should return false when the user email is missing, even when no manager resolves', () => {
// Given a policy where the owner has no employee entry and there is no default approver, so the manager
// email resolves to an empty string
const policy: Policy = {...buildAdvancedPolicy(), approver: undefined, owner: '', employeeList: {}};

// When the user's email isn't known
const result = isWorkflowApprover(policy, employeeEmail, undefined, 0, '');

// Then it is false, because an empty email must never match an empty manager email
expect(result).toBe(false);
});

it('should return false on a Submit & Close policy', () => {
// Given a Submit & Close policy, where reports have no approver, even though the employee's submitsTo is
// still set to the admin
const policy: Policy = {...buildAdvancedPolicy(), approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL};

// When checking whether the admin is the employee's workflow approver
const result = isWorkflowApprover(policy, employeeEmail, undefined, 0, adminEmail);

// Then it is false, because nobody approves reports on this policy
expect(result).toBe(false);
});
});

describe('approval workflow rules', () => {
const policyID = 'RULES_POLICY_1';
const submitFilter = {operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, left: CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM, right: [employeeEmail]};
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/ReportSecondaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,6 @@ describe('getSecondaryAction', () => {
const OWNER_EMAIL = 'owner@mail.com';
// beforeEach's Onyx.clear isn't awaited; explicit clear + reset guarantees state.
await Onyx.clear();
await Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, {[EMPLOYEE_ACCOUNT_ID]: PERSONAL_DETAILS});
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
Expand Down Expand Up @@ -1167,7 +1166,6 @@ describe('getSecondaryAction', () => {
const SUBMITTER_ACCOUNT_ID = 5;
const SUBMITTER_EMAIL = 'submitter@mail.com';
await Onyx.clear();
await Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, {[EMPLOYEE_ACCOUNT_ID]: PERSONAL_DETAILS});
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
Expand Down Expand Up @@ -1212,7 +1210,6 @@ describe('getSecondaryAction', () => {
const SUBMITTER_EMAIL = 'submitter@mail.com';
const OTHER_WORKFLOW_APPROVER_EMAIL = 'other-approver@mail.com';
await Onyx.clear();
await Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, {[EMPLOYEE_ACCOUNT_ID]: PERSONAL_DETAILS});
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
Expand Down
Loading