Skip to content

Commit a71d96f

Browse files
committed
ENG-1723-list-group-members-backend
1 parent 485609a commit a71d96f

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,39 @@ export const getSessionUserData = async (
4444
}
4545
return { id, name: accountReq.data.name, type: "person", email };
4646
};
47+
48+
export const getGroupMemberList = async (
49+
client: DGSupabaseClient,
50+
groupId: string,
51+
): Promise<
52+
{
53+
id: string;
54+
name: string;
55+
agentType: AgentType; // eslint-disable-line @typescript-eslint/naming-convention
56+
admin: boolean;
57+
}[]
58+
> => {
59+
// eslint-disable-next-line @typescript-eslint/naming-convention
60+
const results = await client.rpc("group_members", { p_group_id: groupId });
61+
if (results.error) throw results.error;
62+
if (!results.data) return [];
63+
return (
64+
results.data
65+
// eslint-disable-next-line @typescript-eslint/naming-convention
66+
.map(({ id, name, agent_type, admin }) => ({
67+
id,
68+
name,
69+
agentType: agent_type,
70+
admin,
71+
}))
72+
.filter(
73+
({ id, name, agentType, admin }) =>
74+
admin !== null && id !== null && name !== null && agentType !== null,
75+
) as unknown as {
76+
admin: boolean;
77+
id: string;
78+
name: string;
79+
agentType: AgentType;
80+
}[]
81+
);
82+
};

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 SELECT id FROM public."PlatformAccount" WHERE dg_account=auth.uid()
19+
);
20+
21+
CREATE TYPE public.group_member_info AS (
22+
id BIGINT,
23+
name VARCHAR,
24+
platform public."Platform",
25+
agent_type public."AgentType",
26+
admin boolean
27+
);
28+
29+
CREATE OR REPLACE FUNCTION public.group_members(p_group_id UUID) RETURNS SETOF public.group_member_info
30+
STABLE
31+
SET search_path = ''
32+
LANGUAGE sql AS $$
33+
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;
34+
$$;

packages/database/supabase/schemas/account.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,24 @@ 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 SELECT id FROM public."PlatformAccount" WHERE dg_account=auth.uid()
457458
);
458459

460+
CREATE TYPE public.group_member_info AS (
461+
id BIGINT,
462+
name VARCHAR,
463+
platform public."Platform",
464+
agent_type public."AgentType",
465+
admin boolean
466+
);
467+
468+
CREATE OR REPLACE FUNCTION public.group_members(p_group_id UUID) RETURNS SETOF public.group_member_info
469+
STABLE
470+
SET search_path = ''
471+
LANGUAGE sql AS $$
472+
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;
473+
$$;
474+
459475
DROP POLICY IF EXISTS platform_account_policy ON public."PlatformAccount";
460476

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

0 commit comments

Comments
 (0)