Files
Aislo/M02_MasterTemplete/DataFlow/M02_DF_Material.ts
T

50 lines
1.7 KiB
TypeScript

/* =============================================================================
* M02_DF_Material.ts
* 검증 칸이 쓰는 재료 찾기 · 이름 · 단가 — 서버 `GET /api/m02/df/materials`(Api `cfMaterials` ·
* 띄어쓰기로 나눈 말을 모두 포함 · 쪽 넘기기) · 단가 = 그 줄의 단가(값을 받는 길과 같은 셈)
* 모양은 docs/raw/M01_쉬운단가/흐름도_정함.md 5. 이 폴더째 지워도 됨.
* ========================================================================== */
import { cfMaterials, type CfMaterial } from "./M02_DF_Api";
export type CfMat = CfMaterial;
export interface CfMatPage {
total: number;
page: number;
size: number;
items: CfMat[];
}
/** 재료 찾기 — q = 찾는 말 · sub = 구분 · detail = 상세구분 · page 는 1 부터 */
export function findMaterials(cond: {
q?: string;
sub?: string;
detail?: string;
page?: number;
size?: number;
}): Promise<CfMatPage> {
return cfMaterials({ ...cond, page: cond.page ?? 1, size: cond.size ?? 10 });
}
const byKey = new Map<string, CfMat | null>();
/** 재료 키 → 그 줄(이름 · 규격 · 단가) — 한 번 물은 것은 다시 안 물음 */
export async function materialOf(key: string): Promise<CfMat | null> {
if (!key) return null;
if (byKey.has(key)) return byKey.get(key) ?? null;
let hit: CfMat | null = null;
try {
hit = (await cfMaterials({ q: key, size: 20 })).items.find((m) => m.ref === key) ?? null;
} catch {
hit = null;
}
byKey.set(key, hit);
return hit;
}
/** 재료 키 → 단가(원) — 못 구하면 null */
export async function priceOf(key: string): Promise<number | null> {
return (await materialOf(key))?.price ?? null;
}