Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/project/get-owner.ts
5808 views
1
import getPool from "@cocalc/database/pool";
2
3
type Users = { [account_id: string]: { group?: string } };
4
5
async function getProjectUsers(project_id: string): Promise<Users> {
6
const pool = getPool("minutes");
7
const result = await pool.query(
8
"SELECT users FROM projects WHERE project_id=$1",
9
[project_id],
10
);
11
if (result.rows.length === 0) {
12
throw Error(`no project with id ${project_id}`);
13
}
14
const { users } = result.rows[0] ?? {};
15
return users ?? {};
16
}
17
18
function collectOwnerIds(users: Users): string[] {
19
const owners: string[] = [];
20
for (const account_id in users) {
21
if (users[account_id]?.group === "owner") {
22
owners.push(account_id);
23
}
24
}
25
return owners;
26
}
27
28
// Returns account_id or organization_id of the first owner found for this project.
29
// NOTE: Projects may have multiple owners; use getOwners() to get all owners.
30
export default async function getOwner(project_id: string): Promise<string> {
31
const owners = await getOwners(project_id);
32
return owners[0];
33
}
34
35
// Returns all account_ids of owners for this project.
36
export async function getOwners(project_id: string): Promise<string[]> {
37
const owners = collectOwnerIds(await getProjectUsers(project_id));
38
if (owners.length === 0) {
39
throw Error(`project ${project_id} has no owner`);
40
}
41
return owners;
42
}
43
44