@cocalc/database
Version:
CoCalc: code for working with our PostgreSQL database
52 lines • 2.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const async_utils_1 = require("@cocalc/util/async-utils");
function isDelete(options) {
return options.some((v) => v?.delete === true);
}
async function registrationTokensQuery(db, options, query) {
if (isDelete(options) && query.token) {
// delete if option is set and there is a token which is defined and not an empty string
await (0, async_utils_1.callback2)(db._query, {
query: "DELETE FROM registration_tokens WHERE token = $1",
params: [query.token],
});
return;
}
// either we want to get all tokens or insert/edit one
if (query.token == "*") {
// select all tokens -- there is of course no WHERE clause, since this is not user specific.
// It's the same tokens for any ADMIN.
const { rows } = await (0, async_utils_1.callback2)(db._query, {
query: "SELECT * FROM registration_tokens",
});
return rows;
}
else if (query.token) {
// upsert an existing one
const { token, descr, expires, limit, disabled } = query;
const { rows } = await (0, async_utils_1.callback2)(db._query, {
query: `INSERT INTO registration_tokens ("token","descr","expires","limit","disabled")
VALUES ($1, $2, $3, $4, $5) ON CONFLICT (token)
DO UPDATE SET
"token" = EXCLUDED.token,
"descr" = EXCLUDED.descr,
"expires" = EXCLUDED.expires,
"limit" = EXCLUDED.limit,
"disabled" = EXCLUDED.disabled`,
params: [
token,
descr ? descr : null,
expires ? expires : null,
limit == null ? null : limit,
disabled != null ? disabled : false,
],
});
return rows;
}
else {
throw new Error("don't know what to do with this query");
}
}
exports.default = registrationTokensQuery;
//# sourceMappingURL=registration-tokens.js.map