Skip to content

Commit d8d63ec

Browse files
committed
ENG-1723-list-group-members-backend
1 parent a5c951e commit d8d63ec

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

apps/website/app/utils/supabase/account.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1+
import type { Database } from "@repo/database/dbTypes";
12
import type { DGSupabaseClient } from "@repo/database/lib/client";
23

4+
type AgentType = Database["public"]["Enums"]["AgentType"] | "group";
5+
6+
export const getGroupMemberList = async (
7+
client: DGSupabaseClient,
8+
groupId: string,
9+
): Promise<
10+
{
11+
id: string;
12+
name: string;
13+
agentType: AgentType; // eslint-disable-line @typescript-eslint/naming-convention
14+
admin: boolean;
15+
}[]
16+
> => {
17+
// eslint-disable-next-line @typescript-eslint/naming-convention
18+
const results = await client.rpc("group_members", { p_group_id: groupId });
19+
if (results.error) throw results.error;
20+
if (!results.data) return [];
21+
return (
22+
results.data
23+
// eslint-disable-next-line @typescript-eslint/naming-convention
24+
.map(({ id, name, agent_type, admin }) => ({
25+
id,
26+
name,
27+
agentType: agent_type,
28+
admin,
29+
}))
30+
.filter(
31+
({ id, name, agentType, admin }) =>
32+
admin !== null && id !== null && name !== null && agentType !== null,
33+
) as unknown as {
34+
admin: boolean;
35+
id: string;
36+
name: string;
37+
agentType: AgentType;
38+
}[]
39+
);
40+
};
41+
342
export const createGroup = async (
443
client: DGSupabaseClient,
544
name: string,

packages/database/src/dbTypes.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,16 @@ export type Database = {
15301530
Returns: string
15311531
}
15321532
group_exists: { Args: { group_id_: string }; Returns: boolean }
1533+
group_members: {
1534+
Args: { p_group_id: string }
1535+
Returns: Database["public"]["CompositeTypes"]["group_member_info"][]
1536+
SetofOptions: {
1537+
from: "*"
1538+
to: "group_member_info"
1539+
isOneToOne: false
1540+
isSetofReturn: true
1541+
}
1542+
}
15331543
in_group: { Args: { group_id_: string }; Returns: boolean }
15341544
in_space: {
15351545
Args: {
@@ -1832,6 +1842,13 @@ export type Database = {
18321842
| Database["public"]["CompositeTypes"]["account_local_input"]
18331843
| null
18341844
}
1845+
group_member_info: {
1846+
id: number | null
1847+
name: string | null
1848+
platform: Database["public"]["Enums"]["Platform"] | null
1849+
agent_type: Database["public"]["Enums"]["AgentType"] | null
1850+
admin: boolean | null
1851+
}
18351852
inline_embedding_input: {
18361853
model: string | null
18371854
vector: number[] | null
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
CREATE OR REPLACE VIEW public.my_accounts AS
2+
SELECT
3+
id,
4+
name,
5+
platform,
6+
account_local_id,
7+
write_permission,
8+
active,
9+
agent_type,
10+
metadata,
11+
dg_account
12+
FROM public."PlatformAccount"
13+
WHERE id IN (
14+
SELECT "LocalAccess".account_id FROM public."LocalAccess"
15+
JOIN public."SpaceAccess" USING (space_id)
16+
JOIN public.my_user_accounts() ON (account_uid = my_user_accounts)
17+
WHERE permissions >= 'partial'
18+
UNION
19+
SELECT id FROM public."PlatformAccount" WHERE dg_account = auth.uid()
20+
);
21+
22+
CREATE TYPE public.group_member_info AS (
23+
id BIGINT,
24+
name VARCHAR,
25+
platform public."Platform",
26+
agent_type public."AgentType",
27+
admin boolean
28+
);
29+
30+
CREATE OR REPLACE FUNCTION public.group_members(p_group_id UUID) RETURNS SETOF public.group_member_info
31+
STABLE
32+
SET search_path = ''
33+
LANGUAGE sql AS $$
34+
SELECT pa.id, pa.name, pa.platform, pa.agent_type, gm.admin FROM public.my_accounts AS pa JOIN public.group_membership AS gm ON (pa.dg_account=gm.member_id) WHERE gm.group_id=p_group_id;
35+
$$;

packages/database/supabase/schemas/account.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,25 @@ WHERE id IN (
454454
JOIN public."SpaceAccess" USING (space_id)
455455
JOIN public.my_user_accounts() ON (account_uid = my_user_accounts)
456456
WHERE permissions >= 'partial'
457+
UNION
458+
SELECT id FROM public."PlatformAccount" WHERE dg_account = auth.uid()
457459
);
458460

461+
CREATE TYPE public.group_member_info AS (
462+
id BIGINT,
463+
name VARCHAR,
464+
platform public."Platform",
465+
agent_type public."AgentType",
466+
admin boolean
467+
);
468+
469+
CREATE OR REPLACE FUNCTION public.group_members(p_group_id UUID) RETURNS SETOF public.group_member_info
470+
STABLE
471+
SET search_path = ''
472+
LANGUAGE sql AS $$
473+
SELECT pa.id, pa.name, pa.platform, pa.agent_type, gm.admin FROM public.my_accounts AS pa JOIN public.group_membership AS gm ON (pa.dg_account=gm.member_id) WHERE gm.group_id=p_group_id;
474+
$$;
475+
459476
DROP POLICY IF EXISTS platform_account_policy ON public."PlatformAccount";
460477

461478
DROP POLICY IF EXISTS platform_account_select_policy ON public."PlatformAccount";

0 commit comments

Comments
 (0)