Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/vouchers/redeem.ts
1451 views
1
import redeemVoucher from "@cocalc/server/vouchers/redeem";
2
import getAccountId from "lib/account/get-account";
3
import getParams from "lib/api/get-params";
4
5
export default async function handle(req, res) {
6
try {
7
const createdItems = await doIt(req);
8
res.json(createdItems);
9
} catch (err) {
10
res.json({ error: `${err.message}` });
11
return;
12
}
13
}
14
15
// returns array of objects that describe roughly what redeeming the code provided
16
async function doIt(req) {
17
const { code } = getParams(req);
18
if (!code || code.length < 8) {
19
throw Error("code must be at least 8 characters long");
20
}
21
const account_id = await getAccountId(req);
22
if (account_id == null) {
23
throw Error("must be signed in to redeem a voucher code");
24
}
25
26
return await redeemVoucher({ account_id, code });
27
}
28
29