From c860f78fdb8a1ba6647dd0ee1338cdd159ac4490 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Tue, 17 Nov 2026 19:51:15 +0800 Subject: [PATCH 1/3] compare the email --- src/libs/ReportPrimaryActionUtils.ts | 13 +++++-------- src/libs/ReportSecondaryActionUtils.ts | 8 ++------ src/libs/ReportUtils.ts | 4 ++-- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/libs/ReportPrimaryActionUtils.ts b/src/libs/ReportPrimaryActionUtils.ts index 8209dd306990..c41b51e55ddb 100644 --- a/src/libs/ReportPrimaryActionUtils.ts +++ b/src/libs/ReportPrimaryActionUtils.ts @@ -12,7 +12,7 @@ import { canAdminPayReport, canSubmitOwnReportOnPolicy, getIntegrationAccountingMethod, - getManagerAccountID, + getManagerAccountEmail, getSubmitToAccountID, getValidConnectedIntegration, hasDynamicExternalWorkflow, @@ -165,17 +165,14 @@ 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; } + + // Workflow approver (direct submitsTo, not rule approvers). Fail closed on unresolved ownerLogin — else falls back to policy.approver. + const isWorkflowApprover = + !isReportSubmitter && !!currentUserEmail && !!ownerLogin && !isSubmitAndClose(policy) && currentUserEmail === getManagerAccountEmail(policy, ownerLogin, rules, report.total ?? 0); const canBeSubmitter = isReportSubmitter || isWorkflowApprover; return isExpenseReport && canBeSubmitter && isOpenReport && reportTransactions.length !== 0; } diff --git a/src/libs/ReportSecondaryActionUtils.ts b/src/libs/ReportSecondaryActionUtils.ts index a80922a29495..26374a7b8ade 100644 --- a/src/libs/ReportSecondaryActionUtils.ts +++ b/src/libs/ReportSecondaryActionUtils.ts @@ -28,7 +28,7 @@ import { canMemberWrite, getConnectedIntegration, getCorrectedAutoReportingFrequency, - getManagerAccountID, + getManagerAccountEmail, getReimbursementChoice, getSubmitToAccountID, getValidConnectedIntegration, @@ -273,11 +273,7 @@ function isSubmitAction({ // 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); + !isReportSubmitter && !!currentUserLogin && !!ownerLogin && !isSubmitAndClose(policy) && currentUserLogin === getManagerAccountEmail(policy, ownerLogin, rules, report.total ?? 0); if (!isReportSubmitter && !isAdmin && !isWorkflowApprover) { return false; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 7ef7f9e325fa..3598617806fd 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -6078,7 +6078,7 @@ function canEditMoneyRequest( isExpenseReport(moneyRequestReport) && isOpenReport(moneyRequestReport) && !isSubmitAndClose(reportPolicy) && - deprecatedCurrentUserAccountID === getManagerAccountID(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0); + deprecatedCurrentUserEmail === getManagerAccountEmail(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0); if (isWorkflowApproverEditing) { return true; } @@ -6273,7 +6273,7 @@ function canCurrentUserEditExpense( isExpenseReport(moneyRequestReport) && isOpenReport(moneyRequestReport) && !isSubmitAndClose(reportPolicy) && - deprecatedCurrentUserAccountID === getManagerAccountID(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0); + deprecatedCurrentUserEmail === getManagerAccountEmail(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0); return isAdmin || isManager || isRequestor || isApprover; } From 041b5a12836598f882bf5b3b44697e025ab47f8d Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 18 Nov 2026 12:48:34 +0800 Subject: [PATCH 2/3] DRY-ing code --- src/libs/PolicyUtils/approvals.ts | 10 ++++ src/libs/ReportPrimaryActionUtils.ts | 9 ++-- src/libs/ReportSecondaryActionUtils.ts | 10 ++-- src/libs/ReportUtils.ts | 11 ++-- tests/unit/PolicyUtilsTest.ts | 72 ++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 20 deletions(-) diff --git a/src/libs/PolicyUtils/approvals.ts b/src/libs/PolicyUtils/approvals.ts index 12394b974ffa..acc2c5da9a23 100644 --- a/src/libs/PolicyUtils/approvals.ts +++ b/src/libs/PolicyUtils/approvals.ts @@ -330,6 +330,15 @@ function getManagerAccountEmail(policy: OnyxEntry, 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, ownerLogin: string | undefined, rules: OnyxCollection, reportTotal: number, userEmail: string | undefined): boolean { + return !!userEmail && !!ownerLogin && !isSubmitAndClose(policy) && userEmail === getManagerAccountEmail(policy, ownerLogin, rules, reportTotal); +} + function getManagerAccountID(policy: OnyxEntry, ownerLogin: string | undefined, rules: OnyxCollection, reportTotal = 0) { const managerEmail = getManagerAccountEmail(policy, ownerLogin, rules, reportTotal); return managerEmail ? (getAccountIDsByLogins([managerEmail]).at(0) ?? -1) : -1; @@ -455,6 +464,7 @@ export { evaluateApprovalWorkflowRule, getForwardsToFromRules, getManagerAccountEmail, + isWorkflowApprover, getManagerAccountID, getSubmitToEmail, getSubmitToAccountID, diff --git a/src/libs/ReportPrimaryActionUtils.ts b/src/libs/ReportPrimaryActionUtils.ts index c41b51e55ddb..652a23d9a4ba 100644 --- a/src/libs/ReportPrimaryActionUtils.ts +++ b/src/libs/ReportPrimaryActionUtils.ts @@ -12,7 +12,6 @@ import { canAdminPayReport, canSubmitOwnReportOnPolicy, getIntegrationAccountingMethod, - getManagerAccountEmail, getSubmitToAccountID, getValidConnectedIntegration, hasDynamicExternalWorkflow, @@ -23,9 +22,9 @@ import { isPolicyAdmin as isPolicyAdminPolicyUtils, isPolicyApprover, isPreferredExporter, - isSubmitAndClose, isSubmitPolicy, isSubmitterApproveBlockedOnSubmitWorkspace, + isWorkflowApprover, } from './PolicyUtils'; import { getAllReportActions, @@ -170,10 +169,8 @@ 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 && !!currentUserEmail && !!ownerLogin && !isSubmitAndClose(policy) && currentUserEmail === getManagerAccountEmail(policy, ownerLogin, rules, report.total ?? 0); - 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; } diff --git a/src/libs/ReportSecondaryActionUtils.ts b/src/libs/ReportSecondaryActionUtils.ts index 26374a7b8ade..e2eb4bcf8705 100644 --- a/src/libs/ReportSecondaryActionUtils.ts +++ b/src/libs/ReportSecondaryActionUtils.ts @@ -28,7 +28,6 @@ import { canMemberWrite, getConnectedIntegration, getCorrectedAutoReportingFrequency, - getManagerAccountEmail, getReimbursementChoice, getSubmitToAccountID, getValidConnectedIntegration, @@ -43,6 +42,7 @@ import { isSubmitAndClose, isSubmitPolicy, isSubmitterApproveBlockedOnSubmitWorkspace, + isWorkflowApprover, } from './PolicyUtils'; import { getAllReportActions, @@ -270,12 +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 && !!currentUserLogin && !!ownerLogin && !isSubmitAndClose(policy) && currentUserLogin === getManagerAccountEmail(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; } @@ -304,7 +302,7 @@ function isSubmitAction({ return true; } - if (isAdmin || isWorkflowApprover) { + if (isAdmin || isCurrentUserWorkflowApprover) { return true; } diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 3598617806fd..b1d46b5d0117 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -185,6 +185,7 @@ import { isSubmitAndClose, isSubmitPolicy, isSubmitterApproveBlockedOnSubmitWorkspace, + isWorkflowApprover, resolveCurrentTaxCode, shouldShowPolicy, } from './PolicyUtils'; @@ -6071,14 +6072,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) && - deprecatedCurrentUserEmail === getManagerAccountEmail(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0); + isWorkflowApprover(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0, deprecatedCurrentUserEmail); if (isWorkflowApproverEditing) { return true; } @@ -6266,14 +6264,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) && - deprecatedCurrentUserEmail === getManagerAccountEmail(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0); + isWorkflowApprover(reportPolicy, ownerLogin, rules, moneyRequestReport?.total ?? 0, deprecatedCurrentUserEmail); return isAdmin || isManager || isRequestor || isApprover; } diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index d9268a6a5b6c..89a23e340673 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -60,6 +60,7 @@ import { getIntegrationLastSuccessfulDate, getManagerAccountID, getMemberAccountIDsForWorkspace, + isWorkflowApprover, getActiveVendorMatchingIntegration, getMatchingVendorByID, getMatchingVendors, @@ -3228,6 +3229,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]}; From 992464e0ea588a7cba8dd1ce7e8401cc5e3391f7 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 18 Nov 2026 13:09:37 +0800 Subject: [PATCH 3/3] remove personal_details_list onyx load --- tests/unit/ReportSecondaryActionUtilsTest.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unit/ReportSecondaryActionUtilsTest.ts b/tests/unit/ReportSecondaryActionUtilsTest.ts index 98f805fa1fbb..759e5158c876 100644 --- a/tests/unit/ReportSecondaryActionUtilsTest.ts +++ b/tests/unit/ReportSecondaryActionUtilsTest.ts @@ -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({ reportID: REPORT_ID, type: CONST.REPORT.TYPE.EXPENSE, @@ -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({ reportID: REPORT_ID, type: CONST.REPORT.TYPE.EXPENSE, @@ -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({ reportID: REPORT_ID, type: CONST.REPORT.TYPE.EXPENSE,