diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index f6b277d69d6b..2dc436a4d76c 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -62,6 +62,14 @@ Onyx.connect({ callback: (value) => (allPolicies = value), }); +let currentUserEmail: string; +Onyx.connect({ + key: ONYXKEYS.SESSION, + callback: (val) => { + currentUserEmail = val?.email ?? ''; + }, +}); + Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, callback: (value) => (activePolicyId = value), @@ -71,9 +79,15 @@ Onyx.connect({ * Filter out the active policies, which will exclude policies with pending deletion * These are policies that we can use to create reports with in NewDot. */ -function getActivePolicies(policies: OnyxCollection | null): Policy[] { +function getActivePolicies(policies: OnyxCollection | null, excludePersonalPolicy = false): 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 && + (!excludePersonalPolicy || policy.type !== CONST.POLICY.TYPE.PERSONAL) && + !!getPolicyRole(policy, currentUserEmail), ); } /** diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index a304ee800131..026d55966943 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -8216,9 +8216,7 @@ function createDraftTransactionAndNavigateToParticipantSelector(transactionID: s mccGroup, } as Transaction); - const filteredPolicies = Object.values(allPolicies ?? {}).filter( - (policy) => policy && policy.type !== CONST.POLICY.TYPE.PERSONAL && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, - ); + const filteredPolicies = PolicyUtils.getActivePolicies(allPolicies, true); if (actionName === CONST.IOU.ACTION.CATEGORIZE) { const activePolicy = getPolicy(activePolicyID); diff --git a/src/pages/workspace/WorkspaceNewRoomPage.tsx b/src/pages/workspace/WorkspaceNewRoomPage.tsx index 37686ba1c3a7..a23d35d955a2 100644 --- a/src/pages/workspace/WorkspaceNewRoomPage.tsx +++ b/src/pages/workspace/WorkspaceNewRoomPage.tsx @@ -64,8 +64,7 @@ function WorkspaceNewRoomPage() { const workspaceOptions = useMemo( () => - PolicyUtils.getActivePolicies(policies) - ?.filter((policy) => policy.type !== CONST.POLICY.TYPE.PERSONAL) + PolicyUtils.getActivePolicies(policies, true) .map((policy) => ({ label: policy.name, value: policy.id, diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 71830443063a..488b7f797f0d 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -1,5 +1,15 @@ +import Onyx from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; import * as PolicyUtils from '@libs/PolicyUtils'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy} from '@src/types/onyx'; +import createRandomPolicy from '../utils/collections/policies'; +import * as TestHelper from '../utils/TestHelper'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; +const CARLOS_EMAIL = 'cmartins@expensifail.com'; +const CARLOS_ACCOUNT_ID = 1; function toLocaleDigitMock(dot: string): string { return dot; } @@ -81,4 +91,56 @@ describe('PolicyUtils', () => { }); }); }); + describe('getActivePolicies', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + initialKeyStates: { + [ONYXKEYS.SESSION]: {accountID: CARLOS_ACCOUNT_ID, email: CARLOS_EMAIL}, + }, + }); + }); + + beforeEach(() => { + global.fetch = TestHelper.getGlobalFetchMock(); + return Onyx.clear().then(waitForBatchedUpdates); + }); + it('should return empty array', () => { + // Given a user with a single archived paid policy. + const policies = { + // eslint-disable-next-line @typescript-eslint/naming-convention + 1: { + ...createRandomPolicy(1, CONST.POLICY.TYPE.CORPORATE), + role: '', + }, + }; + const result = PolicyUtils.getActivePolicies(policies as OnyxCollection, true); + // The result should be an empty array since we have no active policies. + expect(result.length).toBe(0); + }); + it('should return array contains policy which has id = 1', () => { + // Given a user with only a paid policy. + const randomPolicy1 = {...createRandomPolicy(1, CONST.POLICY.TYPE.CORPORATE), pendingAction: null}; + const policies = { + // eslint-disable-next-line @typescript-eslint/naming-convention + 1: randomPolicy1, + }; + const result = PolicyUtils.getActivePolicies(policies as OnyxCollection, true); + // The result should contain the mock paid policy, since it is our only active paid policy. + expect(result).toContainEqual(randomPolicy1); + }); + it('should return empty array', () => { + // Given a user with only one control workspace which is pending delete. + const policies = { + // eslint-disable-next-line @typescript-eslint/naming-convention + 1: { + ...createRandomPolicy(1, CONST.POLICY.TYPE.CORPORATE), + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + }, + }; + const result = PolicyUtils.getActivePolicies(policies as OnyxCollection, true); + // The result should be an empty array since there is only one policy which is pending deletion, so we have no active paid policies. + expect(result).toEqual([]); + }); + }); });