"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 });
exports.getOwnerName = void 0;
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const name_rules_1 = require("@cocalc/util/db-schema/name-rules");
async function getOwner(owner) {
    if ((0, name_rules_1.isReserved)(owner)) {
        // can't be so save us the trouble of consulting database; these are common
        // so this is a good win.
        throw Error(`no such owner '${owner}'- reserved name`);
    }
    const pool = (0, pool_1.default)("long");
    // Is it an account?
    let result = await pool.query("SELECT account_id FROM accounts WHERE LOWER(name)=$1", [owner.toLowerCase()]);
    if (result.rows.length > 0) {
        return { type: "account", owner_id: result.rows[0].account_id };
    }
    // Is it an organization?
    result = await pool.query("SELECT title, description, organization_id FROM organizations WHERE LOWER(name)=$1", [owner.toLowerCase()]);
    if (result.rows.length > 0) {
        return { type: "organization", owner_id: result.rows[0].organization_id };
    }
    throw Error(`no account or organization '${owner}'`);
}
exports.default = getOwner;
async function getOwnerName(owner_id) {
    const pool = (0, pool_1.default)("long");
    let result = await pool.query("SELECT name FROM accounts WHERE account_id=$1", [owner_id]);
    if (result.rows.length > 0) {
        const { name } = result.rows[0];
        if (!name)
            return;
        return name;
    }
    result = await pool.query("SELECT name FROM organizations WHERE organization_id=$1", [owner_id]);
    if (result.rows.length > 0) {
        const { name } = result.rows[0];
        if (!name)
            return;
        return name;
    }
}
exports.getOwnerName = getOwnerName;
//# sourceMappingURL=owner.js.map