import { NextResponse } from "next/server"; import type { PageRole } from "@prisma/client"; import { prisma } from "@/lib/db"; import { actorLabel, pageRoleOf } from "@/lib/auth/user"; import { PAGE_ROLES } from "@/lib/auth/roles"; import { requireTeamManager } from "@/lib/teams/access"; import { audit } from "@/lib/audit"; import { readJson, str } from "@/lib/http"; export async function PUT(req: Request, ctx: { params: Promise<{ id: string }> }) { const { id } = await ctx.params; const auth = await requireTeamManager(id); if (!auth.ok) return auth.response; const body = await readJson(req); const userId = str(body.userId); const member = await prisma.teamMember.findUnique({ where: { teamId_userId: { teamId: id, userId } } }); if (!member) return NextResponse.json({ error: "ไม่พบสมาชิกทีม" }, { status: 404 }); const teamRole = str(body.teamRole); if (teamRole) { if (auth.user.id !== auth.team.ownerId || member.role === "OWNER" || !["ADMIN", "MEMBER"].includes(teamRole)) return NextResponse.json({ error: "เฉพาะเจ้าของทีมที่เปลี่ยนผู้ดูแลทีมได้" }, { status: 403 }); await prisma.teamMember.update({ where: { id: member.id }, data: { role: teamRole as "ADMIN" | "MEMBER" } }); await audit({ action: "TEAM_MEMBER_ROLE_SET", actorId: auth.user.id, actorLabel: actorLabel(auth.user), detail: { teamId: id, userId, teamRole } }); return NextResponse.json({ ok: true }); } const pageId = str(body.pageId); const role = str(body.pageRole) as PageRole; const attached = await prisma.teamPage.findUnique({ where: { pageId }, select: { teamId: true } }); if (attached?.teamId !== id || await pageRoleOf(auth.user, pageId) !== "MANAGER") return NextResponse.json({ error: "ไม่มีสิทธิ์จัดการสมาชิกของเพจนี้" }, { status: 403 }); if (member.role === "OWNER") return NextResponse.json({ error: "เปลี่ยนสิทธิ์เจ้าของทีมในเพจนี้ไม่ได้" }, { status: 403 }); if (role && !PAGE_ROLES.includes(role)) return NextResponse.json({ error: "สิทธิ์ไม่ถูกต้อง" }, { status: 400 }); await prisma.$transaction(async (tx) => { if (role) await tx.pageMember.upsert({ where: { userId_pageId: { userId, pageId } }, create: { userId, pageId, role }, update: { role } }); else await tx.pageMember.deleteMany({ where: { userId, pageId } }); await audit({ action: role ? "PAGE_MEMBER_SET" : "PAGE_MEMBER_REMOVED", actorId: auth.user.id, actorLabel: actorLabel(auth.user), pageId, detail: { teamId: id, userId, role: role || null } }, tx); }); return NextResponse.json({ ok: true }); } export async function DELETE(req: Request, ctx: { params: Promise<{ id: string }> }) { const { id } = await ctx.params; const auth = await requireTeamManager(id); if (!auth.ok) return auth.response; const userId = new URL(req.url).searchParams.get("userId") ?? ""; const member = await prisma.teamMember.findUnique({ where: { teamId_userId: { teamId: id, userId } } }); if (!member) return NextResponse.json({ error: "ไม่พบสมาชิกทีม" }, { status: 404 }); if (member.role === "OWNER" || userId === auth.user.id || (member.role === "ADMIN" && auth.team.ownerId !== auth.user.id)) return NextResponse.json({ error: "ไม่มีสิทธิ์ถอดสมาชิกนี้" }, { status: 403 }); await prisma.$transaction(async (tx) => { const pages = await tx.teamPage.findMany({ where: { teamId: id }, select: { pageId: true } }); await tx.pageMember.deleteMany({ where: { userId, pageId: { in: pages.map((p) => p.pageId) } } }); await tx.teamMember.delete({ where: { id: member.id } }); await audit({ action: "TEAM_MEMBER_REMOVED", actorId: auth.user.id, actorLabel: actorLabel(auth.user), detail: { teamId: id, userId } }, tx); }); return NextResponse.json({ ok: true }); }