Files
co-lite/solvway-admin-portal/app/api/messages/[[...path]]/route.ts
T
ngqileandClaude Opus 5.5 918f5fa2b2 feat(portal): Co-Lite labels only filter on Translations
On by default. The portal server sends its own label codes to the new
search; unchecking lists every label as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-02 14:55:56 +08:00

71 lines
3.2 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";
import { DEFAULT_MESSAGES } from "@/app/lib/i18n/defaultMessages";
// Proxy for /api/admin/messages (area A labels). mobile-api owns the BO_MESSAGES guard.
// Reads: list (own=1 limits it to the portal's own label codes), /{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;
if (q.get("own") === "1") {
// Co-Lite labels only: the codes come from the built-in list here, never from the browser.
const body: Record<string, unknown> = {};
for (const key of ["searchKey", "missingLocale"]) {
const v = q.get(key);
if (v) body[key] = v;
}
body.page = Number(q.get("page")) || 0;
body.size = Number(q.get("size")) || 20;
body.codes = Object.keys(DEFAULT_MESSAGES);
return forward("/api/admin/messages/search", { method: "POST", body, token });
}
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 });
}