Your Name e054dcabcc
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: update canvas and flow board features
2026-04-11 22:28:18 +08:00

70 lines
1.6 KiB
TypeScript

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),
});
}