UNPKG

@cocalc/database

Version:

CoCalc: code for working with our PostgreSQL database

109 lines (107 loc) 4.53 kB
"use strict"; /* * 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.is_a_site_license_manager = exports.manager_site_licenses = exports.matching_site_licenses = void 0; const misc_1 = require("@cocalc/util/misc"); const query_1 = require("../query"); // This works and does the entire search in the database. // Unfortunately, it is unusably slow and I just don't have // the time to do something better right now: /* export async function matching_site_licenses( db: PostgreSQL, search: string, limit: number = 5 ): Promise<{ id: string }[]> { const where: any[] = []; const params: (string | number)[] = []; let i = 1; for (const s of search_split(search.toLowerCase())) { where.push( `(lower(site_licenses.title) LIKE $${i}::TEXT OR lower(site_licenses.description) LIKE $${i}::TEXT OR site_licenses.id::TEXT LIKE $${i}::TEXT OR lower(site_licenses.info::TEXT) LIKE $${i}::TEXT OR lower(accounts.first_name) LIKE $${i}::TEXT OR lower(accounts.last_name) LIKE $${i}::TEXT OR lower(accounts.email_address) LIKE $${i}::TEXT)` ); params.push(`%${s}%`); i += 1; } let query = "SELECT DISTINCT(site_licenses.id) AS id, site_licenses.last_used FROM site_licenses, accounts WHERE accounts.account_id::TEXT = ANY(site_licenses.managers) AND"; query += ` (${where.join(" AND ")})`; // recently active licenses are much more relevant than old ones query += " ORDER BY site_licenses.last_used DESC NULLS LAST"; query += ` LIMIT $${i}::INTEGER`; params.push(limit); i += 1; return (await db.async_query({ query, params })).rows; } */ // This is dumb but will be sufficiently fast up to probably 5K licenses. // This is not user facing functionality. We could maybe restrict to last_used // in the few months by default (optionally anything) and this would last // much longer... async function matching_site_licenses(db, search, limit = 5) { if ((0, misc_1.is_valid_uuid_string)(search)) { return (await db.async_query({ cache: true, query: "SELECT id FROM site_licenses WHERE id=$1", params: [search], })).rows; } // Get them all. const licenses = (await db.async_query({ query: "SELECT id, id || ' ' || coalesce(lower(title),'') || ' ' || coalesce(lower(description),'') || ' ' || coalesce(lower(info::TEXT),'') AS info, managers FROM site_licenses ORDER BY last_used DESC NULLS LAST", })).rows; // Replace manager account ids by name and email const managers = new Set(); for (const x of licenses) { if (x.managers != null) { for (const account_id of x.managers) { managers.add(account_id); } } } const accounts = {}; for (const row of (await db.async_query({ cache: true, query: "SELECT account_id, coalesce(lower(first_name),'') || ' ' || coalesce(lower(last_name),'') || ' ' || coalesce(lower(email_address),'') AS info FROM accounts WHERE account_id=ANY($1)", params: [Array.from(managers)], })).rows) { accounts[row.account_id] = row.info; } const v = (0, misc_1.search_split)(search.toLowerCase()); const matches = []; for (const license of licenses) { let s = license.info; if (license.managers) { for (const account_id of license.managers) { s += " " + accounts[account_id]; } } if ((0, misc_1.search_match)(s, v)) { matches.push({ id: license.id }); } if (matches.length >= limit) break; } return matches; } exports.matching_site_licenses = matching_site_licenses; async function manager_site_licenses(db, account_id) { const query = "SELECT * FROM site_licenses WHERE $1=ANY(managers)"; const params = [account_id]; return (await db.async_query({ query, params })).rows; } exports.manager_site_licenses = manager_site_licenses; // Return true if the given user is a manager of any site licenses. async function is_a_site_license_manager(db, account_id) { return ((await (0, query_1.query)({ db, query: "SELECT COUNT(*)::INT FROM site_licenses WHERE $1=ANY(managers)", one: true, params: [account_id], })).count > 0); } exports.is_a_site_license_manager = is_a_site_license_manager; //# sourceMappingURL=search.js.map