261027_01_ENH: 사내 연동 API에 url copy 추가

This commit is contained in:
2026-10-27 09:03:18 +09:00
parent d4747b0ea0
commit a1f39c74de
4 changed files with 118 additions and 56 deletions

View File

@@ -5,8 +5,11 @@ import {
internalTarget,
readInternalQuotaRequest,
readInternalReportBatch,
readInternalShareRequest,
} from "@/lib/kric-internal-policy";
import { readExcelRequest } from "@/types/export";
import { isRecord } from "@/types/kart";
import { isShareLink } from "@/types/share";
import type { RouteContext } from "@/types/common";
export const maxDuration = 660;
@@ -22,7 +25,7 @@ async function handle(request: NextRequest, context: RouteContext) {
if (!apiKey) return jsonError("X-API-Key는 필수입니다.", 401);
let body: unknown;
if (target.kind !== "list") {
if (target.kind !== "list" && target.kind !== "respondent") {
try {
const input: unknown = await request.json();
body =
@@ -30,7 +33,9 @@ async function handle(request: NextRequest, context: RouteContext) {
? readInternalReportBatch(input)
: target.kind === "quota"
? readInternalQuotaRequest(input)
: readExcelRequest(input);
: target.kind === "share"
? readInternalShareRequest(input)
: readExcelRequest(input);
} catch (error) {
return jsonError(
error instanceof SyntaxError
@@ -46,11 +51,26 @@ async function handle(request: NextRequest, context: RouteContext) {
try {
const api = createServerApiWithApiKey(apiKey);
const options = { signal: request.signal, cache: "no-store" as const };
if (target.kind === "list" || target.kind === "quota") {
if (
target.kind === "list" ||
target.kind === "quota" ||
target.kind === "respondent" ||
target.kind === "share"
) {
const result =
target.kind === "list"
? await api.get<unknown>(target.path, options)
: await api.post<unknown>(target.path, body, options);
if (
target.kind === "share" &&
(!isShareLink(result) ||
result.projectType !== Number(slug[1]) ||
result.pkey !== slug[2] ||
result.pageOnly ||
!isRecord(body) ||
result.realUrl !== body.realUrl)
)
return jsonError("공유 링크 응답을 확인해 주세요.", 502);
return NextResponse.json(result, { headers: noStore });
}
const upstream = await api.streamPost(target.path, body, {

View File

@@ -3,12 +3,26 @@ import {
isQuotaListPath,
isQuotaResultPath,
isReportStreamPath,
isProjectReadPath,
} from "@/lib/project-proxy-policy";
import { isRecord, validDateRange } from "@/types/kart";
import { readReportBatch } from "@/lib/report-contract";
import { parseFilterExpression } from "@/lib/filter-expression";
import { reportUrl } from "@/types/share";
type Target = { path: string; kind: "list" | "quota" | "report" | "excel" };
type Target = {
path: string;
kind: "list" | "quota" | "report" | "excel" | "respondent" | "share";
};
/** URL Copy only: the server resolves the project from the authenticated path. */
export function readInternalShareRequest(value: unknown) {
if (!isRecord(value) || value.mode !== "U")
throw new Error("URL Copy 요청은 mode를 U로 지정해 주세요.");
const realUrl = reportUrl(value.realUrl);
if (!realUrl) throw new Error("공유할 보고서 화면 경로를 확인해 주세요.");
return { realUrl, mode: "U" as const };
}
/** Adapt integration input to the existing report contract without coupling the parser to HTTP. */
export function readInternalReportBatch(value: unknown) {
@@ -19,13 +33,41 @@ export function readInternalReportBatch(value: unknown) {
);
}
/** Only the five approved integration operations; no arbitrary upstream forwarding. */
/** Fixed integration operations only; no arbitrary upstream forwarding. */
export function internalTarget(method: string, slug: string[]): Target | null {
const [area, ...parts] = slug;
const path = "/" + slug.map(encodeURIComponent).join("/");
if (method === "GET" && area === "quota" && isQuotaListPath(parts))
return { path, kind: "list" };
if (method !== "POST") return null;
if (
area === "project" &&
parts.length === 3 &&
parts[2] === "share" &&
isProjectReadPath([parts[0], parts[1], "question", "list"])
)
return { path, kind: "share" };
if (
area === "project" &&
parts.length === 6 &&
parts[2] === "derived" &&
parts[3] === "respondent" &&
parts[5] === "recalculate" &&
isProjectReadPath([parts[0], parts[1], "question", "list"])
) {
const id = parts[4];
if (
id.trim() &&
id.length <= 512 &&
id !== "." &&
id !== ".." &&
!/[\/\\]/.test(id) &&
!Array.from(id).some(
(char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127,
)
)
return { path, kind: "respondent" };
}
if (area === "project" && isReportStreamPath(parts))
return { path, kind: "report" };
if (area === "quota" && isQuotaResultPath(parts))