diff --git a/src/libs/WorkflowUtils.ts b/src/libs/WorkflowUtils.ts index 3ff72bf38f01..9fe17c1fe3da 100644 --- a/src/libs/WorkflowUtils.ts +++ b/src/libs/WorkflowUtils.ts @@ -233,6 +233,11 @@ function convertPolicyEmployeesToApprovalWorkflows({policy, personalDetails, fir const hrAdvancedModeFinalApproverEmail = getHRAdvancedModeFinalApprover(policy); for (const employee of Object.values(employees)) { + // The type doesn't allow it, but `employeeList` can hold a null entry at runtime, so skip it rather than crash + if (!employee) { + continue; + } + const {email, submitsTo, pendingAction} = employee; if (!email) { continue; @@ -824,7 +829,8 @@ function mergeWorkflowMembersWithAvailableMembers(workflowMembers: Member[], all */ function includesEveryWorkspaceMember(memberEmails: Array, employeeList: PolicyEmployeeList | undefined): boolean { const memberEmailSet = new Set(memberEmails); - const workspaceMembers = Object.values(employeeList ?? {}).filter((employee) => !!employee.email && employee.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); + // `employeeList` can hold a null entry at runtime, so read it optionally + const workspaceMembers = Object.values(employeeList ?? {}).filter((employee) => !!employee?.email && employee.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); return workspaceMembers.length > 0 && workspaceMembers.every((employee) => memberEmailSet.has(employee.email)); } @@ -1662,6 +1668,11 @@ function convertApprovalWorkflowRulesToWorkflows({ const availableMembers: Member[] = []; for (const employee of Object.values(employees)) { + // The type doesn't allow it, but `employeeList` can hold a null entry at runtime, so skip it rather than crash + if (!employee) { + continue; + } + const {email, submitsTo, pendingAction} = employee; if (!email) { continue; @@ -1845,8 +1856,9 @@ function hasApprovalWorkflowWithNonMemberApprover({policy, currentUserLogin, rul const hasNonMemberRoute = isNonMemberApprover(policy, getHRFinalApprover(policy) ?? getDefaultApprover(policy)) || Object.values(policy?.employeeList ?? {}).some( + // `employeeList` can hold a null entry at runtime, so read it optionally (employee) => - isNonMemberApprover(policy, employee.submitsTo) || isNonMemberApprover(policy, employee.forwardsTo) || isNonMemberApprover(policy, employee.overLimitForwardsTo), + isNonMemberApprover(policy, employee?.submitsTo) || isNonMemberApprover(policy, employee?.forwardsTo) || isNonMemberApprover(policy, employee?.overLimitForwardsTo), ); if (!hasNonMemberRoute) { return false; diff --git a/tests/unit/WorkflowUtilsTest.ts b/tests/unit/WorkflowUtilsTest.ts index 9c7fe114808f..677ad8e2f41a 100644 --- a/tests/unit/WorkflowUtilsTest.ts +++ b/tests/unit/WorkflowUtilsTest.ts @@ -1237,6 +1237,16 @@ describe('WorkflowUtils', () => { // Then it doesn't have everyone, so no workflow is made the default by mistake expect(includesEveryWorkspaceMember(['1@example.com'], {})).toBe(false); }); + + it('skips a null employeeList entry instead of crashing', () => { + // Given a workspace whose employeeList holds a null entry, which the type rules out but happens at runtime + const employeeListWithNull: PolicyEmployeeList = {...employeeList}; + Reflect.set(employeeListWithNull, 'stale@example.com', null); + + // When checking a workflow with every member, as creating or saving a workflow does + // Then the null entry is skipped rather than crashing, and the workflow still has everyone + expect(includesEveryWorkspaceMember(['1@example.com', '2@example.com'], employeeListWithNull)).toBe(true); + }); }); describe('convertApprovalWorkflowToPolicyEmployees', () => { @@ -3416,6 +3426,21 @@ describe('WorkflowUtils', () => { expect(hasApprovalWorkflowWithNonMemberApprover({policy: approvalsOffPolicy, currentUserLogin: ownerEmail, isMultipleApproversBetaEnabled: false})).toBe(false); expect(hasApprovalWorkflowWithNonMemberApprover({policy: lockedPolicy, currentUserLogin: ownerEmail, isMultipleApproversBetaEnabled: false})).toBe(false); }); + + it.each([false, true])('skips a null employeeList entry instead of crashing when the multiple approvers beta is %s', (isMultipleApproversBetaEnabled) => { + // Given a workspace whose employeeList holds a null entry, which the type rules out but happens at runtime, + // next to member 1 who still submits to someone no longer on the workspace + // The type rules out a null entry, so set it without a cast. It goes in first so every pass over employeeList reaches it before member 1 + const employeeList: PolicyEmployeeList = {}; + Reflect.set(employeeList, 'stale@example.com', null); + employeeList[ownerEmail] = {email: ownerEmail, role: CONST.POLICY.ROLE.ADMIN, submitsTo: ownerEmail}; + employeeList['1@example.com'] = {email: '1@example.com', role: CONST.POLICY.ROLE.USER, submitsTo: removedEmail}; + const policy = buildPolicy({employeeList}); + + // When the owner checks whether a workflow needs fixing, as the Workspaces tab does on every page + // Then the null entry is skipped rather than crashing the tab bar, and member 1's workflow is still flagged + expect(hasApprovalWorkflowWithNonMemberApprover({policy, currentUserLogin: ownerEmail, rules: {}, isMultipleApproversBetaEnabled})).toBe(true); + }); }); describe('getNonMemberApproverError', () => {