Skip to content

Commit 485609a

Browse files
committed
Move getSessionUserData to another file
1 parent cc6fb26 commit 485609a

3 files changed

Lines changed: 47 additions & 46 deletions

File tree

apps/website/app/components/auth/ListGroups.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { createClient } from "~/utils/supabase/client";
4-
import { getSessionUserData } from "~/utils/supabase/dbUtils";
4+
import { getSessionUserData } from "~/utils/supabase/account";
55
import { useState, useEffect } from "react";
66
import { Tables } from "@repo/database/dbTypes";
77
import useInternalError from "~/utils/internalError";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { Database } from "@repo/database/dbTypes";
2+
import type { DGSupabaseClient } from "@repo/database/lib/client";
3+
4+
type AgentType = Database["public"]["Enums"]["AgentType"] | "group";
5+
6+
export const getSessionUserData = async (
7+
client: DGSupabaseClient,
8+
): Promise<{
9+
id: string;
10+
name: string;
11+
type: AgentType;
12+
email?: string;
13+
} | null> => {
14+
const session = await client.auth.getSession();
15+
if (!session?.data?.session?.user) return null;
16+
const { id, email } = session.data.session.user;
17+
if (email) {
18+
const [name, host] = email.split("@") as [string, string];
19+
if (host === "database.discoursegraphs.com" && name.endsWith("-anon")) {
20+
const parts = name.split("-");
21+
const spaceId = Number.parseInt(parts[1]!);
22+
const spaceReq = await client
23+
.from("Space")
24+
.select("name")
25+
.eq("id", spaceId)
26+
.maybeSingle();
27+
if (spaceReq.error || !spaceReq.data) {
28+
return null;
29+
}
30+
return { name: spaceReq.data.name, id, type: "anonymous", email };
31+
}
32+
if (host === "groups.discoursegraphs.com") {
33+
return { name, id, email, type: "group" };
34+
}
35+
}
36+
const accountReq = await client
37+
.from("PlatformAccount")
38+
.select("name")
39+
.eq("dg_account", session.data.session.user.id)
40+
.eq("agent_type", "person")
41+
.maybeSingle();
42+
if (accountReq.error || !accountReq.data) {
43+
return null;
44+
}
45+
return { id, name: accountReq.data.name, type: "person", email };
46+
};

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import type {
55
} from "@supabase/supabase-js";
66
import { PostgrestError } from "@supabase/supabase-js";
77
import type { Database } from "@repo/database/dbTypes";
8-
import type { DGSupabaseClient } from "@repo/database/lib/client";
9-
10-
type AgentType = Database["public"]["Enums"]["AgentType"] | "group";
118

129
type PublicTableName = keyof Database["public"]["Tables"];
1310
type RawTables<TN extends PublicTableName> =
@@ -508,45 +505,3 @@ export const processAndInsertBatch = async <
508505
}
509506
return { ...result, data: processedResults };
510507
};
511-
512-
export const getSessionUserData = async (
513-
client: DGSupabaseClient,
514-
): Promise<{
515-
id: string;
516-
name: string;
517-
type: AgentType;
518-
email?: string;
519-
} | null> => {
520-
const session = await client.auth.getSession();
521-
if (!session?.data?.session?.user) return null;
522-
const { id, email } = session.data.session.user;
523-
if (email) {
524-
const [name, host] = email.split("@") as [string, string];
525-
if (host === "database.discoursegraphs.com" && name.endsWith("-anon")) {
526-
const parts = name.split("-");
527-
const spaceId = Number.parseInt(parts[1]!);
528-
const spaceReq = await client
529-
.from("Space")
530-
.select("name")
531-
.eq("id", spaceId)
532-
.maybeSingle();
533-
if (spaceReq.error || !spaceReq.data) {
534-
return null;
535-
}
536-
return { name: spaceReq.data.name, id, type: "anonymous", email };
537-
}
538-
if (host === "groups.discoursegraphs.com") {
539-
return { name, id, email, type: "group" };
540-
}
541-
}
542-
const accountReq = await client
543-
.from("PlatformAccount")
544-
.select("name")
545-
.eq("dg_account", session.data.session.user.id)
546-
.eq("agent_type", "person")
547-
.maybeSingle();
548-
if (accountReq.error || !accountReq.data) {
549-
return null;
550-
}
551-
return { id, name: accountReq.data.name, type: "person", email };
552-
};

0 commit comments

Comments
 (0)