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
16 changes: 14 additions & 2 deletions src/libs/WorkflowUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -824,7 +829,8 @@ function mergeWorkflowMembersWithAvailableMembers(workflowMembers: Member[], all
*/
function includesEveryWorkspaceMember(memberEmails: Array<string | null | undefined>, 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));
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/WorkflowUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
Loading