Skip to content

Commit d43a725

Browse files
authored
Merge pull request #7747 from schrodingersket/feature/7665
Account Management API: Added v2 API endpoint add collaborators…
2 parents 3828968 + c797ac6 commit d43a725

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { z } from "../../../framework";
2+
3+
import { FailedAPIOperationSchema, OkAPIOperationSchema } from "../../common";
4+
5+
import { ProjectIdSchema } from "../common";
6+
import { AccountIdSchema } from "../../accounts/common";
7+
8+
// OpenAPI spec
9+
//
10+
export const AddProjectCollaboratorInputSchema = z
11+
.object({
12+
project_id: ProjectIdSchema,
13+
account_id: AccountIdSchema,
14+
})
15+
.describe("Add a collaborator to an existing project.");
16+
17+
export const AddProjectCollaboratorOutputSchema = z.union([
18+
FailedAPIOperationSchema,
19+
OkAPIOperationSchema,
20+
]);
21+
22+
export type AddProjectCollaboratorInput = z.infer<
23+
typeof AddProjectCollaboratorInputSchema
24+
>;
25+
export type AddProjectCollaboratorOutput = z.infer<
26+
typeof AddProjectCollaboratorOutputSchema
27+
>;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { z } from "../../../framework";
2+
3+
import { FailedAPIOperationSchema, OkAPIOperationSchema } from "../../common";
4+
5+
import { ProjectIdSchema } from "../common";
6+
7+
// OpenAPI spec
8+
//
9+
export const RemoveProjectCollaboratorInputSchema = z
10+
.object({
11+
project_id: ProjectIdSchema,
12+
})
13+
.describe("Remove a collaborator from a project.");
14+
15+
export const RemoveProjectCollaboratorOutputSchema = z.union([
16+
FailedAPIOperationSchema,
17+
OkAPIOperationSchema,
18+
]);
19+
20+
export type RemoveProjectCollaboratorInput = z.infer<
21+
typeof RemoveProjectCollaboratorInputSchema
22+
>;
23+
export type RemoveProjectCollaboratorOutput = z.infer<
24+
typeof RemoveProjectCollaboratorOutputSchema
25+
>;

src/packages/next/pages/api/v2/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,11 @@ export default docsApiRoute({
6464
title: "CoCalc API",
6565
description: "",
6666
logoUrl: "https://cocalc.com/_next/static/media/full.0a70e50d.svg",
67+
ogConfig: {
68+
title: "CoCalc HTTP API (v2)",
69+
type: "website",
70+
url: "https://cocalc.com/api/v2",
71+
imageUrl: "https://cocalc.com/webapp/favicon.ico",
72+
},
6773
},
6874
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
API endpoint to add a user to an existing project.
3+
4+
Permissions checks are performed by the underlying API call and are NOT
5+
executed at this stage.
6+
7+
*/
8+
import { db } from "@cocalc/database";
9+
import { add_collaborators_to_projects } from "@cocalc/server/projects/collab";
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+
AddProjectCollaboratorInputSchema,
17+
AddProjectCollaboratorOutputSchema,
18+
} from "lib/api/schema/projects/collaborators/add";
19+
20+
async function handle(req, res) {
21+
const { project_id, account_id } = getParams(req);
22+
const client_account_id = await getAccountId(req);
23+
24+
try {
25+
if (!client_account_id) {
26+
throw Error("must be signed in");
27+
}
28+
29+
await add_collaborators_to_projects(
30+
db(),
31+
client_account_id,
32+
[account_id],
33+
[project_id],
34+
);
35+
36+
res.json(OkStatus);
37+
} catch (err) {
38+
res.json({ error: err.message });
39+
}
40+
}
41+
42+
export default apiRoute({
43+
add: apiRouteOperation({
44+
method: "POST",
45+
openApiOperation: {
46+
tags: ["Projects", "Administrators"],
47+
},
48+
})
49+
.input({
50+
contentType: "application/json",
51+
body: AddProjectCollaboratorInputSchema,
52+
})
53+
.outputs([
54+
{
55+
status: 200,
56+
contentType: "application/json",
57+
body: AddProjectCollaboratorOutputSchema,
58+
},
59+
])
60+
.handler(handle),
61+
});

0 commit comments

Comments
 (0)