-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaccount.ts
More file actions
150 lines (142 loc) · 4.34 KB
/
Copy pathaccount.ts
File metadata and controls
150 lines (142 loc) · 4.34 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { Database } from "@repo/database/dbTypes";
import type { DGSupabaseClient } from "@repo/database/lib/client";
type AgentType = Database["public"]["Enums"]["AgentType"] | "group";
export const getSessionBaseUserData = 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" };
}
}
return { id, type: "person", email };
};
export const getSessionUserData = async (
client: DGSupabaseClient,
): Promise<{
id: string;
name: string;
type: AgentType;
email?: string;
} | null> => {
const data = await getSessionBaseUserData(client);
if (data === null) return null;
if (!data.name && data.type === "person") {
const accountReq = await client
.from("PlatformAccount")
.select("name")
.eq("dg_account", data.id)
.eq("agent_type", "person")
.maybeSingle();
if (accountReq.error || !accountReq.data?.name) {
return null;
}
return { ...data, name: accountReq.data.name };
}
const { name } = data;
if (name === undefined) return null;
if (data.name === undefined) return null;
return { ...data, name };
};
export const createGroupInvitation = async ({
client,
groupId,
admin = false,
}: {
client: DGSupabaseClient;
groupId: string;
admin?: boolean;
}): Promise<string | null> => {
const userData = await getSessionBaseUserData(client);
if (!userData) return null;
const membershipReq = await client
.from("group_membership")
.select("admin")
.eq("group_id", groupId)
.eq("member_id", userData.id)
.maybeSingle();
if (membershipReq.data?.admin !== true) return null;
/* eslint-disable @typescript-eslint/naming-convention */
const { data, error } = await client.rpc("create_secret_token", {
/* eslint-disable @typescript-eslint/naming-convention */
v_payload: { groupId, type: "groupInvitation", admin },
expiry_interval: "60d",
/* eslint-enable @typescript-eslint/naming-convention */
});
/* eslint-enable @typescript-eslint/naming-convention */
if (error || !data) return null;
return data;
};
export const acceptGroupInvitation = async (
client: DGSupabaseClient,
token: string,
): Promise<string | null> => {
const userData = await getSessionBaseUserData(client);
if (!userData) return "Not logged in";
const { data, error } = await client.rpc("accept_group_invitation", {
token,
});
if (error) return error.message || "Unknown error";
if (!data) return "Unable to accept invitation";
return null;
};
export const createGroup = async (
client: DGSupabaseClient,
name: string,
): Promise<string | null> => {
const result = await client.functions.invoke<{ group_id: string }>(
"create-group",
{ body: { name } },
);
return result.data?.group_id || null;
};
export const removeFromGroup = async ({
client,
groupId,
memberId,
}: {
client: DGSupabaseClient;
groupId: string;
memberId?: string;
}): Promise<string | null> => {
if (memberId === undefined) {
const userData = await getSessionBaseUserData(client);
memberId = userData?.id ?? undefined;
if (memberId === undefined) return "Not logged in";
}
const response = await client
.from("group_membership")
.delete()
.eq("member_id", memberId)
.eq("group_id", groupId)
.select();
if (response.error) return response.error.message;
if (response.data === null || response.data.length === 0)
return "No such record";
return null; // success
};