diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index f6b277d69d6b..8faa0db3b7fc 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -69,11 +69,13 @@ Onyx.connect({ /** * Filter out the active policies, which will exclude policies with pending deletion + * and policies the current user doesn't belong to. * These are policies that we can use to create reports with in NewDot. */ -function getActivePolicies(policies: OnyxCollection | null): Policy[] { +function getActivePolicies(policies: OnyxCollection | null, currentUserLogin: string | undefined): Policy[] { return Object.values(policies ?? {}).filter( - (policy): policy is Policy => !!policy && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !!policy.name && !!policy.id, + (policy): policy is Policy => + !!policy && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !!policy.name && !!policy.id && !!getPolicyRole(policy, currentUserLogin), ); } /** @@ -636,7 +638,7 @@ function getPolicy(policyID: string | undefined): OnyxEntry { /** Return active policies where current user is an admin */ function getActiveAdminWorkspaces(policies: OnyxCollection | null, currentUserLogin: string | undefined): Policy[] { - const activePolicies = getActivePolicies(policies); + const activePolicies = getActivePolicies(policies, currentUserLogin); return activePolicies.filter((policy) => shouldShowPolicy(policy, NetworkStore.isOffline(), currentUserLogin) && isPolicyAdmin(policy, currentUserLogin)); } @@ -652,7 +654,7 @@ function canSendInvoice(policies: OnyxCollection | null, currentUserLogi } function hasWorkspaceWithInvoices(currentUserLogin: string | undefined): boolean { - const activePolicies = getActivePolicies(allPolicies); + const activePolicies = getActivePolicies(allPolicies, currentUserLogin); return activePolicies.some((policy) => shouldShowPolicy(policy, NetworkStore.isOffline(), currentUserLogin) && policy.areInvoicesEnabled); } diff --git a/src/pages/workspace/WorkspaceNewRoomPage.tsx b/src/pages/workspace/WorkspaceNewRoomPage.tsx index 37686ba1c3a7..c64c53306a1f 100644 --- a/src/pages/workspace/WorkspaceNewRoomPage.tsx +++ b/src/pages/workspace/WorkspaceNewRoomPage.tsx @@ -64,14 +64,14 @@ function WorkspaceNewRoomPage() { const workspaceOptions = useMemo( () => - PolicyUtils.getActivePolicies(policies) + PolicyUtils.getActivePolicies(policies, session?.email) ?.filter((policy) => policy.type !== CONST.POLICY.TYPE.PERSONAL) .map((policy) => ({ label: policy.name, value: policy.id, })) .sort((a, b) => localeCompare(a.label, b.label)) ?? [], - [policies], + [policies, session?.email], ); const [policyID, setPolicyID] = useState(() => { if (!!activeWorkspaceOrDefaultID && workspaceOptions.some((option) => option.value === activeWorkspaceOrDefaultID)) { diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 71830443063a..c37d7970f1cf 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -1,10 +1,24 @@ import * as PolicyUtils from '@libs/PolicyUtils'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy} from '@src/types/onyx'; +import createCollection from '../utils/collections/createCollection'; +import createRandomPolicy from '../utils/collections/policies'; function toLocaleDigitMock(dot: string): string { return dot; } describe('PolicyUtils', () => { + describe('getActivePolicies', () => { + it("getActivePolicies should filter out policies that the current user doesn't belong to", () => { + const policies = createCollection( + (item) => `${ONYXKEYS.COLLECTION.POLICY}${item.id}`, + (index) => ({...createRandomPolicy(index + 1), ...(!index && {role: null})} as Policy), + 2, + ); + expect(PolicyUtils.getActivePolicies(policies, undefined)).toHaveLength(1); + }); + }); describe('getRateDisplayValue', () => { it('should return an empty string for NaN', () => { const rate = PolicyUtils.getRateDisplayValue('invalid' as unknown as number, toLocaleDigitMock);