import { NextResponse } from "next/server"; import { isConvexEnabled } from "@/lib/convex/enabled"; type TreeArchiveResponse = { requestId?: string; traceId?: string; }; type DeletePayload = { documentId?: string | null; workspaceId?: string | null; }; export const dynamic = "force-dynamic"; export async function POST(request: Request) { if (!isConvexEnabled()) { return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 }); } try { const { documentId, workspaceId }: DeletePayload = await request.json(); const normalizedDocumentId = typeof documentId === "string" ? documentId.trim() : ""; if (!normalizedDocumentId) { return NextResponse.json({ error: "缺少 documentId" }, { status: 400 }); } const upstreamUrl = new URL("/api/tree/commands", request.url); const response = await fetch(upstreamUrl.toString(), { method: "POST", headers: new Headers({ "content-type": "application/json", }), body: JSON.stringify({ action: "archive", documentId: normalizedDocumentId, workspaceId: typeof workspaceId === "string" ? workspaceId.trim() || null : null, }), }); const payload = (await response.json().catch(() => null)) as | TreeArchiveResponse | { error?: string } | null; if (!response.ok) { return NextResponse.json( { error: payload && typeof payload === "object" && "error" in payload && typeof payload.error === "string" ? payload.error : "删除失败,请稍后再试", }, { status: response.status }, ); } return NextResponse.json({ success: true, meta: { requestId: payload?.requestId, traceId: payload?.traceId, commandName: "tree.node.archive", }, }); } catch (error) { return NextResponse.json( { error: error instanceof Error ? error.message : "删除失败,请稍后再试", }, { status: 500 }, ); } } export const runtime = "nodejs";