CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/purchases/is-purchase-allowed.ts
Views: 687
1
/*
2
Determine whether or not user can purchase some amount of a given
3
service via pay-as-you-go, given all quotas and current balance.
4
5
PARAMS:
6
7
- service (required): one of the services, e.g., openai-gpt-4
8
- cost (optional): cost in dollars of that service; if not given, some amount may be
9
chosen based on the service, e.g., for gpt-4 it is the maximum cost of a single API call.
10
11
RETURNS:
12
13
- {allowed:boolean; reason?:string} or {error:message} (e.g., if not signed in)
14
*/
15
16
import getAccountId from "lib/account/get-account";
17
import { isPurchaseAllowed } from "@cocalc/server/purchases/is-purchase-allowed";
18
import getParams from "lib/api/get-params";
19
20
export default async function handle(req, res) {
21
try {
22
res.json(await get(req));
23
} catch (err) {
24
res.json({ error: `${err.message}` });
25
return;
26
}
27
}
28
29
async function get(req) {
30
const account_id = await getAccountId(req);
31
if (account_id == null) {
32
throw Error("must be signed in");
33
}
34
const { service, cost } = getParams(req);
35
return await isPurchaseAllowed({ account_id, service, cost });
36
}
37
38