-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaccount.ts
More file actions
61 lines (58 loc) · 1.88 KB
/
Copy pathaccount.ts
File metadata and controls
61 lines (58 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { Database } from "@repo/database/dbTypes";
import type { DGSupabaseClient } from "@repo/database/lib/client";
type AgentType = Database["public"]["Enums"]["AgentType"] | "group";
export const getSessionUserData = async (
client: DGSupabaseClient,
): Promise<{
id: string;
name: string;
type: AgentType;
email?: string;
} | null> => {
const { data, error } = await client.auth.getUser();
if (error || !data?.user) return null;
const userData = data.user;
if (typeof userData.id !== "string") return null;
const { id, email }: { id: string; email?: string } = userData;
if (email) {
const [name, host] = email.split("@") as [string, string];
if (host === "database.discoursegraphs.com" && name.endsWith("-anon")) {
const parts = name.split("-");
const spaceId = Number.parseInt(parts[1]!);
if (Number.isNaN(spaceId)) return null;
const spaceReq = await client
.from("Space")
.select("name")
.eq("id", spaceId)
.maybeSingle();
if (spaceReq.error || !spaceReq.data) {
return null;
}
return { name: spaceReq.data.name, id, type: "anonymous", email };
}
if (host === "groups.discoursegraphs.com") {
return { name, id, email, type: "group" };
}
}
const accountReq = await client
.from("PlatformAccount")
.select("name")
.eq("dg_account", id)
.eq("agent_type", "person")
.maybeSingle();
if (accountReq.error || !accountReq.data) {
return null;
}
return { id, name: accountReq.data.name, type: "person", email };
};
export const createGroup = async (
client: DGSupabaseClient,
name: string,
): Promise<string | null> => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const result = await client.functions.invoke<{ group_id: string }>(
"create-group",
{ body: { name } },
);
return result.data?.group_id || null;
};