Skip to content

Commit 7d382ac

Browse files
committed
add workers admin route
1 parent d3386e2 commit 7d382ac

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { ActionFunctionArgs, json } from "@remix-run/server-runtime";
2+
import { z } from "zod";
3+
import { prisma } from "~/db.server";
4+
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
5+
import { WorkerGroupService } from "~/v3/services/worker/workerGroupService.server";
6+
7+
const RequestBodySchema = z.object({
8+
name: z.string().optional(),
9+
description: z.string().optional(),
10+
projectId: z.string().optional(),
11+
makeDefault: z.boolean().optional(),
12+
});
13+
14+
export async function action({ request }: ActionFunctionArgs) {
15+
// Next authenticate the request
16+
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
17+
18+
if (!authenticationResult) {
19+
return json({ error: "Invalid or Missing API key" }, { status: 401 });
20+
}
21+
22+
const user = await prisma.user.findUnique({
23+
where: {
24+
id: authenticationResult.userId,
25+
},
26+
});
27+
28+
if (!user) {
29+
return json({ error: "Invalid or Missing API key" }, { status: 401 });
30+
}
31+
32+
if (!user.admin) {
33+
return json({ error: "You must be an admin to perform this action" }, { status: 403 });
34+
}
35+
36+
try {
37+
const rawBody = await request.json();
38+
const { name, description, projectId, makeDefault } = RequestBodySchema.parse(rawBody ?? {});
39+
40+
const service = new WorkerGroupService();
41+
const { workerGroup, token } = await service.createWorkerGroup({
42+
name,
43+
description,
44+
});
45+
46+
if (makeDefault && projectId) {
47+
await prisma.project.update({
48+
where: {
49+
id: projectId,
50+
},
51+
data: {
52+
defaultWorkerGroupId: workerGroup.id,
53+
engine: "V2",
54+
},
55+
});
56+
}
57+
58+
return json({
59+
token,
60+
workerGroup,
61+
});
62+
} catch (error) {
63+
return json({ error: error instanceof Error ? error.message : error }, { status: 400 });
64+
}
65+
}

0 commit comments

Comments
 (0)