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/projects/get.ts
Views: 687
1
/* Get projects that the authenticated user is a collaborator on. */
2
3
import getProjects from "@cocalc/server/projects/get";
4
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
5
import getAccountId from "lib/account/get-account";
6
import getParams from "lib/api/get-params";
7
import { apiRoute, apiRouteOperation } from "lib/api";
8
9
import {
10
GetAccountProjectsInputSchema,
11
GetAccountProjectsOutputSchema,
12
} from "lib/api/schema/projects/get";
13
14
async function handle(req, res) {
15
const client_account_id = await getAccountId(req);
16
try {
17
if (client_account_id == null) {
18
throw Error("Must be signed in.");
19
}
20
21
const { account_id, limit } = getParams(req);
22
23
// User must be an admin to specify account_id field
24
//
25
if (account_id && !(await userIsInGroup(client_account_id, "admin"))) {
26
throw Error(
27
"The `account_id` field may only be specified by account administrators.",
28
);
29
}
30
31
res.json(
32
await getProjects({
33
account_id: account_id || client_account_id,
34
limit,
35
}),
36
);
37
} catch (err) {
38
res.json({ error: err.message });
39
}
40
}
41
42
export default apiRoute({
43
getProject: apiRouteOperation({
44
method: "POST",
45
openApiOperation: {
46
tags: ["Projects", "Admin"],
47
},
48
})
49
.input({
50
contentType: "application/json",
51
body: GetAccountProjectsInputSchema,
52
})
53
.outputs([
54
{
55
status: 200,
56
contentType: "application/json",
57
body: GetAccountProjectsOutputSchema,
58
},
59
])
60
.handler(handle),
61
});
62
63