|
| 1 | +/* |
| 2 | +Set project title, description, etc. |
| 3 | +*/ |
| 4 | + |
| 5 | +import userIsInGroup from "@cocalc/server/accounts/is-in-group"; |
| 6 | +import setProject from "@cocalc/server/projects/set-one"; |
| 7 | + |
| 8 | +import getAccountId from "lib/account/get-account"; |
| 9 | +import getParams from "lib/api/get-params"; |
| 10 | + |
| 11 | +import { OkStatus } from "lib/api/status"; |
| 12 | +import { apiRoute, apiRouteOperation } from "lib/api"; |
| 13 | +import { |
| 14 | + UpdateProjectInputSchema, |
| 15 | + UpdateProjectOutputSchema, |
| 16 | +} from "lib/api/schema/projects/update"; |
| 17 | + |
| 18 | +async function handle(req, res) { |
| 19 | + try { |
| 20 | + await get(req); |
| 21 | + res.json(OkStatus); |
| 22 | + } catch (err) { |
| 23 | + res.json({ error: `${err.message ? err.message : err}` }); |
| 24 | + return; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +async function get(req) { |
| 29 | + const client_account_id = await getAccountId(req); |
| 30 | + |
| 31 | + if (client_account_id == null) { |
| 32 | + throw Error("Must be signed in to update project."); |
| 33 | + } |
| 34 | + |
| 35 | + const { account_id, project_id, title, description, name } = getParams(req); |
| 36 | + |
| 37 | + // If the API client is an admin, they may act on any project on behalf of any account. |
| 38 | + // Otherwise, the client may only update projects for which they are listed as |
| 39 | + // collaborators. |
| 40 | + // |
| 41 | + if (account_id && !(await userIsInGroup(client_account_id, "admin"))) { |
| 42 | + throw Error( |
| 43 | + "The `account_id` field may only be specified by account administrators.", |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + return setProject({ |
| 48 | + acting_account_id: account_id || client_account_id, |
| 49 | + project_id, |
| 50 | + project_update: { |
| 51 | + title, |
| 52 | + description, |
| 53 | + name, |
| 54 | + }, |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +export default apiRoute({ |
| 59 | + update: apiRouteOperation({ |
| 60 | + method: "POST", |
| 61 | + openApiOperation: { |
| 62 | + tags: ["Projects", "Admin"], |
| 63 | + }, |
| 64 | + }) |
| 65 | + .input({ |
| 66 | + contentType: "application/json", |
| 67 | + body: UpdateProjectInputSchema, |
| 68 | + }) |
| 69 | + .outputs([ |
| 70 | + { |
| 71 | + status: 200, |
| 72 | + contentType: "application/json", |
| 73 | + body: UpdateProjectOutputSchema, |
| 74 | + }, |
| 75 | + ]) |
| 76 | + .handler(handle), |
| 77 | +}); |
0 commit comments