|
1 | 1 | /* Get projects that the authenticated user is a collaborator on. */
|
2 | 2 |
|
3 |
| -import getAccountId from "lib/account/get-account"; |
4 | 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"; |
5 | 6 | import getParams from "lib/api/get-params";
|
| 7 | +import { apiRoute, apiRouteOperation } from "lib/api"; |
6 | 8 |
|
7 |
| -export default async function handle(req, res) { |
8 |
| - const account_id = await getAccountId(req); |
| 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); |
9 | 16 | try {
|
10 |
| - if (account_id == null) throw Error("must be authenticated"); |
11 |
| - const { limit } = getParams(req); |
12 |
| - res.json(await getProjects({ account_id, limit })); |
| 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 | + ); |
13 | 37 | } catch (err) {
|
14 | 38 | res.json({ error: err.message });
|
15 | 39 | }
|
16 | 40 | }
|
| 41 | + |
| 42 | +export default apiRoute({ |
| 43 | + get: 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 | +}); |
0 commit comments