UNPKG

@cocalc/server

Version:

CoCalc server functionality: functions used by either the hub and the next.js server

47 lines 1.96 kB
"use strict"; /* Returns information about a given license, which the user with the given account is *allowed* to get. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isManager = void 0; const pool_1 = __importDefault(require("@cocalc/database/pool")); const util_1 = require("@cocalc/database/postgres/util"); const analytics_1 = require("@cocalc/database/postgres/site-license/analytics"); const misc_1 = require("@cocalc/util/misc"); async function isManager(license_id, account_id) { if (!(0, misc_1.isValidUUID)(account_id)) { return false; } const pool = (0, pool_1.default)("short"); const { rows } = await pool.query("SELECT COUNT(*)::INT AS count FROM site_licenses WHERE id=$1 AND $2=ANY(managers)", [license_id, account_id]); return rows[0].count > 0; } exports.isManager = isManager; async function getLicense(license_id, account_id) { const pool = (0, pool_1.default)(); const is_manager = await isManager(license_id, account_id); const query = is_manager ? `SELECT id, title, description, expires, activates, last_used, managers, upgrades, quota, run_limit, info FROM site_licenses WHERE $1=id` : `SELECT title, expires, activates, upgrades, quota, run_limit FROM site_licenses WHERE $1=id`; const { rows } = await pool.query(query, [license_id]); if (rows.length == 0) { throw Error(`no license with id ${license_id}`); } (0, util_1.toEpoch)(rows, ["expires", "activates", "last_used"]); rows[0].is_manager = is_manager; if (is_manager) { const nr = await pool.query((0, analytics_1.numberRunningQuery)(license_id)); rows[0].number_running = nr.rows[0].count; } return rows[0]; } exports.default = getLicense; //# sourceMappingURL=get-license.js.map