import { NextResponse } from "next/server"; import { auth } from "@/app/(auth)/auth"; import { getFlowBoardDraftByUserId, upsertFlowBoardDraftByUserId, } from "@/lib/db/queries"; import { mapFlowBoardDraft, normalizeFlowBoardSnapshot } from "../_utils"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; export const revalidate = 0; export async function GET() { const session = await auth(); if (!session?.user?.id) { return NextResponse.json( { success: false, error: "未登录" }, { status: 401 } ); } const draft = await getFlowBoardDraftByUserId({ userId: session.user.id, }); return NextResponse.json({ success: true, data: draft ? mapFlowBoardDraft(draft) : { id: null, title: "流程白板", snapshot: { children: [] }, createdAt: null, updatedAt: null, }, }); } export async function PUT(request: Request) { const session = await auth(); if (!session?.user?.id) { return NextResponse.json( { success: false, error: "未登录" }, { status: 401 } ); } const body = (await request.json().catch(() => ({}))) as { snapshot?: unknown; }; const snapshot = normalizeFlowBoardSnapshot(body.snapshot ?? body); const savedDraft = await upsertFlowBoardDraftByUserId({ userId: session.user.id, snapshot, }); if (!savedDraft) { return NextResponse.json( { success: false, error: "保存流程白板失败" }, { status: 500 } ); } return NextResponse.json({ success: true, data: mapFlowBoardDraft(savedDraft), }); }