76 lines
3.8 KiB
TypeScript
76 lines
3.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { registryForwardContext } from "@/server/rag-forward-context";
|
|
import { operationDiagnostics } from "@/server/operation-diagnostics";
|
|
import {
|
|
attestRegisteredFile, attestReuploadOwner, dcRagUrl, FileRegistryError,
|
|
ragInternalHeaders, registryAuthHeaders,
|
|
} from "@/server/file-registry-attestation";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
export const maxDuration = 60;
|
|
|
|
/** Failure recording and lookup never create a document-processing job. */
|
|
export async function POST(
|
|
request: NextRequest,
|
|
context: { params: Promise<{ action: string }> },
|
|
): Promise<Response> {
|
|
const diagnostic = operationDiagnostics("conversion-failures");
|
|
let dispatched = false;
|
|
let fileSn: string | undefined;
|
|
const fail = (status: number, code: string, message: string) => diagnostic.finish(
|
|
NextResponse.json({ detail: { code, message, dispatchState: dispatched ? "UNKNOWN" : "NOT_SENT" } }, {
|
|
status, headers: { "Cache-Control": "no-store" },
|
|
}), { code, fileSn },
|
|
);
|
|
try {
|
|
const { action } = await context.params;
|
|
if (!["lookup", "record"].includes(action)) {
|
|
return fail(404, "CONVERSION_FAILURES_NOT_FOUND", "요청한 실패 이력 기능을 찾지 못했습니다.");
|
|
}
|
|
if (Number(request.headers.get("content-length") ?? 0) > 16_384) {
|
|
return fail(413, "CONVERSION_FAILURES_TOO_LARGE", "실패 이력 요청 크기를 초과했습니다.");
|
|
}
|
|
let body: Record<string, unknown>;
|
|
try {
|
|
const raw: unknown = await request.json();
|
|
if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new Error("invalid body");
|
|
body = raw as Record<string, unknown>;
|
|
} catch {
|
|
return fail(400, "CONVERSION_FAILURES_INVALID", "실패 이력 요청이 올바르지 않습니다.");
|
|
}
|
|
fileSn = typeof body.file_sn === "string" ? body.file_sn : undefined;
|
|
const projectId = typeof body.project_id === "string" ? body.project_id : "";
|
|
const classification = typeof body.clsf_systm_sn === "string" ? body.clsf_systm_sn : "";
|
|
const idField = action === "record" ? "conversion_job_id" : null;
|
|
if (!fileSn || !/^[A-Za-z0-9_-]{1,128}$/.test(fileSn) || !projectId || !classification ||
|
|
(idField && (typeof body[idField] !== "string" || !/^[a-f0-9-]{36}$/i.test(body[idField] as string)))) {
|
|
return fail(400, "CONVERSION_FAILURES_INVALID", "파일·분류·실패 작업 정보를 확인해 주세요.");
|
|
}
|
|
const registered = await attestRegisteredFile(request, { fileSn, projectId, clsfSystmSn: classification });
|
|
// Keep the same owner boundary for retained registration-failure history.
|
|
await attestReuploadOwner(request, registered);
|
|
const headers = ragInternalHeaders(registryAuthHeaders(request));
|
|
headers.set("Content-Type", "application/json");
|
|
headers.set("x-request-id", diagnostic.requestId);
|
|
const payload = {
|
|
file_sn: registered.fileSn,
|
|
registry_context: registryForwardContext(registered, headers),
|
|
...(idField ? { [idField]: body[idField] } : {}),
|
|
};
|
|
dispatched = true;
|
|
const upstream = await fetch(dcRagUrl(`/convert-to-pdf/failures/${action}`), {
|
|
method: "POST", headers, body: JSON.stringify(payload), cache: "no-store", signal: request.signal,
|
|
});
|
|
const response = new Response(await upstream.text(), {
|
|
status: upstream.status,
|
|
headers: { "Content-Type": "application/json", "Cache-Control": "no-store" },
|
|
});
|
|
return diagnostic.finish(response, { fileSn, upstreamRequestId: upstream.headers.get("x-request-id") });
|
|
} catch (error) {
|
|
if (error instanceof FileRegistryError) return fail(error.status, error.code, error.message);
|
|
return fail(request.signal.aborted ? 499 : 502, "CONVERSION_FAILURES_UNAVAILABLE",
|
|
"실패 이력 요청 결과를 확인하지 못했습니다.");
|
|
}
|
|
}
|