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. Commercial Alternative to JupyterHub.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/purchases/stripe/get-open-payments.ts
Views: 791
1
import getAccountId from "lib/account/get-account";
2
import { getAllOpenPayments } from "@cocalc/server/purchases/stripe/get-payments";
3
import throttle from "@cocalc/util/api/throttle";
4
import getParams from "lib/api/get-params";
5
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
6
7
export default async function handle(req, res) {
8
try {
9
res.json(await get(req));
10
} catch (err) {
11
res.json({ error: `${err.message}` });
12
return;
13
}
14
}
15
16
async function get(req) {
17
const account_id = await getAccountId(req);
18
if (account_id == null) {
19
throw Error("must be signed in");
20
}
21
throttle({
22
account_id,
23
endpoint: "purchases/stripe/get-open-payments",
24
});
25
const { user_account_id } = getParams(req);
26
if (user_account_id) {
27
// This user MUST be an admin:
28
if (!(await userIsInGroup(account_id, "admin"))) {
29
throw Error("only admins can get other user's open payments");
30
}
31
return await getAllOpenPayments(user_account_id);
32
}
33
return await getAllOpenPayments(account_id);
34
}
35
36