import { NextResponse, type NextRequest } from "next/server"; import { callMobileApi } from "@/app/lib/server/mobileApi"; import { getToken } from "@/app/lib/server/session"; import { errorResponse, unauthorized, badRequest } from "@/app/lib/server/apiRoute"; import type { ModuleNode } from "@/app/lib/types"; // GET/POST -> /api/admin/modules (add-admin-module-node-crud). Forwards the // admin JWT from the bb_admin session cookie, same as /api/locales. GET is a // pure read; the module tree writes live on POST (create) / PUT (update) / // DELETE — never on a GET (Next.js prefetch rule in CLAUDE.md). Downstream // 409 (duplicate name) / 422 (invalid parent, protected name, children) are // surfaced verbatim so the page can show the reason inline. export async function GET(_req: NextRequest) { const token = await getToken(); if (!token) return unauthorized(); const result = await callMobileApi("/api/admin/modules", { token }); if (!result.ok) return errorResponse(result); return NextResponse.json(result.data); } export async function POST(req: NextRequest) { const token = await getToken(); if (!token) return unauthorized(); const body = await req.json().catch(() => ({})); const { name } = body as Partial; if (!name) { return badRequest("name is required."); } const result = await callMobileApi("/api/admin/modules", { method: "POST", body, token, }); if (!result.ok) return errorResponse(result); return NextResponse.json(result.data); }