/* ============================================================================= * ui_template_item_picker_project.ts * 항목 고르기 모달의 **자료 창구**(계획서 25-2d) — 프로젝트 복사본 · 입력 목록 · 계산을 한 곳에서 부름. * * 프로젝트 복사본(sub6 · 25-2b) * GET /api/projects/{id}/items → {항목: [{폴더, 이름, 원본, 원본판, 가져온날, 마스터바뀜, …}]} * POST /api/projects/{id}/items {layer, owner, key} 미리 가져오기 → {폴더, 만듦, 가져온날} * GET|PUT /api/projects/{id}/items/{폴더}/{부분} → {종류, 판, 내용} · 고침은 {판, 내용}(판이 다르면 409) * POST …/{폴더}/refresh 새로 가져오기(입력값은 같은 키면 살림) * POST …/{폴더}/calc 복사본만으로 계산(계 · 빠진 입력) * 입력 목록 · 항목 연결(sub4 · 25-2a) — `M02_MasterTemplete_Api_Fetch` 의 `fetchItemInputs` · `readInputLinks` * ========================================================================== */ import { API_BASE_URL } from "@config/config_frontend"; import { fetchItemInputs, type ItemInputs, } from "../M02_MasterTemplete/M02_MasterTemplete_Api_Fetch"; import { layerOf, ownerOf, type PickerRow } from "./ui_template_item_picker_data"; /** 복사본 폴더 안 부분 — 25-1b 약속 그대로 */ export type ProjectPart = "도면" | "면적" | "재료" | "자재조합" | "입력연결" | "입력값" | "고른자재"; /** 원본 종류 → 복사본 부분 */ export const PART_OF: Record<"structure" | "area" | "material", ProjectPart> = { structure: "도면", area: "면적", material: "재료", }; export interface ProjectItemInfo { 폴더: string; 이름?: string | null; 원본: { layer: string; owner: string | null; key: string; name?: string }; 원본판?: string | null; 가져온날?: string | null; /** 원본(마스터 · 회사 · 개인)이 가져온 뒤 바뀜 */ 마스터바뀜?: boolean; 원본없음?: boolean; 구조물?: string[]; 고침?: boolean; } /** `{층}_{주인 슬러그}_{키}` — 서버 `snapshot_folder_name` 과 같은 글(owner 없음 = none · `/` = `-`) */ export const folderOf = (row: PickerRow): string => `${layerOf(row)}_${(ownerOf(row) ?? "none").replace(/\//g, "-")}_${row.key}`; /** 복사본 계산 결과에서 모달이 보이는 것 — 계 · 빠진 입력 수 · 멈춘 로직 수 */ export interface ProjectCalc { total: number; gaps: number; stuck: number; } interface Reply { status: number; body: unknown; } const enc = encodeURIComponent; /** 서버가 막은 까닭 그대로 — 문장 · `{message}`(409) · 입력 검사 목록(`[{msg}]`) */ function reasonOf(reply: Reply): string { const detail = (reply.body as { detail?: unknown } | null)?.detail; if (typeof detail === "string" && detail) return detail; const message = (detail as { message?: unknown } | null)?.message; if (typeof message === "string" && message) return message; if (Array.isArray(detail)) { const lines = detail .map((one) => String((one as { msg?: unknown })?.msg ?? "")) .filter(Boolean); if (lines.length) return lines.join(" · "); } return `HTTP ${reply.status}`; } async function call(path: string, init: RequestInit = {}): Promise { const response = await fetch(`${API_BASE_URL}${path}`, { credentials: "include", ...init, headers: init.body ? { "Content-Type": "application/json" } : undefined, }); return { status: response.status, body: await response.json().catch(() => null) }; } const post = (body: unknown): RequestInit => ({ method: "POST", body: JSON.stringify(body) }); export interface ProjectItems { /** 프로젝트에 가져온 항목 목록 — 서버가 항목당 0.5초쯤 걸려 모달이 열 때 한 번만 부르고 들고 있음 */ list: () => Promise; /** 복사본 부분 하나(`내용`) — 없으면 null · 판은 고칠 때를 위해 들고 있음 */ read: (folder: string, part: ProjectPart) => Promise; /** 복사본 고침 — 못 하면 서버 까닭 그대로 던짐(권한 · 판이 다름 …) */ write: (folder: string, part: ProjectPart, doc: unknown) => Promise; /** 프로젝트로 미리 가져옴 */ importItem: (row: PickerRow) => Promise; /** 새로 가져옴 — 복사본을 원본으로 덮음(넣은 값은 같은 키면 서버가 살림) */ refresh: (info: ProjectItemInfo) => Promise; /** 복사본만으로 계산 — 계산할 것이 없거나 못 하면 null */ calc: (folder: string) => Promise; } const clients = new Map(); /** 프로젝트 하나의 창구 — 같은 프로젝트는 한 벌(고칠 때 쓸 판을 들고 있음) */ export function projectItems(projectId: string): ProjectItems { const kept = clients.get(projectId); if (kept) return kept; const base = `/projects/${enc(projectId)}/items`; const versions = new Map(); const at = (folder: string, part: string): string => `${base}/${enc(folder)}/${enc(part)}`; const client: ProjectItems = { list: async () => { const reply = await call(base); const rows = (reply.body as { 항목?: ProjectItemInfo[] } | null)?.항목; if (reply.status !== 200 || !Array.isArray(rows)) throw new Error(reasonOf(reply)); return rows; }, read: async (folder, part) => { const reply = await call(at(folder, part)); if (reply.status === 404) return null; if (reply.status !== 200) throw new Error(reasonOf(reply)); const got = reply.body as { 판?: string | null; 내용?: unknown }; versions.set(`${folder}/${part}`, got.판 ?? null); return got.내용 ?? null; }, write: async (folder, part, doc) => { const key = `${folder}/${part}`; if (!versions.has(key)) await client.read(folder, part); // 고칠 판을 먼저 앎 const reply = await call(at(folder, part), { method: "PUT", body: JSON.stringify({ 판: versions.get(key) ?? null, 내용: doc }), }); if (reply.status >= 400) throw new Error(reasonOf(reply)); versions.set(key, (reply.body as { 판?: string } | null)?.판 ?? null); }, importItem: async (row) => { const ref = { layer: layerOf(row), owner: ownerOf(row), key: row.key }; const reply = await call(base, post(ref)); if (reply.status >= 300) throw new Error(reasonOf(reply)); const got = reply.body as { 폴더: string; 가져온날?: string | null }; return { 폴더: got.폴더, 원본: ref, 가져온날: got.가져온날, 마스터바뀜: false }; }, refresh: async (info) => { const reply = await call(`${base}/${enc(info.폴더)}/refresh`, post({})); if (reply.status >= 300) throw new Error(reasonOf(reply)); for (const key of [...versions.keys()]) if (key.startsWith(`${info.폴더}/`)) versions.delete(key); inputsCache.clear(); // 원본이 바뀌었을 수 있어 입력 목록도 다시 받음 const got = reply.body as { 가져온날?: string | null; 원본판?: string | null }; return { ...info, 가져온날: got.가져온날, 원본판: got.원본판, 마스터바뀜: false, 고침: false, }; }, calc: async (folder) => { const reply = await call(`${base}/${enc(folder)}/calc`, post({})); if (reply.status !== 200) return null; const got = reply.body as { 계?: number; 빠진입력?: unknown[]; 멈춤?: unknown[] }; return { total: Number(got.계 ?? 0), gaps: got.빠진입력?.length ?? 0, stuck: got.멈춤?.length ?? 0, }; }, }; clients.set(projectId, client); return client; } /* ── 입력 목록(원본 층 기준) — 같은 항목은 한 번만 받음 ───────────────────────── */ const inputsCache = new Map>(); /** 원본 항목의 입력 목록(로직마다 갈라 · 합치지 않음) — 못 받으면 다음에 다시 */ export function loadInputs(row: PickerRow): Promise { const id = `${layerOf(row)}|${ownerOf(row) ?? ""}|${row.key}`; const kept = inputsCache.get(id); if (kept) return kept; const made = fetchItemInputs(layerOf(row), row.key); inputsCache.set(id, made); made.catch(() => inputsCache.delete(id)); return made; }