Skip to content

Commit b09b689

Browse files
authored
ENG-1796 Duplicate entries in spaces_in_group function (#1077)
1 parent 1f8b8fa commit b09b689

5 files changed

Lines changed: 61 additions & 66 deletions

File tree

apps/website/test/integration/listGroupMembers.test.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "assert";
22
import { describe, it, beforeAll, afterAll } from "vitest";
33
import { createClient } from "@supabase/supabase-js";
4-
import type { Database } from "@repo/database/dbTypes";
4+
import type { Database, Tables } from "@repo/database/dbTypes";
55
import type { DGSupabaseClient } from "@repo/database/lib/client";
66
import {
77
fetchOrCreateSpaceDirect,
@@ -14,7 +14,7 @@ const ANON_KEY = process.env.SUPABASE_PUBLISHABLE_KEY!;
1414
const SERVICE_KEY = process.env.SUPABASE_SECRET_KEY!;
1515
const PASSWORD = "abcdefgh";
1616

17-
type GroupSpaceInfo = Database["public"]["CompositeTypes"]["group_space_info"];
17+
type PseudoAccountInfo = Tables<"my_pseudo_accounts">;
1818

1919
const freshClient = (): DGSupabaseClient =>
2020
createClient<Database, "public">(SUPABASE_URL, ANON_KEY);
@@ -121,19 +121,17 @@ describe("list group members flow", { tags: ["database"] }, () => {
121121

122122
const expectedSpaceIds = [spaceId1, spaceId2];
123123
// Step 3: user1 lists group members
124-
const { data: data1, error: error1 } = await client1.rpc(
125-
"spaces_in_group",
126-
{
127-
p_group_id: createdGroupId, // eslint-disable-line @typescript-eslint/naming-convention
128-
},
129-
);
124+
const { data: data1, error: error1 } = await client1
125+
.from("my_pseudo_accounts")
126+
.select()
127+
.eq("group_id", createdGroupId);
130128

131129
assert(error1 === null, error1 ? error1.message : "");
132130
assert(data1 !== null, "group spaces should not be empty");
133131
assert(data1.length === 2, "There should be two spaces");
134132
const spacesSeenBy1 = Object.fromEntries(
135-
data1.filter((gm) => gm.id !== null).map((gm) => [gm.id, gm]),
136-
) as Record<number, GroupSpaceInfo>;
133+
data1.filter((gm) => gm.space_id !== null).map((gm) => [gm.space_id, gm]),
134+
) as Record<number, PseudoAccountInfo>;
137135
assert(
138136
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
139137
expectedSpaceIds.every((id) => spacesSeenBy1[id] !== undefined),
@@ -145,17 +143,15 @@ describe("list group members flow", { tags: ["database"] }, () => {
145143
),
146144
);
147145
// Step 4: user2 lists group members
148-
const { data: data2, error: error2 } = await client2.rpc(
149-
"spaces_in_group",
150-
{
151-
p_group_id: createdGroupId, // eslint-disable-line @typescript-eslint/naming-convention
152-
},
153-
);
146+
const { data: data2, error: error2 } = await client2
147+
.from("my_pseudo_accounts")
148+
.select()
149+
.eq("group_id", createdGroupId);
154150
assert(error2 === null, error2 ? error2.message : "");
155151
assert(data2 !== null, "group spaces should not be empty");
156152
assert(data2.length === 2, "There should be two spaces");
157153
const spacesSeenBy2 = new Set(
158-
data2.map((gm) => gm.id).filter((id) => id !== null),
154+
data2.map((gm) => gm.space_id).filter((id) => id !== null),
159155
);
160156
assert(
161157
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
@@ -173,19 +169,19 @@ describe("list group members flow", { tags: ["database"] }, () => {
173169
});
174170
assert(!errorPublishSpace2);
175171
// Step 6: that space is now seen as published by 1.
176-
const { data: data1b, error: error1b } = await client1.rpc(
177-
"spaces_in_group",
178-
{
179-
p_group_id: createdGroupId, // eslint-disable-line @typescript-eslint/naming-convention
180-
},
181-
);
172+
const { data: data1b, error: error1b } = await client1
173+
.from("my_pseudo_accounts")
174+
.select()
175+
.eq("group_id", createdGroupId);
182176

183177
assert(error1b === null, error1b ? error1b.message : "");
184178
assert(data1b !== null, "group spaces should not be empty");
185179
assert(data1b.length === 2, "There should be two spaces");
186180
const spacesSeenBy1b = Object.fromEntries(
187-
data1b.filter((gm) => gm.id !== null).map((gm) => [gm.id, gm]),
188-
) as Record<number, GroupSpaceInfo>;
181+
data1b
182+
.filter((gm) => gm.space_id !== null)
183+
.map((gm) => [gm.space_id, gm]),
184+
) as Record<number, PseudoAccountInfo>;
189185
assert(
190186
spacesSeenBy1b[spaceId2]?.sharing_permissions,
191187
"Second space should now be seen as shared",

packages/database/src/dbTypes.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,9 @@ export type Database = {
13221322
}
13231323
my_pseudo_accounts: {
13241324
Row: {
1325+
admin: boolean | null
13251326
dg_account: string | null
1327+
group_id: string | null
13261328
id: number | null
13271329
name: string | null
13281330
platform: Database["public"]["Enums"]["Platform"] | null
@@ -1332,6 +1334,13 @@ export type Database = {
13321334
space_id: number | null
13331335
}
13341336
Relationships: [
1337+
{
1338+
foreignKeyName: "group_membership_group_id_fkey"
1339+
columns: ["group_id"]
1340+
isOneToOne: false
1341+
referencedRelation: "my_groups"
1342+
referencedColumns: ["id"]
1343+
},
13351344
{
13361345
foreignKeyName: "PlatformAccount_dg_account_fkey"
13371346
columns: ["dg_account"]
@@ -1803,16 +1812,6 @@ export type Database = {
18031812
isSetofReturn: true
18041813
}
18051814
}
1806-
spaces_in_group: {
1807-
Args: { p_group_id: string }
1808-
Returns: Database["public"]["CompositeTypes"]["group_space_info"][]
1809-
SetofOptions: {
1810-
from: "*"
1811-
to: "group_space_info"
1812-
isOneToOne: false
1813-
isSetofReturn: true
1814-
}
1815-
}
18161815
unowned_account_in_shared_space: {
18171816
Args: { p_account_id: number }
18181817
Returns: boolean
@@ -1983,15 +1982,6 @@ export type Database = {
19831982
| Database["public"]["CompositeTypes"]["account_local_input"]
19841983
| null
19851984
}
1986-
group_space_info: {
1987-
id: number | null
1988-
name: string | null
1989-
platform: Database["public"]["Enums"]["Platform"] | null
1990-
sharing_permissions:
1991-
| Database["public"]["Enums"]["SpaceAccessPermissions"]
1992-
| null
1993-
admin: boolean | null
1994-
}
19951985
inline_embedding_input: {
19961986
model: string | null
19971987
vector: number[] | null
@@ -2178,3 +2168,4 @@ export const Constants = {
21782168
},
21792169
},
21802170
} as const
2171+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
DROP VIEW public.my_pseudo_accounts;
2+
3+
CREATE OR REPLACE VIEW public.my_pseudo_accounts AS
4+
SELECT
5+
pa.id,
6+
pa.platform,
7+
pa.dg_account,
8+
gm.group_id,
9+
gm.admin,
10+
sa.space_id,
11+
sp.name,
12+
grpsa.permissions AS sharing_permissions
13+
FROM public."PlatformAccount" AS pa
14+
JOIN public.group_membership AS gm ON (member_id = pa.dg_account)
15+
JOIN public.group_membership AS gm2 ON (gm2.member_id = auth.uid() AND gm2.group_id = gm.group_id)
16+
JOIN public."SpaceAccess" AS sa ON (sa.account_uid = pa.dg_account)
17+
JOIN public."Space" AS sp ON (sp.id = sa.space_id)
18+
LEFT OUTER JOIN public."SpaceAccess" AS grpsa ON (grpsa.account_uid = gm.group_id AND grpsa.space_id = sp.id)
19+
WHERE pa.agent_type = 'anonymous' AND sa.permissions = 'editor';
20+
21+
DROP FUNCTION public.spaces_in_group;
22+
23+
DROP TYPE public.group_space_info;

packages/database/supabase/schemas/account.sql

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -463,34 +463,19 @@ SELECT
463463
pa.id,
464464
pa.platform,
465465
pa.dg_account,
466+
gm.group_id,
467+
gm.admin,
466468
sa.space_id,
467469
sp.name,
468-
mysa.permissions AS sharing_permissions
470+
grpsa.permissions AS sharing_permissions
469471
FROM public."PlatformAccount" AS pa
470-
JOIN public.group_membership AS gm ON (member_id = dg_account)
472+
JOIN public.group_membership AS gm ON (member_id = pa.dg_account)
471473
JOIN public.group_membership AS gm2 ON (gm2.member_id = auth.uid() AND gm2.group_id = gm.group_id)
472-
JOIN public."SpaceAccess" AS sa ON (sa.account_uid = dg_account)
474+
JOIN public."SpaceAccess" AS sa ON (sa.account_uid = pa.dg_account)
473475
JOIN public."Space" AS sp ON (sp.id = sa.space_id)
474-
LEFT OUTER JOIN public."SpaceAccess" AS mysa ON (mysa.account_uid = gm.group_id AND mysa.space_id = sp.id)
476+
LEFT OUTER JOIN public."SpaceAccess" AS grpsa ON (grpsa.account_uid = gm.group_id AND grpsa.space_id = sp.id)
475477
WHERE pa.agent_type = 'anonymous' AND sa.permissions = 'editor';
476478

477-
CREATE TYPE public.group_space_info AS (
478-
id BIGINT,
479-
name VARCHAR,
480-
platform public."Platform",
481-
sharing_permissions public."SpaceAccessPermissions",
482-
admin boolean
483-
);
484-
485-
CREATE OR REPLACE FUNCTION public.spaces_in_group(p_group_id UUID) RETURNS SETOF public.group_space_info
486-
STABLE
487-
SET search_path = ''
488-
LANGUAGE sql AS $$
489-
SELECT pa.space_id as id, pa.name, pa.platform, pa.sharing_permissions, gm.admin
490-
FROM public.my_pseudo_accounts AS pa
491-
JOIN public.group_membership AS gm ON (gm.member_id = pa.dg_account)
492-
WHERE gm.group_id = p_group_id;
493-
$$;
494479

495480
CREATE OR REPLACE FUNCTION public.accept_group_invitation(token varchar) RETURNS boolean
496481
SET search_path = '' SECURITY DEFINER

packages/database/supabase/schemas/secret_tokens.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ BEGIN
3232
END;
3333
$$;
3434

35-
ALTER TABLE secret_token OWNER TO "postgres";
35+
ALTER TABLE public.secret_token OWNER TO "postgres";
3636

3737
REVOKE ALL ON TABLE public.secret_token FROM anon;
3838
GRANT ALL ON TABLE public.secret_token TO authenticated;

0 commit comments

Comments
 (0)