import { NextResponse } from "next/server"; import { isConvexEnabled } from "@/lib/convex/enabled"; import { getAuthedConvexClient } from "@/lib/convex/route"; import { api } from "@/lib/convex/api"; const defaultMindmapData = { data: { text: "中心主题" }, children: [], }; export async function GET( _req: Request, { params }: { params: Promise<{ docId: string; mindmapId: string }> }, ) { const { docId, mindmapId } = await params; if (isConvexEnabled()) { const { client } = await getAuthedConvexClient(); const res = await client.query(api.mindmaps.get, { docId, mindmapId }); return NextResponse.json({ data: res?.data ?? defaultMindmapData, source: "convex" }); } return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 }); } export async function POST( request: Request, { params }: { params: Promise<{ docId: string; mindmapId: string }> }, ) { const { docId, mindmapId } = await params; if (isConvexEnabled()) { const { client } = await getAuthedConvexClient(); const { data, createOnly } = (await request.json().catch(() => ({ data: null }))) as { data?: unknown; createOnly?: boolean; }; try { const result = await client.mutation(api.mindmaps.put, { docId, mindmapId, data: data ?? defaultMindmapData, ...(typeof createOnly === "boolean" ? { createOnly } : {}), }); return NextResponse.json(result ?? { ok: true }); } catch (error) { return NextResponse.json({ error: (error as Error).message }, { status: 400 }); } } return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 }); } export async function DELETE( _req: Request, { params }: { params: Promise<{ docId: string; mindmapId: string }> }, ) { const { docId, mindmapId } = await params; if (isConvexEnabled()) { const { client } = await getAuthedConvexClient(); try { const result = await client.mutation(api.mindmaps.softDelete, { docId, mindmapId }); return NextResponse.json(result ?? { ok: true }); } catch (error) { return NextResponse.json({ error: (error as Error).message }, { status: 400 }); } } return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 }); } export async function PATCH( request: Request, { params }: { params: Promise<{ docId: string; mindmapId: string }> }, ) { const { docId, mindmapId } = await params; if (isConvexEnabled()) { const { client } = await getAuthedConvexClient(); const { action } = (await request.json().catch(() => ({}))) as { action?: string }; if (action !== "restore" && action !== "purge") { return NextResponse.json({ error: "不支持的操作" }, { status: 400 }); } try { if (action === "purge") { const result = await client.mutation(api.mindmaps.purge, { docId, mindmapId }); return NextResponse.json(result ?? { ok: true }); } const result = await client.mutation(api.mindmaps.restore, { docId, mindmapId }); return NextResponse.json(result ?? { ok: true }); } catch (error) { const msg = (error as Error).message ?? "操作失败"; const status = msg.includes("未找到") ? 404 : 400; return NextResponse.json({ error: msg }, { status }); } } return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 }); }