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 - Room - Workspace the current user is not member appears in workspace list during room creation flow #53630

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
10 changes: 6 additions & 4 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Policy> | null): Policy[] {
function getActivePolicies(policies: OnyxCollection<Policy> | null, currentUserLogin: string | undefined): Policy[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't modify this function. Instead use shouldShowPolicy. Maybe implement function like getActiveAdminWorkspaces to get filtered workspace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? I have checked all instances where we have used getActivePolicies and additional check of roles is already the requirement. shouldShowPolicy is not the correct choice because it returns pending delete policies when offline. There is no way a policy where the current user has no role can be considered an active policy and updating getActivePolicies will avoid similar bugs in the future too.

Copy link
Contributor

@shubham1206agra shubham1206agra Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change shouldShowPolicy to include logic from getActivePolicies and remove the getActivePolicies function altogether then.

it returns pending delete policies when offline

True, but we can control this via parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the advantage of doing that:
getActivePolicies accepts policies list and returns only policies that are active while shouldShowPolicy determines whether the policy should be shown or not (which allows pending delete policy when offline) and using additional param to use it for other purpose doesn't look like better approach to me a function should ideally serve one functionality.
@cristipaval WDYT about the above discussion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is important to maintain the single responsibility principle for the functions. So yes, I think your suggestion makes sense, @FitseTLT. On the other hand, I think the function name might be a bit confusing, and I can see @shubham1206agra's concern. Maybe we could update the function doc and mention that we also filter based on the role since the users can have in Onyx policies that they are not members of.

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 && !!getPolicyRole(policy, currentUserLogin),
);
}
/**
Expand Down Expand Up @@ -636,7 +638,7 @@ function getPolicy(policyID: string | undefined): OnyxEntry<Policy> {

/** Return active policies where current user is an admin */
function getActiveAdminWorkspaces(policies: OnyxCollection<Policy> | 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));
}

Expand All @@ -652,7 +654,7 @@ function canSendInvoice(policies: OnyxCollection<Policy> | 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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/WorkspaceNewRoomPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(() => {
if (!!activeWorkspaceOrDefaultID && workspaceOptions.some((option) => option.value === activeWorkspaceOrDefaultID)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -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<Policy>(
(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);
Expand Down
Loading