Files
vexplor_dev/frontend/lib/utils/categoryFlatten.ts
kjs fa9f5451f6 Add Customer Contact Routes and Enhance User Management
- Introduced new routes for customer contact management, allowing for the retrieval of customer contact information.
- Updated the user management functionality to include validation for the hire date, ensuring proper date format and handling of null values.
- Enhanced the save user functionality to accommodate the new hire date field, maintaining existing values when not provided.

(TASK: ERP-XXX)
2026-05-18 16:13:29 +09:00

80 lines
2.6 KiB
TypeScript

/**
* TASK:ERP-049 카테고리 옵션 평탄화 공통 헬퍼
*
* 정책:
* - use_hierarchy=false → depth=1(루트) 노드만 옵션화. 부모 카테고리만 노출하고 자식은 숨김.
* - use_hierarchy=true → 잎노드(children 없는 노드)만 옵션화. 부모는 그룹 헤더 역할이라 선택 불가.
*
* 어떤 경우에도 "자식을 가진 부모 카테고리"는 셀렉트 옵션에 포함되지 않는다.
*/
export interface CategoryNodeLike {
valueCode: string;
valueLabel: string;
children?: CategoryNodeLike[];
// depth/parentValueId는 옵션에 영향 없음 (계층은 children 유무로 판단)
}
export interface CategoryOption {
code: string;
label: string;
}
/**
* 카테고리 트리에서 셀렉트 옵션 배열을 생성한다.
*
* @param values 백엔드 응답의 카테고리 트리 (buildHierarchy 결과)
* @param useHierarchy 트리 사용 여부 (null=미설정 → 자동 fallback)
*/
export function pickCategoryOptions(
values: CategoryNodeLike[] | undefined | null,
useHierarchy: boolean | null | undefined,
): CategoryOption[] {
if (!Array.isArray(values) || values.length === 0) return [];
// 자동 fallback: 어떤 노드라도 children을 가지면 true로 추정
const effectiveHierarchy =
typeof useHierarchy === "boolean"
? useHierarchy
: values.some((v) => Array.isArray(v.children) && v.children.length > 0);
if (!effectiveHierarchy) {
// depth=1(루트)만 — buildHierarchy 결과의 최상위 배열 = 루트들
return values.map((v) => ({ code: v.valueCode, label: v.valueLabel }));
}
// 잎노드만 (children이 없는 노드)
const result: CategoryOption[] = [];
const visit = (nodes: CategoryNodeLike[]) => {
for (const v of nodes) {
const kids = Array.isArray(v.children) ? v.children : [];
if (kids.length === 0) {
result.push({ code: v.valueCode, label: v.valueLabel });
} else {
visit(kids);
}
}
};
visit(values);
return result;
}
/**
* 레거시 호환: 기존 flatten 패턴(부모+자식 전체)을 그대로 평탄화한다.
* 새 코드는 pickCategoryOptions을 사용할 것.
*/
export function flattenAllCategoryNodes(values: CategoryNodeLike[] | undefined | null): CategoryOption[] {
if (!Array.isArray(values) || values.length === 0) return [];
const result: CategoryOption[] = [];
const visit = (nodes: CategoryNodeLike[]) => {
for (const v of nodes) {
result.push({ code: v.valueCode, label: v.valueLabel });
if (Array.isArray(v.children) && v.children.length > 0) {
visit(v.children);
}
}
};
visit(values);
return result;
}