2026-01-08 06:28:14 +08:00
|
|
|
import { NextResponse } from "next/server";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
|
|
|
|
import { api } from "@/lib/convex/api";
|
2026-04-15 03:06:29 +08:00
|
|
|
import { buildMindmapRouteMeta } from "@/lib/mindmap/mindmapRouteMeta";
|
2026-01-08 06:28:14 +08:00
|
|
|
|
|
|
|
|
const defaultMindmapData = {
|
|
|
|
|
data: { text: "中心主题" },
|
|
|
|
|
children: [],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function GET(
|
2026-04-15 03:06:29 +08:00
|
|
|
request: Request,
|
2026-01-08 06:28:14 +08:00
|
|
|
{ params }: { params: Promise<{ docId: string; mindmapId: string }> },
|
|
|
|
|
) {
|
|
|
|
|
const { docId, mindmapId } = await params;
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
if (isConvexEnabled()) {
|
2026-04-15 03:06:29 +08:00
|
|
|
const { auth, client } = await getAuthedConvexClient();
|
2026-01-18 19:01:31 +08:00
|
|
|
const res = await client.query(api.mindmaps.get, { docId, mindmapId });
|
2026-04-15 03:06:29 +08:00
|
|
|
return NextResponse.json({
|
|
|
|
|
data: res?.data ?? defaultMindmapData,
|
|
|
|
|
source: "convex",
|
|
|
|
|
meta: {
|
|
|
|
|
...buildMindmapRouteMeta(request, {
|
|
|
|
|
workspaceId: res?.meta?.workspace_id ?? null,
|
|
|
|
|
documentId: docId,
|
|
|
|
|
mindmapId,
|
|
|
|
|
ownerUserId: auth.userId,
|
|
|
|
|
}),
|
|
|
|
|
exists: Boolean(res?.meta?.exists),
|
|
|
|
|
deletedAt: res?.meta?.deleted_at ?? null,
|
|
|
|
|
createdAt: res?.meta?.created_at ?? null,
|
|
|
|
|
updatedAt: res?.meta?.updated_at ?? null,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-01-17 10:12:53 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2026-01-08 06:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function POST(
|
|
|
|
|
request: Request,
|
|
|
|
|
{ params }: { params: Promise<{ docId: string; mindmapId: string }> },
|
|
|
|
|
) {
|
|
|
|
|
const { docId, mindmapId } = await params;
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
if (isConvexEnabled()) {
|
2026-04-15 03:06:29 +08:00
|
|
|
const { auth, client } = await getAuthedConvexClient();
|
2026-01-17 10:12:53 +08:00
|
|
|
const { data, createOnly } = (await request.json().catch(() => ({ data: null }))) as {
|
|
|
|
|
data?: unknown;
|
|
|
|
|
createOnly?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const result = await client.mutation(api.mindmaps.put, {
|
|
|
|
|
docId,
|
|
|
|
|
mindmapId,
|
|
|
|
|
data: data ?? defaultMindmapData,
|
|
|
|
|
...(typeof createOnly === "boolean" ? { createOnly } : {}),
|
|
|
|
|
});
|
2026-04-15 03:06:29 +08:00
|
|
|
return NextResponse.json({
|
|
|
|
|
...(result ?? { ok: true }),
|
|
|
|
|
meta: {
|
|
|
|
|
...buildMindmapRouteMeta(request, {
|
|
|
|
|
workspaceId: result?.workspace_id ?? null,
|
|
|
|
|
documentId: docId,
|
|
|
|
|
mindmapId,
|
|
|
|
|
ownerUserId: auth.userId,
|
|
|
|
|
}),
|
|
|
|
|
updatedAt: result?.updated_at ?? null,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-01-17 10:12:53 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
return NextResponse.json({ error: (error as Error).message }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2026-01-08 06:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function DELETE(
|
2026-04-15 03:06:29 +08:00
|
|
|
request: Request,
|
2026-01-08 06:28:14 +08:00
|
|
|
{ params }: { params: Promise<{ docId: string; mindmapId: string }> },
|
|
|
|
|
) {
|
|
|
|
|
const { docId, mindmapId } = await params;
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
if (isConvexEnabled()) {
|
2026-04-15 03:06:29 +08:00
|
|
|
const { auth, client } = await getAuthedConvexClient();
|
2026-01-17 10:12:53 +08:00
|
|
|
try {
|
2026-01-18 19:01:31 +08:00
|
|
|
const result = await client.mutation(api.mindmaps.softDelete, { docId, mindmapId });
|
2026-04-15 03:06:29 +08:00
|
|
|
return NextResponse.json({
|
|
|
|
|
...(result ?? { ok: true }),
|
|
|
|
|
meta: {
|
|
|
|
|
...buildMindmapRouteMeta(request, {
|
|
|
|
|
workspaceId: result?.workspace_id ?? null,
|
|
|
|
|
documentId: docId,
|
|
|
|
|
mindmapId,
|
|
|
|
|
ownerUserId: auth.userId,
|
|
|
|
|
}),
|
|
|
|
|
deletedAt: result?.deleted_at ?? null,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-01-17 10:12:53 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
return NextResponse.json({ error: (error as Error).message }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2026-01-10 10:35:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function PATCH(
|
|
|
|
|
request: Request,
|
|
|
|
|
{ params }: { params: Promise<{ docId: string; mindmapId: string }> },
|
|
|
|
|
) {
|
|
|
|
|
const { docId, mindmapId } = await params;
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
if (isConvexEnabled()) {
|
2026-04-15 03:06:29 +08:00
|
|
|
const { auth, client } = await getAuthedConvexClient();
|
2026-01-17 10:12:53 +08:00
|
|
|
const { action } = (await request.json().catch(() => ({}))) as { action?: string };
|
|
|
|
|
if (action !== "restore" && action !== "purge") {
|
|
|
|
|
return NextResponse.json({ error: "不支持的操作" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (action === "purge") {
|
2026-01-18 19:01:31 +08:00
|
|
|
const result = await client.mutation(api.mindmaps.purge, { docId, mindmapId });
|
2026-04-15 03:06:29 +08:00
|
|
|
return NextResponse.json({
|
|
|
|
|
...(result ?? { ok: true }),
|
|
|
|
|
meta: buildMindmapRouteMeta(request, {
|
|
|
|
|
workspaceId: result?.workspace_id ?? null,
|
|
|
|
|
documentId: docId,
|
|
|
|
|
mindmapId,
|
|
|
|
|
ownerUserId: auth.userId,
|
|
|
|
|
}),
|
|
|
|
|
});
|
2026-01-17 10:12:53 +08:00
|
|
|
}
|
2026-01-18 19:01:31 +08:00
|
|
|
const result = await client.mutation(api.mindmaps.restore, { docId, mindmapId });
|
2026-04-15 03:06:29 +08:00
|
|
|
return NextResponse.json({
|
|
|
|
|
...(result ?? { ok: true }),
|
|
|
|
|
meta: {
|
|
|
|
|
...buildMindmapRouteMeta(request, {
|
|
|
|
|
workspaceId: result?.workspace_id ?? null,
|
|
|
|
|
documentId: docId,
|
|
|
|
|
mindmapId,
|
|
|
|
|
ownerUserId: auth.userId,
|
|
|
|
|
}),
|
|
|
|
|
updatedAt: result?.updated_at ?? null,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-01-17 10:12:53 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
const msg = (error as Error).message ?? "操作失败";
|
|
|
|
|
const status = msg.includes("未找到") ? 404 : 400;
|
|
|
|
|
return NextResponse.json({ error: msg }, { status });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2026-01-08 06:28:14 +08:00
|
|
|
}
|