Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/projects/delete.ts
Views: 687
/*1API endpoint to delete a project, which sets the "delete" flag to `true` in the database.2*/3import isCollaborator from "@cocalc/server/projects/is-collaborator";4import userIsInGroup from "@cocalc/server/accounts/is-in-group";5import removeAllLicensesFromProject from "@cocalc/server/licenses/remove-all-from-project";6import { getProject } from "@cocalc/server/projects/control";7import userQuery from "@cocalc/database/user-query";8import { isValidUUID } from "@cocalc/util/misc";910import getAccountId from "lib/account/get-account";11import getParams from "lib/api/get-params";12import { apiRoute, apiRouteOperation } from "lib/api";13import { OkStatus } from "lib/api/status";14import {15DeleteProjectInputSchema,16DeleteProjectOutputSchema,17} from "lib/api/schema/projects/delete";1819async function handle(req, res) {20const { project_id } = getParams(req);21const account_id = await getAccountId(req);2223try {24if (!isValidUUID(project_id)) {25throw Error("project_id must be a valid uuid");26}27if (!account_id) {28throw Error("must be signed in");29}3031// If client is not an administrator, they must be a project collaborator in order to32// delete a project.33if (34!(await userIsInGroup(account_id, "admin")) &&35!(await isCollaborator({ account_id, project_id }))36) {37throw Error("must be an owner to delete a project");38}3940// Remove all project licenses41//42await removeAllLicensesFromProject({ project_id });4344// Stop project45//46const project = getProject(project_id);47await project.stop();4849// Set "deleted" flag. We do this last to ensure that the project is not consuming any50// resources while it is in the deleted state.51//52await userQuery({53account_id,54query: {55projects: {56project_id,57deleted: true,58},59},60});6162res.json(OkStatus);63} catch (err) {64res.json({ error: err.message });65}66}6768export default apiRoute({69deleteProject: apiRouteOperation({70method: "POST",71openApiOperation: {72tags: ["Projects", "Admin"],73},74})75.input({76contentType: "application/json",77body: DeleteProjectInputSchema,78})79.outputs([80{81status: 200,82contentType: "application/json",83body: DeleteProjectOutputSchema,84},85])86.handler(handle),87});888990