2025-11-23 10:55:04 +08:00
|
|
|
import { NextResponse } from "next/server";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
2026-04-14 13:22:29 +08:00
|
|
|
import {
|
|
|
|
|
assertDocumentId,
|
|
|
|
|
assertTitle,
|
|
|
|
|
buildDocumentBridgeContext,
|
|
|
|
|
buildDocumentCommandEnvelope,
|
|
|
|
|
documentBridgeErrorResponse,
|
|
|
|
|
} from "@/lib/documents/bridge";
|
|
|
|
|
import {
|
|
|
|
|
type DocumentTitleUpdatePayload,
|
|
|
|
|
} from "@/lib/documents/metadata-command-adapter";
|
2026-04-23 07:38:34 +08:00
|
|
|
import { executePageWriteBridgeCommand } from "@/lib/documents/page-write-command-adapter";
|
|
|
|
|
import { PAGE_COMMAND_NAMES } from "@/lib/documents/page-command-contract";
|
2025-11-23 10:55:04 +08:00
|
|
|
|
|
|
|
|
interface RenamePayload {
|
|
|
|
|
documentId: string;
|
2026-04-14 13:22:29 +08:00
|
|
|
workspaceId?: string | null;
|
2025-11-23 10:55:04 +08:00
|
|
|
title: string;
|
2026-04-24 06:10:18 +08:00
|
|
|
commandName?: string | null;
|
2025-11-23 10:55:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-24 06:10:18 +08:00
|
|
|
type TreeRenameResponse = {
|
|
|
|
|
requestId?: string;
|
|
|
|
|
traceId?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-23 10:55:04 +08:00
|
|
|
export async function POST(request: Request) {
|
2026-01-17 10:12:53 +08:00
|
|
|
if (isConvexEnabled()) {
|
2026-04-14 13:22:29 +08:00
|
|
|
try {
|
2026-04-24 06:10:18 +08:00
|
|
|
const { documentId, workspaceId, title, commandName }: RenamePayload = await request.json();
|
2026-04-14 13:22:29 +08:00
|
|
|
const normalizedDocumentId = assertDocumentId(documentId);
|
|
|
|
|
const normalizedTitle = assertTitle(title);
|
|
|
|
|
const normalizedWorkspaceId = workspaceId?.trim() || null;
|
2026-04-24 06:10:18 +08:00
|
|
|
|
|
|
|
|
if (commandName !== PAGE_COMMAND_NAMES.updateTitle) {
|
|
|
|
|
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: "rename",
|
|
|
|
|
documentId: normalizedDocumentId,
|
|
|
|
|
workspaceId: normalizedWorkspaceId,
|
|
|
|
|
title: normalizedTitle,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const payload = (await response.json().catch(() => null)) as TreeRenameResponse | { 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({
|
|
|
|
|
ok: true,
|
|
|
|
|
meta: {
|
|
|
|
|
requestId: payload?.requestId,
|
|
|
|
|
traceId: payload?.traceId,
|
|
|
|
|
commandName: "tree.node.rename",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 13:22:29 +08:00
|
|
|
const bridgeContext = await buildDocumentBridgeContext({ request, workspaceId: normalizedWorkspaceId });
|
|
|
|
|
const envelope = buildDocumentCommandEnvelope({
|
2026-04-23 07:38:34 +08:00
|
|
|
name: PAGE_COMMAND_NAMES.updateTitle,
|
2026-04-14 13:22:29 +08:00
|
|
|
payload: {
|
|
|
|
|
documentId: normalizedDocumentId,
|
|
|
|
|
workspaceId: normalizedWorkspaceId,
|
|
|
|
|
title: normalizedTitle,
|
|
|
|
|
} satisfies DocumentTitleUpdatePayload,
|
|
|
|
|
context: bridgeContext,
|
|
|
|
|
target: {
|
|
|
|
|
workspaceId: normalizedWorkspaceId,
|
|
|
|
|
pageId: normalizedDocumentId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-04-23 07:38:34 +08:00
|
|
|
const result = await executePageWriteBridgeCommand({
|
2026-04-14 13:22:29 +08:00
|
|
|
context: bridgeContext,
|
|
|
|
|
envelope,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
ok: true,
|
|
|
|
|
meta: {
|
|
|
|
|
requestId: result.requestId,
|
|
|
|
|
traceId: result.traceId,
|
|
|
|
|
commandId: result.commandId,
|
|
|
|
|
commandName: result.commandName,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return documentBridgeErrorResponse(error);
|
|
|
|
|
}
|
2026-01-17 10:12:53 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2025-11-23 10:55:04 +08:00
|
|
|
}
|
2026-04-15 20:01:12 +08:00
|
|
|
|
|
|
|
|
export const runtime = "nodejs";
|