import { exportTarget, isQuotaListPath, isQuotaResultPath, isReportStreamPath, isProjectReadPath, projectReadQuery, } 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" | "projects" | "quota" | "report" | "excel" | "respondent" | "share"; }; /** Linked lookup only: without all three keys the server would list the API key owner's projects. */ export function readInternalProjectListQuery(search: URLSearchParams) { const query = projectReadQuery(["list"], search); if ( !["0", "1"].includes(query.projectType ?? "") || !query.intranetCode?.trim() || !query.userTeam?.trim() ) throw new Error("projectType(0|1), intranetCode, userTeam을 함께 입력해 주세요."); return query; } /** 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) { return readReportBatch( isRecord(value) && typeof value.filter === "string" ? { ...value, filter: parseFilterExpression(value.filter) } : value, ); } /** 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 === "GET" && area === "project" && parts.length === 1 && parts[0] === "list") return { path, kind: "projects" }; 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)) return { path, kind: "quota" }; if ( area === "export" && parts.length === 3 && ["table", "quota"].includes(parts[2]) ) { const target = exportTarget("POST", parts); if (target) return { path: target.path, kind: "excel" }; } return null; } /** Broader backend quota contract, without changing the ordinary screen's request policy. */ export function readInternalQuotaRequest(value: unknown) { if (!isRecord(value)) throw new Error("쿼터 조회 조건을 확인해 주세요."); const dataScope = value.dataScope ?? "ACTUAL"; const startDate = value.startDate ?? ""; const endDate = value.endDate ?? ""; if ( !["ACTUAL", "TEST", "ALL"].includes(String(dataScope)) || typeof dataScope !== "string" || typeof startDate !== "string" || typeof endDate !== "string" || !validDateRange({ startDate, endDate }) ) throw new Error("쿼터 데이터 범위와 조회 날짜를 확인해 주세요."); return { dataScope, ...(startDate ? { startDate, endDate } : {}) }; }