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