import { NextResponse } from "next/server"; import { auth } from "@/app/(auth)/auth"; import { deleteFlowBoardDraftById, getFlowBoardDraftById, updateFlowBoardDraftById, } from "@/lib/db/queries"; import { mapFlowBoardDraft, normalizeFlowBoardSnapshot, normalizeFlowBoardTitle, } from "../../_utils"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; export const revalidate = 0; type RouteContext = { params: Promise<{ id: string }>; }; export async function GET(_request: Request, { params }: RouteContext) { const session = await auth(); if (!session?.user?.id) { return NextResponse.json( { success: false, error: "未登录" }, { status: 401 } ); } const { id } = await params; const draft = await getFlowBoardDraftById({ userId: session.user.id, draftId: id, }); if (!draft) { return NextResponse.json( { success: false, error: "流程白板文件不存在" }, { status: 404 } ); } return NextResponse.json({ success: true, data: mapFlowBoardDraft(draft), }); } export async function PUT(request: Request, { params }: RouteContext) { const session = await auth(); if (!session?.user?.id) { return NextResponse.json( { success: false, error: "未登录" }, { status: 401 } ); } const { id } = await params; const body = (await request.json().catch(() => ({}))) as { title?: unknown; snapshot?: unknown; }; const draft = await updateFlowBoardDraftById({ userId: session.user.id, draftId: id, title: body.title !== undefined ? normalizeFlowBoardTitle(body.title) : undefined, snapshot: body.snapshot !== undefined ? normalizeFlowBoardSnapshot(body.snapshot) : undefined, }); if (!draft) { return NextResponse.json( { success: false, error: "流程白板文件不存在" }, { status: 404 } ); } return NextResponse.json({ success: true, data: mapFlowBoardDraft(draft), }); } export async function DELETE(_request: Request, { params }: RouteContext) { const session = await auth(); if (!session?.user?.id) { return NextResponse.json( { success: false, error: "未登录" }, { status: 401 } ); } const { id } = await params; const draft = await deleteFlowBoardDraftById({ userId: session.user.id, draftId: id, }); if (!draft) { return NextResponse.json( { success: false, error: "流程白板文件不存在" }, { status: 404 } ); } return NextResponse.json({ success: true, data: mapFlowBoardDraft(draft), }); }