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/next/pages/api/v2/vouchers/create-vouchers.ts
Views: 687
import createVouchers from "@cocalc/server/vouchers/create-vouchers";1import getAccountId from "lib/account/get-account";2import userIsInGroup from "@cocalc/server/accounts/is-in-group";3import getParams from "lib/api/get-params";45export default async function handle(req, res) {6try {7const result = await doIt(req);8res.json({ ...result, success: true });9} catch (err) {10res.json({ error: `${err.message}` });11return;12}13}1415async function doIt(req) {16const {17whenPay,18count,19active,20expire,21cancelBy,22title,23length,24charset,25prefix,26postfix,27} = getParams(req);28if (count == null) {29throw Error("must provide number of vouchers");30}31if (whenPay == "invoice") {32if (active == null) {33throw Error("must provide activation date");34}35if (expire == null) {36throw Error("must provide expiration date");37}38if (cancelBy == null) {39throw Error("must provide cancelBy date");40}41}42if (title == null) {43throw Error("must provide title");44}45if (length == null || length < 6 || length > 16) {46throw Error("must provide length that is at least 6 and less than 16");47}48if (prefix == null) {49throw Error("must provide prefix");50}51if (postfix == null) {52throw Error("must provide postfix");53}54if (charset == null) {55throw Error("must provide charset");56}57const account_id = await getAccountId(req);58if (account_id == null) {59throw Error("must be signed in to create vouchers");60}61if (whenPay == "invoice" && !(await userIsInGroup(account_id, "partner"))) {62throw Error("only partners can create vouchers");63}6465return await createVouchers({66account_id,67whenPay,68count,69active: new Date(active),70expire: new Date(expire),71cancelBy: new Date(cancelBy),72title,73generate: { length, prefix, postfix, charset },74});75}767778