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/delete.ts
Views: 687
1
/*
2
API endpoint to delete a project, which sets the "delete" flag to `true` in the database.
3
*/
4
import isCollaborator from "@cocalc/server/projects/is-collaborator";
5
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
6
import removeAllLicensesFromProject from "@cocalc/server/licenses/remove-all-from-project";
7
import { getProject } from "@cocalc/server/projects/control";
8
import userQuery from "@cocalc/database/user-query";
9
import { isValidUUID } from "@cocalc/util/misc";
10
11
import getAccountId from "lib/account/get-account";
12
import getParams from "lib/api/get-params";
13
import { apiRoute, apiRouteOperation } from "lib/api";
14
import { OkStatus } from "lib/api/status";
15
import {
16
DeleteProjectInputSchema,
17
DeleteProjectOutputSchema,
18
} from "lib/api/schema/projects/delete";
19
20
async function handle(req, res) {
21
const { project_id } = getParams(req);
22
const account_id = await getAccountId(req);
23
24
try {
25
if (!isValidUUID(project_id)) {
26
throw Error("project_id must be a valid uuid");
27
}
28
if (!account_id) {
29
throw Error("must be signed in");
30
}
31
32
// If client is not an administrator, they must be a project collaborator in order to
33
// delete a project.
34
if (
35
!(await userIsInGroup(account_id, "admin")) &&
36
!(await isCollaborator({ account_id, project_id }))
37
) {
38
throw Error("must be an owner to delete a project");
39
}
40
41
// Remove all project licenses
42
//
43
await removeAllLicensesFromProject({ project_id });
44
45
// Stop project
46
//
47
const project = getProject(project_id);
48
await project.stop();
49
50
// Set "deleted" flag. We do this last to ensure that the project is not consuming any
51
// resources while it is in the deleted state.
52
//
53
await userQuery({
54
account_id,
55
query: {
56
projects: {
57
project_id,
58
deleted: true,
59
},
60
},
61
});
62
63
res.json(OkStatus);
64
} catch (err) {
65
res.json({ error: err.message });
66
}
67
}
68
69
export default apiRoute({
70
deleteProject: apiRouteOperation({
71
method: "POST",
72
openApiOperation: {
73
tags: ["Projects", "Admin"],
74
},
75
})
76
.input({
77
contentType: "application/json",
78
body: DeleteProjectInputSchema,
79
})
80
.outputs([
81
{
82
status: 200,
83
contentType: "application/json",
84
body: DeleteProjectOutputSchema,
85
},
86
])
87
.handler(handle),
88
});
89
90