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