Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/projects/copy-path-status.ts
14425 views
1
/*
2
API endpoint to query the status of a copy_paths row submitted via
3
/api/v2/projects/copy-path with wait_until_done=false.
4
5
The frontend polls this until `finished` is set; then `copy_error` is
6
either null (success) or contains a human-readable description of what
7
went wrong on the project pod / manage side. We deliberately use
8
`copy_error` rather than `error` so the shared apiPost wrapper, which
9
throws on any top-level `error` field, doesn't conflate "the copy
10
failed" with "this status request failed" — the caller wants to
11
distinguish those.
12
13
Auth: the requesting account must be a collaborator on the target
14
project that the copy was directed to. (Anonymous owners count, since
15
they are the sole collaborator on a freshly-created project they made
16
via "Use CoCalc Anonymously".)
17
*/
18
19
import getAccountId from "lib/account/get-account";
20
import getPool from "@cocalc/database/pool";
21
import isCollaborator from "@cocalc/server/projects/is-collaborator";
22
import { isValidUUID } from "@cocalc/util/misc";
23
import getParams from "lib/api/get-params";
24
25
export default async function handle(req, res) {
26
const { copy_id } = getParams(req);
27
try {
28
if (!isValidUUID(copy_id)) {
29
throw Error("copy_id must be a valid uuid");
30
}
31
const account_id = await getAccountId(req);
32
if (!account_id) {
33
throw Error("must be signed in");
34
}
35
const pool = getPool("short");
36
const { rows } = await pool.query(
37
"SELECT target_project_id, started, finished, error FROM copy_paths WHERE id=$1",
38
[copy_id],
39
);
40
if (rows.length === 0) {
41
throw Error("copy_id not found");
42
}
43
const row = rows[0];
44
if (
45
!(await isCollaborator({
46
account_id,
47
project_id: row.target_project_id,
48
}))
49
) {
50
throw Error("must be a collaborator on the target project");
51
}
52
res.json({
53
started: row.started,
54
finished: row.finished,
55
copy_error: row.error,
56
});
57
} catch (err) {
58
res.json({ error: `${err.message}` });
59
}
60
}
61
62