- copyArea · copyMaterial · fetchStructureNumbers → {도번, 면적번, 재료번} · Kind area · material
- 옛 이름 copyBasis · 표번 · Kind basis 는 sub2 가 옮길 때까지 별칭(@deprecated)
- 좌측 목록 셋 · 줄 글 = 번호 맨 앞 + 이름 + 변수값
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EUypcnp5d1gU2aeKh9F2H7
181 lines
7.6 KiB
TypeScript
181 lines
7.6 KiB
TypeScript
/* =============================================================================
|
|
* M02_MasterTemplete_Api_Fetch.ts
|
|
* 마스터 템플릿 서버 호출 — 계약 `6_계약.md` 서버 길 · 시스템 층은 시스템 관리자만
|
|
* ========================================================================== */
|
|
|
|
import { API_BASE_URL } from "@config/config_frontend";
|
|
|
|
/** 층 이름 — 서버와 같은 글 */
|
|
export type Layer = "system" | "company" | "personal" | "project";
|
|
/** 서버 종류 — 화면의 「표 양식」 = table · 「도면 양식」 = drawing · 「구조물 도면」 = structure ·
|
|
* 「면적산출」 = area · 「재료산출」 = material(둘 다 구조물집계표 열마다 한 통합문서) ·
|
|
* @deprecated "basis" — 옛 이름(서버 길은 area 로 보냄) · sub2 가 새 이름으로 옮기면 지움 */
|
|
export type Kind = "table" | "drawing" | "structure" | "area" | "material" | "basis";
|
|
|
|
export interface TemplateInfo {
|
|
종류: Kind;
|
|
이름: string;
|
|
판: string;
|
|
수정일: string;
|
|
}
|
|
|
|
export interface TemplateDoc {
|
|
종류: Kind;
|
|
이름: string;
|
|
/** 프로젝트 층에서 작업본이 아직 없으면(옛 프로젝트) 시스템 양식이 와서 null */
|
|
판: string | null;
|
|
문서: unknown;
|
|
}
|
|
|
|
/** 서버가 판 불일치로 막았을 때 */
|
|
export class StaleError extends Error {}
|
|
|
|
async function call<T>(path: string, init: RequestInit = {}): Promise<T> {
|
|
const response = await fetch(`${API_BASE_URL}/m02${path}`, {
|
|
credentials: "include",
|
|
headers: { "Content-Type": "application/json" },
|
|
...init,
|
|
});
|
|
const body = (await response.json().catch(() => ({}))) as { detail?: unknown } & T;
|
|
if (response.status === 409)
|
|
throw new StaleError(typeof body.detail === "string" ? body.detail : "409");
|
|
if (!response.ok) {
|
|
const detail = body.detail;
|
|
throw new Error(typeof detail === "string" ? detail : `HTTP ${response.status}`);
|
|
}
|
|
return body;
|
|
}
|
|
|
|
const enc = encodeURIComponent;
|
|
const q = (projectId: string | null): string => (projectId ? `?project_id=${enc(projectId)}` : "");
|
|
const head = (layer: Layer): string =>
|
|
layer === "system" ? "/templates" : `/layers/${layer}/templates`;
|
|
/** 서버 길의 종류 — 옛 "basis" 는 "area" 로 */
|
|
const kindPath = (kind: Kind): string => (kind === "basis" ? "area" : kind);
|
|
const item = (layer: Layer, kind: Kind, name: string, projectId: string | null): string =>
|
|
`${head(layer)}/${kindPath(kind)}/${enc(name)}${layer === "system" ? "" : q(projectId)}`;
|
|
|
|
/** 시스템 길은 배열 · 층 길은 `{층, 양식: [...]}` — 둘 다 목록으로 */
|
|
export const listTemplates = async (
|
|
layer: Layer,
|
|
projectId: string | null,
|
|
): Promise<TemplateInfo[]> => {
|
|
const got = await call<TemplateInfo[] | { 양식: TemplateInfo[] }>(
|
|
layer === "system" ? head(layer) : `${head(layer)}${q(projectId)}`,
|
|
);
|
|
return Array.isArray(got) ? got : got.양식;
|
|
};
|
|
|
|
export const readTemplate = (
|
|
layer: Layer,
|
|
kind: Kind,
|
|
name: string,
|
|
projectId: string | null,
|
|
): Promise<TemplateDoc> => call(item(layer, kind, name, projectId));
|
|
|
|
export const saveTemplate = (
|
|
layer: Layer,
|
|
kind: Kind,
|
|
name: string,
|
|
projectId: string | null,
|
|
판: string,
|
|
문서: unknown,
|
|
): Promise<TemplateInfo> =>
|
|
call(item(layer, kind, name, projectId), { method: "PUT", body: JSON.stringify({ 판, 문서 }) });
|
|
|
|
/** 시스템 층만 지움 */
|
|
export const deleteTemplate = (kind: Kind, name: string, 판: string): Promise<{ ok: boolean }> =>
|
|
call(`/templates/${kindPath(kind)}/${enc(name)}?판=${enc(판)}`, { method: "DELETE" });
|
|
|
|
/** 개인·회사 층에서 지움 — 판이 맞을 때만(다르면 409) */
|
|
export const deleteLayerTemplate = (
|
|
layer: Layer,
|
|
kind: Kind,
|
|
name: string,
|
|
판: string,
|
|
): Promise<{ ok: boolean }> =>
|
|
call(`/layers/${layer}/templates/${kindPath(kind)}/${enc(name)}?판=${enc(판)}`, {
|
|
method: "DELETE",
|
|
});
|
|
|
|
/* --- 구조물 도면(종류 structure · 이름 = 구조물집계표 열 id) ---
|
|
* 읽기 · 저장 · 지우기는 위 readTemplate · saveTemplate · deleteTemplate 에 "structure" */
|
|
|
|
/** 구조물 도면 문서 — `도면` = 도면 양식과 같은 CAD 문서 · 면적산출 · 재료산출은 따로(같은 열 id) */
|
|
export interface StructureDoc {
|
|
양식: "구조물도면";
|
|
종류: "structure";
|
|
판: number;
|
|
열: string;
|
|
도번: string;
|
|
도면: { entities: unknown[] } & Record<string, unknown>;
|
|
}
|
|
|
|
/** 시스템 층 구조물 도면 목록 */
|
|
export const listStructures = async (): Promise<TemplateInfo[]> =>
|
|
(await listTemplates("system", null)).filter((t) => t.종류 === "structure");
|
|
|
|
/** 새로 — 도번 자동 · 빈 도면 (+ 없으면 빈 면적산출 · 재료산출 통합문서) · 이미 있으면 StaleError */
|
|
export const createStructure = (column: string): Promise<TemplateDoc> =>
|
|
call(`/structures/${enc(column)}`, { method: "POST" });
|
|
|
|
/** 한 열의 번호 — 도번 `SD` · 면적번 `SA` · 재료번 `SM` + 같은 8자리
|
|
* [종류][분류1][분류2][분류3](없는 쪽은 빈 글) */
|
|
export interface StructureNumbers {
|
|
도번: string;
|
|
면적번: string;
|
|
재료번: string;
|
|
/** @deprecated 옛 이름 — 면적번과 같은 값 · sub2 가 옮기면 지움 */
|
|
표번?: string;
|
|
}
|
|
|
|
/** `{열 id: {도번, 면적번, 재료번}}` — 구조물 열만(글 열 · 없는 열은 키 없음 = 「없음」) */
|
|
export const fetchStructureNumbers = async (): Promise<Record<string, StructureNumbers>> => {
|
|
const got = await call<Record<string, StructureNumbers>>("/structures/numbers");
|
|
for (const row of Object.values(got)) row.표번 = row.면적번;
|
|
return got;
|
|
};
|
|
|
|
/** 다른 열(`from`) 구조물 도면 내용을 받는 열(`column`)로 가져옴 — 도번 · 열은 받는 쪽 그대로 */
|
|
export const copyStructure = async (column: string, from: string): Promise<void> => {
|
|
await call(`/structures/${enc(column)}/copy?from=${enc(from)}`, { method: "POST" });
|
|
};
|
|
|
|
/** 다른 열(`from`) 면적산출 내용(알약 연결 포함)을 받는 열(`column`)로 가져옴 — 면적번 · 열은 그대로 */
|
|
export const copyArea = async (column: string, from: string): Promise<void> => {
|
|
await call(`/area/${enc(column)}/copy?from=${enc(from)}`, { method: "POST" });
|
|
};
|
|
|
|
/** 다른 열(`from`) 재료산출 내용(알약 연결 포함)을 받는 열(`column`)로 가져옴 — 재료번 · 열은 그대로 */
|
|
export const copyMaterial = async (column: string, from: string): Promise<void> => {
|
|
await call(`/material/${enc(column)}/copy?from=${enc(from)}`, { method: "POST" });
|
|
};
|
|
|
|
/** @deprecated 옛 이름 — copyArea · sub2 가 옮기면 지움 */
|
|
export const copyBasis = copyArea;
|
|
|
|
/* --- 프로젝트 층 단추 다섯 --- */
|
|
const project = (id: string, tail: string): string => `/projects/${enc(id)}/templates/${tail}`;
|
|
const post = (path: string, body: object = {}): Promise<unknown> =>
|
|
call(path, { method: "POST", body: JSON.stringify(body) });
|
|
|
|
export const resetProject = (id: string): Promise<unknown> => post(project(id, "reset"));
|
|
|
|
export const applyToProject = (
|
|
id: string,
|
|
from: { from: Layer; user_id?: string; project_id?: string; 종류?: Kind; 이름?: string },
|
|
): Promise<unknown> => post(project(id, "apply"), from);
|
|
|
|
export const saveProjectAs = (
|
|
id: string,
|
|
to: "personal" | "company",
|
|
종류: Kind,
|
|
이름: string,
|
|
): Promise<unknown> => post(project(id, "save-as"), { to, 종류, 이름 });
|
|
|
|
export const fetchSources = (id: string): Promise<unknown> => call(`/projects/${enc(id)}/sources`);
|
|
|
|
/** 설계값을 채운 표 문서 — 서버가 그때그때 채움 · 저장 안 함 */
|
|
export const readFilled = (id: string, name: string): Promise<TemplateDoc> =>
|
|
call(`/projects/${enc(id)}/tables/${enc(name)}/filled`);
|