Skip to content

Commit 43e8bc3

Browse files
authored
ENG-1799 Add better error message to createGroup (#1085)
1 parent b09b689 commit 43e8bc3

5 files changed

Lines changed: 37 additions & 10 deletions

File tree

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,10 @@ export const createGroupInvitation = async ({
8585
.eq("member_id", userData.id)
8686
.maybeSingle();
8787
if (membershipReq.data?.admin !== true) return null;
88-
/* eslint-disable @typescript-eslint/naming-convention */
8988
const { data, error } = await client.rpc("create_secret_token", {
90-
/* eslint-disable @typescript-eslint/naming-convention */
9189
v_payload: { groupId, type: "groupInvitation", admin },
9290
expiry_interval: "60d",
93-
/* eslint-enable @typescript-eslint/naming-convention */
9491
});
95-
/* eslint-enable @typescript-eslint/naming-convention */
9692
if (error || !data) return null;
9793
return data;
9894
};
@@ -114,12 +110,27 @@ export const acceptGroupInvitation = async (
114110
export const createGroup = async (
115111
client: DGSupabaseClient,
116112
name: string,
117-
): Promise<string | null> => {
113+
): Promise<{ groupId: string | null; error: string | null }> => {
118114
const result = await client.functions.invoke<{ group_id: string }>(
119115
"create-group",
120116
{ body: { name } },
121117
);
122-
return result.data?.group_id || null;
118+
if (result.error) {
119+
let message =
120+
typeof result.error === "string"
121+
? result.error
122+
: (result.error as { message: string }).message;
123+
try {
124+
const body = (await (
125+
result.error as { context?: Response }
126+
).context?.json()) as { msg?: string } | undefined;
127+
if (body?.msg) message = body.msg;
128+
} catch {
129+
// ignore parse errors
130+
}
131+
return { groupId: null, error: message };
132+
}
133+
return { groupId: result.data?.group_id ?? null, error: null };
123134
};
124135

125136
export const removeFromGroup = async ({

apps/website/test/integration/groupInvitation.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ describe(
9393

9494
it("executes the full invitation flow", async () => {
9595
// Step 1: user1 creates a group
96-
const groupId = await createGroup(client1, "vitest-invite-group");
96+
const { groupId, error: createError } = await createGroup(
97+
client1,
98+
"vitest-invite-group",
99+
);
100+
expect(createError, "createGroup should not error").toBeNull();
97101
expect(groupId, "createGroup should return a group ID").toBeTruthy();
98102
createdGroupId = groupId;
99103

apps/website/test/integration/leaveGroup.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ describe("leave group flow", { tags: ["database"] }, () => {
103103

104104
it("lists group members", async () => {
105105
// Step 1: user1 creates a group
106-
const groupId = await createGroup(client1, "vitest-invite-group");
106+
const { groupId, error: createError } = await createGroup(
107+
client1,
108+
"vitest-invite-group",
109+
);
110+
assert(createError === null, createError!);
107111
assert(groupId !== null, "createGroup should return a group ID");
108112
createdGroupId = groupId;
109113

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ describe("list group members flow", { tags: ["database"] }, () => {
105105

106106
it("lists group members", async () => {
107107
// Step 1: user1 creates a group
108-
const groupId = await createGroup(client1, "vitest-invite-group");
108+
const { groupId, error: createError } = await createGroup(
109+
client1,
110+
"vitest-invite-group",
111+
);
112+
assert(createError === null, createError!);
109113
assert(groupId !== null, "createGroup should return a group ID");
110114
createdGroupId = groupId;
111115

apps/website/test/integration/listMyGroups.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ describe("list group members flow", { tags: ["database"] }, () => {
103103

104104
it("lists group members", async () => {
105105
// Step 1: user1 creates a group
106-
const groupId = await createGroup(client1, "vitest-invite-group");
106+
const { groupId, error: createError } = await createGroup(
107+
client1,
108+
"vitest-invite-group",
109+
);
110+
assert(createError === null, createError!);
107111
assert(groupId !== null, "createGroup should return a group ID");
108112
createdGroupId = groupId;
109113

0 commit comments

Comments
 (0)