Skip to content

Commit 653af04

Browse files
authored
Merge pull request #53630 from FitseTLT/fix-showing-workspace-current-user-is-not-member
Fix - Room - Workspace the current user is not member appears in workspace list during room creation flow
2 parents 0d17435 + 50392ae commit 653af04

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/libs/PolicyUtils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ Onyx.connect({
6969

7070
/**
7171
* Filter out the active policies, which will exclude policies with pending deletion
72+
* and policies the current user doesn't belong to.
7273
* These are policies that we can use to create reports with in NewDot.
7374
*/
74-
function getActivePolicies(policies: OnyxCollection<Policy> | null): Policy[] {
75+
function getActivePolicies(policies: OnyxCollection<Policy> | null, currentUserLogin: string | undefined): Policy[] {
7576
return Object.values(policies ?? {}).filter<Policy>(
76-
(policy): policy is Policy => !!policy && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !!policy.name && !!policy.id,
77+
(policy): policy is Policy =>
78+
!!policy && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !!policy.name && !!policy.id && !!getPolicyRole(policy, currentUserLogin),
7779
);
7880
}
7981
/**
@@ -636,7 +638,7 @@ function getPolicy(policyID: string | undefined): OnyxEntry<Policy> {
636638

637639
/** Return active policies where current user is an admin */
638640
function getActiveAdminWorkspaces(policies: OnyxCollection<Policy> | null, currentUserLogin: string | undefined): Policy[] {
639-
const activePolicies = getActivePolicies(policies);
641+
const activePolicies = getActivePolicies(policies, currentUserLogin);
640642
return activePolicies.filter((policy) => shouldShowPolicy(policy, NetworkStore.isOffline(), currentUserLogin) && isPolicyAdmin(policy, currentUserLogin));
641643
}
642644

@@ -652,7 +654,7 @@ function canSendInvoice(policies: OnyxCollection<Policy> | null, currentUserLogi
652654
}
653655

654656
function hasWorkspaceWithInvoices(currentUserLogin: string | undefined): boolean {
655-
const activePolicies = getActivePolicies(allPolicies);
657+
const activePolicies = getActivePolicies(allPolicies, currentUserLogin);
656658
return activePolicies.some((policy) => shouldShowPolicy(policy, NetworkStore.isOffline(), currentUserLogin) && policy.areInvoicesEnabled);
657659
}
658660

src/pages/workspace/WorkspaceNewRoomPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ function WorkspaceNewRoomPage() {
6464

6565
const workspaceOptions = useMemo(
6666
() =>
67-
PolicyUtils.getActivePolicies(policies)
67+
PolicyUtils.getActivePolicies(policies, session?.email)
6868
?.filter((policy) => policy.type !== CONST.POLICY.TYPE.PERSONAL)
6969
.map((policy) => ({
7070
label: policy.name,
7171
value: policy.id,
7272
}))
7373
.sort((a, b) => localeCompare(a.label, b.label)) ?? [],
74-
[policies],
74+
[policies, session?.email],
7575
);
7676
const [policyID, setPolicyID] = useState<string>(() => {
7777
if (!!activeWorkspaceOrDefaultID && workspaceOptions.some((option) => option.value === activeWorkspaceOrDefaultID)) {

tests/unit/PolicyUtilsTest.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
import * as PolicyUtils from '@libs/PolicyUtils';
2+
import ONYXKEYS from '@src/ONYXKEYS';
3+
import type {Policy} from '@src/types/onyx';
4+
import createCollection from '../utils/collections/createCollection';
5+
import createRandomPolicy from '../utils/collections/policies';
26

37
function toLocaleDigitMock(dot: string): string {
48
return dot;
59
}
610

711
describe('PolicyUtils', () => {
12+
describe('getActivePolicies', () => {
13+
it("getActivePolicies should filter out policies that the current user doesn't belong to", () => {
14+
const policies = createCollection<Policy>(
15+
(item) => `${ONYXKEYS.COLLECTION.POLICY}${item.id}`,
16+
(index) => ({...createRandomPolicy(index + 1), ...(!index && {role: null})} as Policy),
17+
2,
18+
);
19+
expect(PolicyUtils.getActivePolicies(policies, undefined)).toHaveLength(1);
20+
});
21+
});
822
describe('getRateDisplayValue', () => {
923
it('should return an empty string for NaN', () => {
1024
const rate = PolicyUtils.getRateDisplayValue('invalid' as unknown as number, toLocaleDigitMock);

0 commit comments

Comments
 (0)