import { callback2 } from "@cocalc/util/async-utils" ; function isDelete ( options ) { return options . some ( ( v ) => v ?. delete === true ) ; } interface Query { token : string ; descr ? : string ; expires ? : Date ; limit ? : number ; disabled ? : boolean ; } export default async function registrationTokensQuery ( db , options , query ) { if ( isDelete ( options ) && query . token ) { await callback2 ( db . _query , { query : "DELETE FROM registration_tokens WHERE token = $1" , params : [ query . token ] , } ) ; return ; } if ( query . token == "*" ) { const { rows } = await callback2 ( db . _query , { query : "SELECT * FROM registration_tokens" , } ) ; return rows ; } else if ( query . token ) { const { token , descr , expires , limit , disabled } = query ; const { rows } = await 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" ) ; } }