ERP-node/backend-node/src/services/menuService.ts

148 lines
4.1 KiB
TypeScript
Raw Normal View History

import { getPool } from "../database/db";
import { logger } from "../utils/logger";
/**
*
*
*
*/
/**
* OBJID
* ( + )
*
* :
* - /
* - (3 )
* - (parent_obj_id = 0)
* -
*
* @param menuObjid OBJID
* @returns + OBJID ( , )
*
* @example
* // 영업관리 (200)
* // ├── 고객관리 (201)
* // │ └── 고객등록 (211)
* // ├── 계약관리 (202)
* // └── 주문관리 (203)
*
* await getSiblingMenuObjids(201);
* // 결과: [201, 202, 203, 211] - 형제(202, 203) + 자식(211)
*/
export async function getSiblingMenuObjids(menuObjid: number): Promise<number[]> {
const pool = getPool();
try {
logger.debug("메뉴 스코프 조회 시작", { menuObjid });
// 1. 현재 메뉴 자신을 포함
const menuObjids = [menuObjid];
// 2. 현재 메뉴의 자식 메뉴들 조회
const childrenQuery = `
SELECT objid FROM menu_info
WHERE parent_obj_id = $1
ORDER BY objid
`;
const childrenResult = await pool.query(childrenQuery, [menuObjid]);
const childObjids = childrenResult.rows.map((row) => Number(row.objid));
// 3. 자신 + 자식을 합쳐서 정렬
const allObjids = Array.from(new Set([...menuObjids, ...childObjids])).sort((a, b) => a - b);
logger.debug("메뉴 스코프 조회 완료", {
menuObjid,
childCount: childObjids.length,
totalCount: allObjids.length
});
return allObjids;
} catch (error: any) {
logger.error("메뉴 스코프 조회 실패", {
menuObjid,
error: error.message,
stack: error.stack
});
// 에러 발생 시 안전하게 자기 자신만 반환
return [menuObjid];
}
}
/**
* OBJID
*
*
*
* @param menuObjids OBJID
* @returns OBJID ( , )
*
* @example
* // 서로 다른 부모를 가진 메뉴들의 형제를 모두 조회
* await getAllSiblingMenuObjids([201, 301]);
* // 201의 형제: [201, 202, 203]
* // 301의 형제: [301, 302]
* // 결과: [201, 202, 203, 301, 302]
*/
export async function getAllSiblingMenuObjids(
menuObjids: number[]
): Promise<number[]> {
if (!menuObjids || menuObjids.length === 0) {
logger.warn("getAllSiblingMenuObjids: 빈 배열 입력");
return [];
}
const allSiblings = new Set<number>();
for (const objid of menuObjids) {
const siblings = await getSiblingMenuObjids(objid);
siblings.forEach((s) => allSiblings.add(s));
}
const result = Array.from(allSiblings).sort((a, b) => a - b);
logger.info("여러 메뉴의 형제 조회 완료", {
inputMenus: menuObjids,
resultCount: result.length,
result,
});
return result;
}
/**
*
*
* @param menuObjid OBJID
* @returns ( null)
*/
export async function getMenuInfo(menuObjid: number): Promise<any | null> {
const pool = getPool();
try {
const query = `
SELECT
objid,
parent_obj_id AS "parentObjId",
menu_name_kor AS "menuNameKor",
menu_name_eng AS "menuNameEng",
menu_url AS "menuUrl",
company_code AS "companyCode"
FROM menu_info
WHERE objid = $1
`;
const result = await pool.query(query, [menuObjid]);
if (result.rows.length === 0) {
return null;
}
return result.rows[0];
} catch (error: any) {
logger.error("메뉴 정보 조회 실패", { menuObjid, error: error.message });
return null;
}
}