Ported from babyboo and trimmed to back-office labels: list with search and the Missing Chinese filter, edit panel with placeholder checks and an unsaved-changes guard, export, import with preview and summary, and the Languages page with guarded switches. A blank translation is no longer treated as an old placeholder mismatch, so Save blocks a broken new one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
58 lines
2.6 KiB
TypeScript
58 lines
2.6 KiB
TypeScript
import { NextResponse, type NextRequest } from "next/server";
|
|
import { callMobileApi } from "@/app/lib/server/mobileApi";
|
|
import { getToken } from "@/app/lib/server/session";
|
|
import { badRequest, errorResponse, unauthorized } from "@/app/lib/server/apiRoute";
|
|
|
|
// Proxy for /api/admin/messages (area A labels). mobile-api owns the BO_MESSAGES guard.
|
|
// Reads: list, /{id}, /export. Writes: PUT /{id} (content only), POST /import. No create, no delete.
|
|
type Ctx = { params: Promise<{ path?: string[] }> };
|
|
const ID = /^\d+$/;
|
|
|
|
export async function GET(req: NextRequest, { params }: Ctx) {
|
|
const token = await getToken();
|
|
if (!token) return unauthorized();
|
|
const { path = [] } = await params;
|
|
if (path.length === 0) {
|
|
const q = new URL(req.url).searchParams;
|
|
const out = new URLSearchParams();
|
|
for (const key of ["searchKey", "missingLocale"]) {
|
|
const v = q.get(key);
|
|
if (v) out.set(key, v);
|
|
}
|
|
out.set("page", String(Number(q.get("page")) || 0));
|
|
out.set("size", String(Number(q.get("size")) || 20));
|
|
return forward(`/api/admin/messages?${out}`, { token });
|
|
}
|
|
if (path.length === 1 && (path[0] === "export" || ID.test(path[0]))) {
|
|
return forward(`/api/admin/messages/${encodeURIComponent(path[0])}`, { token });
|
|
}
|
|
return badRequest("Invalid path.");
|
|
}
|
|
|
|
export async function PUT(req: NextRequest, { params }: Ctx) {
|
|
const token = await getToken();
|
|
if (!token) return unauthorized();
|
|
const { path = [] } = await params;
|
|
if (path.length !== 1 || !ID.test(path[0])) return badRequest("Invalid path.");
|
|
const body = await req.json().catch(() => ({}));
|
|
const content = (body as { content?: unknown }).content;
|
|
if (typeof content !== "string") return badRequest("content is required.");
|
|
return forward(`/api/admin/messages/${encodeURIComponent(path[0])}`, { method: "PUT", body: { content }, token });
|
|
}
|
|
|
|
export async function POST(req: NextRequest, { params }: Ctx) {
|
|
const token = await getToken();
|
|
if (!token) return unauthorized();
|
|
const { path = [] } = await params;
|
|
if (path.length !== 1 || path[0] !== "import") return badRequest("Invalid path.");
|
|
const rows = await req.json().catch(() => null);
|
|
if (!Array.isArray(rows) || rows.length === 0) return badRequest("A non-empty JSON array of messages is required.");
|
|
return forward("/api/admin/messages/import", { method: "POST", body: rows, token });
|
|
}
|
|
|
|
async function forward(target: string, opts: Parameters<typeof callMobileApi>[1]) {
|
|
const result = await callMobileApi<unknown>(target, opts);
|
|
if (!result.ok) return errorResponse(result);
|
|
return NextResponse.json(result.data ?? { ok: true });
|
|
}
|