-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(groups): split useUserGroups' {all} boolean into two queries #515
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { renderHook, waitFor } from "@testing-library/react"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { allGroupsQuery } from "./useAllGroups"; | ||
| import { createQueryWrapper } from "@/test/integration/harness"; | ||
| import { signInAsTestUser } from "@/test/integration/fixtures/auth"; | ||
| import { grantAdminRole } from "@/test/integration/fixtures/adminRoles"; | ||
| import { | ||
| addGroupMember, | ||
| createGroup, | ||
| } from "@/test/integration/fixtures/groups"; | ||
|
|
||
| describe("allGroupsQuery", () => { | ||
| it("falls back to member-only results for a non-admin caller", async () => { | ||
| const userId = await signInAsTestUser(); | ||
| const myGroupId = await createGroup(userId); | ||
| await addGroupMember(myGroupId, userId); | ||
| // A group the caller has no membership row in at all. | ||
| const otherGroupId = await createGroup(crypto.randomUUID()); | ||
| await addGroupMember(otherGroupId, crypto.randomUUID()); | ||
|
|
||
| const { result } = renderHook(() => useQuery(allGroupsQuery(userId)), { | ||
| wrapper: createQueryWrapper(), | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(result.current.data?.map((group) => group.id)).toEqual([myGroupId]); | ||
| }); | ||
|
|
||
| it("returns every non-archived group for a super-admin caller, with is_member computed per group", async () => { | ||
| const userId = await signInAsTestUser(); | ||
| await grantAdminRole(userId, "super_admin"); | ||
| const myGroupId = await createGroup(userId); | ||
| await addGroupMember(myGroupId, userId); | ||
| const otherGroupId = await createGroup(crypto.randomUUID()); | ||
| await addGroupMember(otherGroupId, crypto.randomUUID()); | ||
|
|
||
| const { result } = renderHook(() => useQuery(allGroupsQuery(userId)), { | ||
| wrapper: createQueryWrapper(), | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| const myGroup = result.current.data?.find((g) => g.id === myGroupId); | ||
| const otherGroup = result.current.data?.find((g) => g.id === otherGroupId); | ||
| expect(myGroup?.is_member).toBe(true); | ||
| expect(otherGroup).toBeDefined(); | ||
| expect(otherGroup?.is_member).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { queryOptions } from "@tanstack/react-query"; | ||
| import { supabase } from "@/integrations/supabase/client"; | ||
| import type { Group } from "./types"; | ||
| import { groupsKeys } from "./types"; | ||
| import { attachGroupMeta, fetchMyGroups, getUserGroupIds } from "./useMyGroups"; | ||
|
|
||
| export function allGroupsQuery(userId: string) { | ||
| return queryOptions({ | ||
| queryKey: groupsKeys.allGroups(userId), | ||
| queryFn: () => fetchAllGroups(userId), | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Every non-archived group, for a super-admin caller (the only role `groups` RLS | ||
| * lets read them all). Any other caller falls | ||
| * back to exactly the member-only result (never an error, never a leaked | ||
| * full group list). | ||
| */ | ||
| async function fetchAllGroups(userId: string): Promise<Group[]> { | ||
| const canViewAll = await isSuperAdmin(userId); | ||
|
|
||
| if (!canViewAll) { | ||
| return fetchMyGroups(userId); | ||
| } | ||
|
|
||
| const [userGroupIds, { data: groupsData, error }] = await Promise.all([ | ||
| getUserGroupIds(userId), | ||
| supabase | ||
| .from("groups") | ||
| .select("*") | ||
| .eq("archived", false) | ||
| .order("created_at", { ascending: false }), | ||
| ]); | ||
|
|
||
| if (error) { | ||
| throw new Error(error.message || "Failed to fetch groups"); | ||
| } | ||
|
|
||
| return attachGroupMeta(groupsData || [], userId, userGroupIds); | ||
| } | ||
|
|
||
| async function isSuperAdmin(userId: string): Promise<boolean> { | ||
| const { data, error } = await supabase.rpc("has_admin_role", { | ||
| check_user_id: userId, | ||
| check_role: "super_admin", | ||
| }); | ||
|
|
||
| if (error) { | ||
| console.error("Error checking super admin role:", error); | ||
| return false; | ||
| } | ||
|
|
||
| return data === true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { renderHook, waitFor } from "@testing-library/react"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { myGroupsQuery } from "./useMyGroups"; | ||
| import { createQueryWrapper } from "@/test/integration/harness"; | ||
| import { signInAsTestUser } from "@/test/integration/fixtures/auth"; | ||
| import { | ||
| addGroupMember, | ||
| createGroup, | ||
| } from "@/test/integration/fixtures/groups"; | ||
|
|
||
| describe("myGroupsQuery", () => { | ||
| it("returns only the groups the caller is a member of", async () => { | ||
| const userId = await signInAsTestUser(); | ||
| const myGroupId = await createGroup(userId); | ||
| await addGroupMember(myGroupId, userId); | ||
| // A group the caller has no membership row in at all. | ||
| const otherGroupId = await createGroup(crypto.randomUUID()); | ||
| await addGroupMember(otherGroupId, crypto.randomUUID()); | ||
|
|
||
| const { result } = renderHook(() => useQuery(myGroupsQuery(userId)), { | ||
| wrapper: createQueryWrapper(), | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(result.current.data?.map((group) => group.id)).toEqual([myGroupId]); | ||
| }); | ||
|
|
||
| it("marks is_member true and is_creator correctly, without an admin check", async () => { | ||
| const userId = await signInAsTestUser(); | ||
| const ownGroupId = await createGroup(userId); | ||
| await addGroupMember(ownGroupId, userId); | ||
| const joinedGroupId = await createGroup(crypto.randomUUID()); | ||
| await addGroupMember(joinedGroupId, userId); | ||
|
|
||
| const { result } = renderHook(() => useQuery(myGroupsQuery(userId)), { | ||
| wrapper: createQueryWrapper(), | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| const ownGroup = result.current.data?.find((g) => g.id === ownGroupId); | ||
| const joinedGroup = result.current.data?.find( | ||
| (g) => g.id === joinedGroupId, | ||
| ); | ||
| expect(ownGroup).toMatchObject({ is_member: true, is_creator: true }); | ||
| expect(joinedGroup).toMatchObject({ is_member: true, is_creator: false }); | ||
| }); | ||
|
|
||
| it("reflects every member in member_count, not just the caller", async () => { | ||
| const userId = await signInAsTestUser(); | ||
| const groupId = await createGroup(userId); | ||
| await addGroupMember(groupId, userId); | ||
| await addGroupMember(groupId, crypto.randomUUID()); | ||
|
|
||
| const { result } = renderHook(() => useQuery(myGroupsQuery(userId)), { | ||
| wrapper: createQueryWrapper(), | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect( | ||
| result.current.data?.find((g) => g.id === groupId)?.member_count, | ||
| ).toBe(2); | ||
| }); | ||
|
|
||
| it("returns an empty array for a user with no groups", async () => { | ||
| const userId = await signInAsTestUser(); | ||
|
|
||
| const { result } = renderHook(() => useQuery(myGroupsQuery(userId)), { | ||
| wrapper: createQueryWrapper(), | ||
| }); | ||
|
|
||
| await waitFor(() => expect(result.current.isSuccess).toBe(true)); | ||
|
|
||
| expect(result.current.data).toEqual([]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comment