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/locales. mobile-api owns the guards (list: any admin; writes: BO_LOCALES).
|
|
// The code is set on create only and never sent on update.
|
|
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) return badRequest("Invalid path.");
|
|
return forward("/api/admin/locales", { token });
|
|
}
|
|
|
|
export async function POST(req: NextRequest, { params }: Ctx) {
|
|
const token = await getToken();
|
|
if (!token) return unauthorized();
|
|
const { path = [] } = await params;
|
|
if (path.length !== 0) return badRequest("Invalid path.");
|
|
const body = (await req.json().catch(() => ({}))) as Record<string, unknown>;
|
|
const code = typeof body.code === "string" ? body.code.trim() : "";
|
|
const label = typeof body.label === "string" ? body.label.trim() : "";
|
|
if (!code || !label) return badRequest("code and label are required.");
|
|
return forward("/api/admin/locales", { method: "POST", body: { code, label, ...editable(body) }, token });
|
|
}
|
|
|
|
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(() => ({}))) as Record<string, unknown>;
|
|
const payload = editable(body);
|
|
if (typeof body.label === "string") {
|
|
if (!body.label.trim()) return badRequest("label is required.");
|
|
payload.label = body.label.trim();
|
|
}
|
|
if (Object.keys(payload).length === 0) return badRequest("At least one field is required.");
|
|
return forward(`/api/admin/locales/${encodeURIComponent(path[0])}`, { method: "PUT", body: payload, token });
|
|
}
|
|
|
|
function editable(body: Record<string, unknown>): Record<string, unknown> {
|
|
const out: Record<string, unknown> = {};
|
|
if (typeof body.enabled === "boolean") out.enabled = body.enabled;
|
|
if (typeof body.sortOrder === "number") out.sortOrder = body.sortOrder;
|
|
return out;
|
|
}
|
|
|
|
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 });
|
|
}
|