@cocalc/database
Version:
CoCalc: code for working with our PostgreSQL database
54 lines • 2.01 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.site_license_manager_set = exports.site_license_is_manager = void 0;
const query_1 = require("../query");
const misc_1 = require("@cocalc/util/misc");
async function site_license_is_manager(db, account_id, license_id) {
return ((await (0, query_1.query)({
db,
query: "SELECT COUNT(*)::INT FROM site_licenses WHERE id=$1 AND $2 = ANY(managers)",
one: true,
params: [license_id, account_id],
})).count > 0);
}
exports.site_license_is_manager = site_license_is_manager;
async function site_license_manager_set(db, account_id, info) {
// First make sure they really are a manager
if (!(await site_license_is_manager(db, account_id, info.id))) {
throw Error("user must be a manager of the license to change it");
}
const set = {};
// Set files and do some sanity checks to avoid having bad data in the database.
if (info.title != null) {
set.title = info.title;
if (typeof set.title != "string") {
throw Error("title must be a string");
}
}
if (info.description != null) {
set.description = info.description;
if (typeof set.description != "string") {
throw Error("description must be a string");
}
}
if (info.managers != null) {
set.managers = info.managers;
for (const manager of set.managers) {
if (!(0, misc_1.is_valid_uuid_string)(manager)) {
throw Error("managers must be an array of valid uuid's");
}
}
}
// Now do the query
await db.async_query({
query: "UPDATE site_licenses",
set,
where: { id: info.id },
});
}
exports.site_license_manager_set = site_license_manager_set;
//# sourceMappingURL=manager.js.map