제어관리 외부커넥션 설정기능
This commit is contained in:
175
frontend/lib/api/codeManagement.ts
Normal file
175
frontend/lib/api/codeManagement.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* 코드 관리 API
|
||||
*/
|
||||
|
||||
import { apiClient } from "./client";
|
||||
|
||||
export interface CodeItem {
|
||||
code: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
orderNo?: number;
|
||||
useYn: string;
|
||||
}
|
||||
|
||||
export interface CodeCategory {
|
||||
categoryCode: string;
|
||||
categoryName: string;
|
||||
description?: string;
|
||||
useYn: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 코드 카테고리 목록 조회
|
||||
*/
|
||||
export const getCodeCategories = async (): Promise<CodeCategory[]> => {
|
||||
try {
|
||||
// 올바른 API 엔드포인트 사용 (apiClient 사용)
|
||||
const response = await apiClient.get("/common-codes/categories");
|
||||
const data = response.data;
|
||||
|
||||
// 응답 데이터 구조에 맞게 변환
|
||||
const categories = data.data || [];
|
||||
return categories.map((category: any) => ({
|
||||
categoryCode: category.categoryCode,
|
||||
categoryName: category.categoryName,
|
||||
description: category.description,
|
||||
useYn: category.isActive ? "Y" : "N",
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error("코드 카테고리 조회 실패:", error);
|
||||
|
||||
// API 호출 실패 시 빈 배열 반환
|
||||
console.warn("코드 카테고리 API 호출 실패 - 빈 배열 반환");
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 특정 카테고리의 코드 목록 조회
|
||||
*/
|
||||
export const getCodesByCategory = async (categoryCode: string): Promise<CodeItem[]> => {
|
||||
try {
|
||||
// API URL 디버깅
|
||||
const apiUrl = `/common-codes/categories/${encodeURIComponent(categoryCode)}/codes`;
|
||||
console.log("🔗 코드 API 호출 URL:", {
|
||||
categoryCode,
|
||||
apiUrl,
|
||||
currentURL: typeof window !== "undefined" ? window.location.href : "서버사이드",
|
||||
baseURL: typeof window !== "undefined" ? `${window.location.protocol}//${window.location.host}` : "서버사이드",
|
||||
apiClientBaseURL: apiClient.defaults.baseURL,
|
||||
});
|
||||
|
||||
console.log("📡 실제 요청 URL:", `${apiClient.defaults.baseURL}${apiUrl}`);
|
||||
|
||||
// 올바른 API 엔드포인트 사용 (apiClient 사용)
|
||||
const response = await apiClient.get(apiUrl);
|
||||
const data = response.data;
|
||||
|
||||
console.log("🔍 백엔드 응답 데이터:", {
|
||||
fullResponse: data,
|
||||
dataArray: data.data,
|
||||
firstItem: data.data && data.data[0] ? data.data[0] : "없음",
|
||||
dataType: typeof data.data,
|
||||
isArray: Array.isArray(data.data),
|
||||
});
|
||||
|
||||
// 응답 데이터 구조에 맞게 변환
|
||||
const codes = data.data || [];
|
||||
const mappedCodes = codes.map((code: any) => {
|
||||
console.log("🔄 코드 매핑:", {
|
||||
original: code,
|
||||
mapped: {
|
||||
code: code.codeValue || code.code || code.id || code.value,
|
||||
name: code.codeName || code.name || code.label || code.description,
|
||||
description: code.description || code.codeName || code.name,
|
||||
orderNo: code.sortOrder || code.orderNo || code.order,
|
||||
useYn: code.isActive ? "Y" : code.useYn || "Y",
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
code: code.codeValue || code.code || code.id || code.value,
|
||||
name: code.codeName || code.name || code.label || code.description,
|
||||
description: code.description || code.codeName || code.name,
|
||||
orderNo: code.sortOrder || code.orderNo || code.order,
|
||||
useYn: code.isActive ? "Y" : code.useYn || "Y",
|
||||
};
|
||||
});
|
||||
|
||||
console.log("📋 최종 매핑된 코드들:", mappedCodes);
|
||||
return mappedCodes;
|
||||
} catch (error: any) {
|
||||
console.error("코드 목록 조회 실패:", error);
|
||||
|
||||
// 인증 오류인 경우 명시적으로 알림
|
||||
if (error.response?.status === 401) {
|
||||
console.warn("🔐 인증이 필요합니다. 로그인 후 다시 시도하세요.");
|
||||
} else if (error.response?.status === 404) {
|
||||
console.warn(`📭 코드 카테고리 '${categoryCode}'가 존재하지 않습니다.`);
|
||||
} else {
|
||||
console.warn(`❌ 코드 카테고리 '${categoryCode}'에 대한 API 호출 실패:`, error.message);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 컬럼의 웹타입과 코드 카테고리 정보 기반으로 코드 조회
|
||||
*/
|
||||
export const getCodesForColumn = async (
|
||||
columnName: string,
|
||||
webType?: string,
|
||||
codeCategory?: string,
|
||||
): Promise<CodeItem[]> => {
|
||||
// 코드 타입이 아니면 빈 배열 반환
|
||||
if (webType !== "code" && !codeCategory) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 코드 카테고리가 있으면 해당 카테고리의 코드 조회
|
||||
if (codeCategory) {
|
||||
return await getCodesByCategory(codeCategory);
|
||||
}
|
||||
|
||||
// 컬럼명에서 코드 카테고리 추론 (예: status_code -> STATUS)
|
||||
const inferredCategory = inferCodeCategoryFromColumnName(columnName);
|
||||
if (inferredCategory) {
|
||||
return await getCodesByCategory(inferredCategory);
|
||||
}
|
||||
|
||||
return [];
|
||||
};
|
||||
|
||||
/**
|
||||
* 컬럼명에서 코드 카테고리 추론
|
||||
*/
|
||||
const inferCodeCategoryFromColumnName = (columnName: string): string | null => {
|
||||
const lowerName = columnName.toLowerCase();
|
||||
|
||||
// 일반적인 패턴들
|
||||
const patterns = [
|
||||
{ pattern: /status/i, category: "STATUS" },
|
||||
{ pattern: /state/i, category: "STATE" },
|
||||
{ pattern: /type/i, category: "TYPE" },
|
||||
{ pattern: /category/i, category: "CATEGORY" },
|
||||
{ pattern: /grade/i, category: "GRADE" },
|
||||
{ pattern: /level/i, category: "LEVEL" },
|
||||
{ pattern: /priority/i, category: "PRIORITY" },
|
||||
{ pattern: /role/i, category: "ROLE" },
|
||||
];
|
||||
|
||||
for (const { pattern, category } of patterns) {
|
||||
if (pattern.test(lowerName)) {
|
||||
return category;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* 실제 데이터베이스에 있는 코드 카테고리만 사용
|
||||
* Mock 데이터는 더 이상 사용하지 않음
|
||||
*/
|
||||
Reference in New Issue
Block a user