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