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/get-purchases.ts
Views: 923
1
/*
2
Let user get all of their purchases
3
*/
4
5
import getAccountId from "lib/account/get-account";
6
import getPurchases from "@cocalc/server/purchases/get-purchases";
7
import getParams from "lib/api/get-params";
8
import { apiRoute, apiRouteOperation } from "lib/api";
9
import {
10
GetPurchasesInputSchema,
11
GetPurchasesOutputSchema,
12
} from "lib/api/schema/purchases/get-purchases";
13
import throttle from "@cocalc/util/api/throttle";
14
15
async function handle(req, res) {
16
try {
17
res.json(await get(req));
18
} catch (err) {
19
res.json({ error: `${err.message}` });
20
return;
21
}
22
}
23
24
async function get(req) {
25
const account_id = await getAccountId(req);
26
if (!account_id) {
27
throw Error("must be signed in");
28
}
29
const {
30
limit,
31
offset,
32
service,
33
project_id,
34
group,
35
cutoff,
36
thisMonth,
37
day_statement_id,
38
month_statement_id,
39
no_statement,
40
compute_server_id,
41
} = getParams(req);
42
if (!compute_server_id) {
43
// for now we are only throttling when compute_server_id is NOT set. There are several cases -- course management etc
44
// where a client calls get-purchases for each compute server separately with group -- it's not much load.
45
throttle({
46
account_id,
47
endpoint: "purchases/get-purchases",
48
});
49
}
50
return await getPurchases({
51
cutoff,
52
thisMonth,
53
limit,
54
offset,
55
service,
56
account_id,
57
project_id,
58
group,
59
day_statement_id,
60
month_statement_id,
61
no_statement,
62
compute_server_id,
63
});
64
}
65
66
export default apiRoute({
67
getPurchases: apiRouteOperation({
68
method: "POST",
69
openApiOperation: {
70
tags: ["Purchases"],
71
},
72
})
73
.input({
74
contentType: "application/json",
75
body: GetPurchasesInputSchema,
76
})
77
.outputs([
78
{
79
status: 200,
80
contentType: "application/json",
81
body: GetPurchasesOutputSchema,
82
},
83
])
84
.handler(handle),
85
});
86
87