Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/purchases/get-purchases-admin.ts
1454 views
1
/*
2
Let admin get all of the purchases for a specified user.
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 userIsInGroup from "@cocalc/server/accounts/is-in-group";
9
import throttle from "@cocalc/util/api/throttle";
10
11
export default async function handle(req, res) {
12
try {
13
res.json(await get(req));
14
} catch (err) {
15
res.json({ error: `${err.message}` });
16
return;
17
}
18
}
19
20
async function get(req) {
21
const admin_account_id = await getAccountId(req);
22
if (admin_account_id == null) {
23
throw Error("must be signed in");
24
}
25
// This user MUST be an admin:
26
if (!(await userIsInGroup(admin_account_id, "admin"))) {
27
throw Error("only admins can use the get-purchases-admin endpoint");
28
}
29
throttle({
30
account_id: admin_account_id,
31
endpoint: "purchases/get-purchases-admin",
32
});
33
34
const {
35
account_id,
36
limit,
37
offset,
38
service,
39
project_id,
40
group,
41
cutoff,
42
thisMonth,
43
day_statement_id,
44
month_statement_id,
45
no_statement,
46
includeName,
47
} = getParams(req);
48
49
return await getPurchases({
50
cutoff,
51
thisMonth,
52
limit,
53
offset,
54
service,
55
account_id,
56
project_id,
57
group,
58
day_statement_id,
59
month_statement_id,
60
no_statement,
61
includeName,
62
});
63
}
64
65