"use strict";
/*
Get basic information about a user or organization
from the database.  This should be enough to render
a nice "homepage" for that user or organization.

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const owner_1 = __importDefault(require("./owner"));
const project_1 = __importDefault(require("lib/share/proxy/project"));
// Throws an exception if there is no project with this name.
// TODO: take into account redirects for when name is changed.
async function getProjectId(owner, project) {
    if (owner == "github") {
        return await (0, project_1.default)();
    }
    const { owner_id } = await (0, owner_1.default)(owner);
    const pool = (0, pool_1.default)("long"); // map from owner/project --> project_id unlikely to change and when it does, stale data is ok.
    // NOTE: it's not enough that owner is a collab on the project -- they have to be the owner.
    // We do users ? $2 also, since it's fast/indexed, then dig in to the JSON...
    // Note that we have put owner_id in the query string explicitly (as far as I know),
    // though it is NOT user supplied so that's safe.
    const result = await pool.query(`SELECT project_id FROM projects WHERE LOWER(name)=$1 AND users ? $2 AND users#>>'{${owner_id},group}' = 'owner'`, [project.toLowerCase(), owner_id]);
    if (result.rows.length > 0) {
        return result.rows[0].project_id;
    }
    throw Error(`no project ${owner}/${project}`);
}
exports.default = getProjectId;
//# sourceMappingURL=project.js.map