Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Hmm it's not here in RHP is displayed after deleting a workspace #53816

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
18 changes: 16 additions & 2 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<Policy> | null): Policy[] {
function getActivePolicies(policies: OnyxCollection<Policy> | null, excludePersonalPolicy = false): Policy[] {
return Object.values(policies ?? {}).filter<Policy>(
(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),
);
}
/**
Expand Down
4 changes: 1 addition & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/pages/workspace/WorkspaceNewRoomPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]';
const CARLOS_ACCOUNT_ID = 1;
function toLocaleDigitMock(dot: string): string {
return dot;
}
Expand Down Expand Up @@ -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<Policy>, true);
// The result should be empty array since the policies contains only archived paid policy.
truph01 marked this conversation as resolved.
Show resolved Hide resolved
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<Policy>, true);
// The result should contain the mock paid policy.
truph01 marked this conversation as resolved.
Show resolved Hide resolved
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<Policy>, true);
// The result should be empty array since there is only one policy which has pendingAction is 'delete'.
truph01 marked this conversation as resolved.
Show resolved Hide resolved
expect(result).toEqual([]);
});
});
});
Loading