공통코드 관리 시스템 개선 완료
This commit is contained in:
@@ -395,4 +395,105 @@ export class CommonCodeController {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 카테고리 중복 검사
|
||||
* GET /api/common-codes/categories/check-duplicate?field=categoryCode&value=USER_STATUS&excludeCode=OLD_CODE
|
||||
*/
|
||||
async checkCategoryDuplicate(req: AuthenticatedRequest, res: Response) {
|
||||
try {
|
||||
const { field, value, excludeCode } = req.query;
|
||||
|
||||
// 입력값 검증
|
||||
if (!field || !value) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "field와 value 파라미터가 필요합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const validFields = ['categoryCode', 'categoryName', 'categoryNameEng'];
|
||||
if (!validFields.includes(field as string)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "field는 categoryCode, categoryName, categoryNameEng 중 하나여야 합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.commonCodeService.checkCategoryDuplicate(
|
||||
field as 'categoryCode' | 'categoryName' | 'categoryNameEng',
|
||||
value as string,
|
||||
excludeCode as string
|
||||
);
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: {
|
||||
...result,
|
||||
field,
|
||||
value
|
||||
},
|
||||
message: "카테고리 중복 검사 완료",
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("카테고리 중복 검사 실패:", error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "카테고리 중복 검사 중 오류가 발생했습니다.",
|
||||
error: error instanceof Error ? error.message : "Unknown error",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 코드 중복 검사
|
||||
* GET /api/common-codes/categories/:categoryCode/codes/check-duplicate?field=codeValue&value=ACTIVE&excludeCode=OLD_CODE
|
||||
*/
|
||||
async checkCodeDuplicate(req: AuthenticatedRequest, res: Response) {
|
||||
try {
|
||||
const { categoryCode } = req.params;
|
||||
const { field, value, excludeCode } = req.query;
|
||||
|
||||
// 입력값 검증
|
||||
if (!field || !value) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "field와 value 파라미터가 필요합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const validFields = ['codeValue', 'codeName', 'codeNameEng'];
|
||||
if (!validFields.includes(field as string)) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: "field는 codeValue, codeName, codeNameEng 중 하나여야 합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.commonCodeService.checkCodeDuplicate(
|
||||
categoryCode,
|
||||
field as 'codeValue' | 'codeName' | 'codeNameEng',
|
||||
value as string,
|
||||
excludeCode as string
|
||||
);
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: {
|
||||
...result,
|
||||
categoryCode,
|
||||
field,
|
||||
value
|
||||
},
|
||||
message: "코드 중복 검사 완료",
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(`코드 중복 검사 실패 (${req.params.categoryCode}):`, error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "코드 중복 검사 중 오류가 발생했습니다.",
|
||||
error: error instanceof Error ? error.message : "Unknown error",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,12 @@ router.use(authenticateToken);
|
||||
router.get("/categories", (req, res) =>
|
||||
commonCodeController.getCategories(req, res)
|
||||
);
|
||||
|
||||
// 카테고리 중복 검사 (구체적인 경로를 먼저 배치)
|
||||
router.get("/categories/check-duplicate", (req, res) =>
|
||||
commonCodeController.checkCategoryDuplicate(req, res)
|
||||
);
|
||||
|
||||
router.post("/categories", (req, res) =>
|
||||
commonCodeController.createCategory(req, res)
|
||||
);
|
||||
@@ -30,6 +36,11 @@ router.post("/categories/:categoryCode/codes", (req, res) =>
|
||||
commonCodeController.createCode(req, res)
|
||||
);
|
||||
|
||||
// 코드 중복 검사 (구체적인 경로를 먼저 배치)
|
||||
router.get("/categories/:categoryCode/codes/check-duplicate", (req, res) =>
|
||||
commonCodeController.checkCodeDuplicate(req, res)
|
||||
);
|
||||
|
||||
// 코드 순서 변경 (구체적인 경로를 먼저 배치)
|
||||
router.put("/categories/:categoryCode/codes/reorder", (req, res) =>
|
||||
commonCodeController.reorderCodes(req, res)
|
||||
|
||||
@@ -417,4 +417,135 @@ export class CommonCodeService {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 카테고리 중복 검사
|
||||
*/
|
||||
async checkCategoryDuplicate(
|
||||
field: 'categoryCode' | 'categoryName' | 'categoryNameEng',
|
||||
value: string,
|
||||
excludeCategoryCode?: string
|
||||
): Promise<{ isDuplicate: boolean; message: string }> {
|
||||
try {
|
||||
if (!value || !value.trim()) {
|
||||
return {
|
||||
isDuplicate: false,
|
||||
message: "값을 입력해주세요."
|
||||
};
|
||||
}
|
||||
|
||||
const trimmedValue = value.trim();
|
||||
let whereCondition: any = {};
|
||||
|
||||
// 필드별 검색 조건 설정
|
||||
switch (field) {
|
||||
case 'categoryCode':
|
||||
whereCondition.category_code = trimmedValue;
|
||||
break;
|
||||
case 'categoryName':
|
||||
whereCondition.category_name = trimmedValue;
|
||||
break;
|
||||
case 'categoryNameEng':
|
||||
whereCondition.category_name_eng = trimmedValue;
|
||||
break;
|
||||
}
|
||||
|
||||
// 수정 시 자기 자신 제외
|
||||
if (excludeCategoryCode) {
|
||||
whereCondition.category_code = {
|
||||
...whereCondition.category_code,
|
||||
not: excludeCategoryCode
|
||||
};
|
||||
}
|
||||
|
||||
const existingCategory = await prisma.code_category.findFirst({
|
||||
where: whereCondition,
|
||||
select: { category_code: true }
|
||||
});
|
||||
|
||||
const isDuplicate = !!existingCategory;
|
||||
const fieldNames = {
|
||||
categoryCode: '카테고리 코드',
|
||||
categoryName: '카테고리명',
|
||||
categoryNameEng: '카테고리 영문명'
|
||||
};
|
||||
|
||||
return {
|
||||
isDuplicate,
|
||||
message: isDuplicate
|
||||
? `이미 사용 중인 ${fieldNames[field]}입니다.`
|
||||
: `사용 가능한 ${fieldNames[field]}입니다.`
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error(`카테고리 중복 검사 중 오류 (${field}: ${value}):`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 코드 중복 검사
|
||||
*/
|
||||
async checkCodeDuplicate(
|
||||
categoryCode: string,
|
||||
field: 'codeValue' | 'codeName' | 'codeNameEng',
|
||||
value: string,
|
||||
excludeCodeValue?: string
|
||||
): Promise<{ isDuplicate: boolean; message: string }> {
|
||||
try {
|
||||
if (!value || !value.trim()) {
|
||||
return {
|
||||
isDuplicate: false,
|
||||
message: "값을 입력해주세요."
|
||||
};
|
||||
}
|
||||
|
||||
const trimmedValue = value.trim();
|
||||
let whereCondition: any = {
|
||||
code_category: categoryCode
|
||||
};
|
||||
|
||||
// 필드별 검색 조건 설정
|
||||
switch (field) {
|
||||
case 'codeValue':
|
||||
whereCondition.code_value = trimmedValue;
|
||||
break;
|
||||
case 'codeName':
|
||||
whereCondition.code_name = trimmedValue;
|
||||
break;
|
||||
case 'codeNameEng':
|
||||
whereCondition.code_name_eng = trimmedValue;
|
||||
break;
|
||||
}
|
||||
|
||||
// 수정 시 자기 자신 제외
|
||||
if (excludeCodeValue) {
|
||||
whereCondition.code_value = {
|
||||
...whereCondition.code_value,
|
||||
not: excludeCodeValue
|
||||
};
|
||||
}
|
||||
|
||||
const existingCode = await prisma.code_info.findFirst({
|
||||
where: whereCondition,
|
||||
select: { code_value: true }
|
||||
});
|
||||
|
||||
const isDuplicate = !!existingCode;
|
||||
const fieldNames = {
|
||||
codeValue: '코드값',
|
||||
codeName: '코드명',
|
||||
codeNameEng: '코드 영문명'
|
||||
};
|
||||
|
||||
return {
|
||||
isDuplicate,
|
||||
message: isDuplicate
|
||||
? `이미 사용 중인 ${fieldNames[field]}입니다.`
|
||||
: `사용 가능한 ${fieldNames[field]}입니다.`
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error(`코드 중복 검사 중 오류 (${categoryCode}, ${field}: ${value}):`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user