-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ts
53 lines (44 loc) · 1.53 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaBoard } from "@/lib/schemas/board.schema";
import { BoardService } from "@/lib/services/board";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth/auth";
import { ZodError } from "zod";
import { hasTeamPermission } from "@/lib/auth/permission-utils";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({ message: "Method not allowed" });
}
try {
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).json({ message: "Unauthorized" });
}
const { data, teamId } = req.body;
if (!teamId) {
return res.status(400).json({ message: "Team ID is required" });
}
const validatedData = schemaBoard.parse(data);
const boardService = new BoardService();
const hasPermission = await hasTeamPermission(session.user.id, teamId);
if (!hasPermission) {
return res
.status(403)
.json({ message: "Forbidden: No access to this team" });
}
const board = await boardService.create({
name: validatedData.name,
teamId,
});
return res.status(201).json(board);
} catch (error) {
if (error instanceof ZodError) {
return res.status(400).json({ message: error.message });
}
console.error("Board creation error:", error);
return res.status(500).json({ message: "Internal server error" });
}
}