Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/database/postgres/registration-tokens.ts
Views: 687
import { callback2 } from "@cocalc/util/async-utils";1import { PostgreSQL } from "./types";23function isDelete(options: { delete?: boolean }[]) {4return options.some((v) => v?.delete === true);5}67interface Query {8token: string;9descr?: string;10expires?: Date;11limit?: number;12disabled?: boolean;13}1415export default async function registrationTokensQuery(16db: PostgreSQL,17options: { delete?: boolean }[],18query: Query19) {20if (isDelete(options) && query.token) {21// delete if option is set and there is a token which is defined and not an empty string22await callback2(db._query, {23query: "DELETE FROM registration_tokens WHERE token = $1",24params: [query.token],25});26return;27}2829// either we want to get all tokens or insert/edit one30if (query.token == "*") {31// select all tokens -- there is of course no WHERE clause, since this is not user specific.32// It's the same tokens for any ADMIN.33const { rows } = await callback2(db._query, {34query: "SELECT * FROM registration_tokens",35});36return rows;37} else if (query.token) {38// upsert an existing one39const { token, descr, expires, limit, disabled } = query;40const { rows } = await callback2(db._query, {41query: `INSERT INTO registration_tokens ("token","descr","expires","limit","disabled")42VALUES ($1, $2, $3, $4, $5) ON CONFLICT (token)43DO UPDATE SET44"token" = EXCLUDED.token,45"descr" = EXCLUDED.descr,46"expires" = EXCLUDED.expires,47"limit" = EXCLUDED.limit,48"disabled" = EXCLUDED.disabled`,49params: [50token,51descr ? descr : null,52expires ? expires : null,53limit == null ? null : limit, // if undefined make it null54disabled != null ? disabled : false,55],56});57return rows;58} else {59throw new Error("don't know what to do with this query");60}61}626364