CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/project/get-owner.ts
Views: 687
1
import getPool from "@cocalc/database/pool";
2
3
// Returns account_id or organization_id of the owner of this project.
4
export default async function getOwner(project_id: string): Promise<string> {
5
const pool = getPool("minutes"); // we don't even have a way to change the owner ever in cocalc.
6
7
// TODO: this seems *really* stupid/inefficient in general, e.g., what if
8
// there are 1000 users? I don't know JSONB PostgreSQL enough to come up
9
// with a better query...
10
const result = await pool.query(
11
"SELECT users FROM projects WHERE project_id=$1",
12
[project_id]
13
);
14
if (result.rows.length == 0) {
15
throw Error(`no project with id ${project_id}`);
16
}
17
const { users } = result.rows[0] ?? {};
18
for (const account_id in users) {
19
if (users[account_id].group == "owner") {
20
return account_id;
21
}
22
}
23
throw Error(`project ${project_id} has no owner`);
24
}
25
26