feat: Enhance mold serial summary retrieval and improve category handling
- Updated the `getMoldSerialSummary` function to dynamically retrieve category values for mold statuses and operations, allowing for more flexible data aggregation. - Implemented a mapping mechanism to categorize status codes based on their labels, improving the clarity of the summary results. - Adjusted SQL queries to utilize the new category mappings for more accurate counts of mold statuses. - Refactored the packaging and loading unit deletion logic to handle company code checks more efficiently, ensuring proper data access control.
This commit is contained in:
@@ -512,13 +512,36 @@ export async function getMoldSerialSummary(req: AuthenticatedRequest, res: Respo
|
||||
const companyCode = req.user!.companyCode;
|
||||
const { moldCode } = req.params;
|
||||
|
||||
// 카테고리 코드/영문코드/한글라벨 모두 대응
|
||||
// 먼저 카테고리 값 조회하여 매핑
|
||||
// mold_serial.status + mold_mng.operation_status 양쪽 카테고리 모두 조회
|
||||
const catSql = `SELECT value_code, value_label FROM category_values
|
||||
WHERE ((table_name='mold_serial' AND column_name='status') OR (table_name='mold_mng' AND column_name='operation_status'))
|
||||
AND company_code=$1`;
|
||||
const catRows = await query(catSql, [companyCode]);
|
||||
|
||||
// 카테고리 라벨 기준으로 그룹핑할 코드 목록 생성
|
||||
const codesByLabel: Record<string, string[]> = { "사용중": ["IN_USE"], "수리중": ["REPAIR"], "보관중": ["STORED"], "폐기": ["DISPOSED"] };
|
||||
for (const cat of catRows) {
|
||||
const label = cat.value_label || "";
|
||||
if (label.includes("사용")) (codesByLabel["사용중"] = codesByLabel["사용중"] || []).push(cat.value_code);
|
||||
else if (label.includes("수리")) (codesByLabel["수리중"] = codesByLabel["수리중"] || []).push(cat.value_code);
|
||||
else if (label.includes("보관") || label.includes("미사용")) (codesByLabel["보관중"] = codesByLabel["보관중"] || []).push(cat.value_code);
|
||||
else if (label.includes("폐기")) (codesByLabel["폐기"] = codesByLabel["폐기"] || []).push(cat.value_code);
|
||||
}
|
||||
|
||||
const inUseCodes = codesByLabel["사용중"].map(c => `'${c}'`).join(",");
|
||||
const repairCodes = codesByLabel["수리중"].map(c => `'${c}'`).join(",");
|
||||
const storedCodes = codesByLabel["보관중"].map(c => `'${c}'`).join(",");
|
||||
const disposedCodes = codesByLabel["폐기"].map(c => `'${c}'`).join(",");
|
||||
|
||||
const sql = `
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
COUNT(*) FILTER (WHERE status = 'IN_USE') as in_use,
|
||||
COUNT(*) FILTER (WHERE status = 'REPAIR') as repair,
|
||||
COUNT(*) FILTER (WHERE status = 'STORED') as stored,
|
||||
COUNT(*) FILTER (WHERE status = 'DISPOSED') as disposed
|
||||
COUNT(*) FILTER (WHERE status IN (${inUseCodes})) as in_use,
|
||||
COUNT(*) FILTER (WHERE status IN (${repairCodes})) as repair,
|
||||
COUNT(*) FILTER (WHERE status IN (${storedCodes})) as stored,
|
||||
COUNT(*) FILTER (WHERE status IN (${disposedCodes})) as disposed
|
||||
FROM mold_serial
|
||||
WHERE mold_code = $1 AND company_code = $2
|
||||
`;
|
||||
|
||||
@@ -228,10 +228,11 @@ export async function deletePkgUnitItem(
|
||||
const { id } = req.params;
|
||||
const pool = getPool();
|
||||
|
||||
const result = await pool.query(
|
||||
`DELETE FROM pkg_unit_item WHERE id=$1 AND company_code=$2 RETURNING id`,
|
||||
[id, companyCode]
|
||||
);
|
||||
const query = companyCode === "*"
|
||||
? `DELETE FROM pkg_unit_item WHERE id=$1 RETURNING id`
|
||||
: `DELETE FROM pkg_unit_item WHERE id=$1 AND company_code=$2 RETURNING id`;
|
||||
const params = companyCode === "*" ? [id] : [id, companyCode];
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
if (result.rowCount === 0) {
|
||||
res.status(404).json({ success: false, message: "데이터를 찾을 수 없습니다." });
|
||||
@@ -471,10 +472,11 @@ export async function deleteLoadingUnitPkg(
|
||||
const { id } = req.params;
|
||||
const pool = getPool();
|
||||
|
||||
const result = await pool.query(
|
||||
`DELETE FROM loading_unit_pkg WHERE id=$1 AND company_code=$2 RETURNING id`,
|
||||
[id, companyCode]
|
||||
);
|
||||
const query = companyCode === "*"
|
||||
? `DELETE FROM loading_unit_pkg WHERE id=$1 RETURNING id`
|
||||
: `DELETE FROM loading_unit_pkg WHERE id=$1 AND company_code=$2 RETURNING id`;
|
||||
const params = companyCode === "*" ? [id] : [id, companyCode];
|
||||
const result = await pool.query(query, params);
|
||||
|
||||
if (result.rowCount === 0) {
|
||||
res.status(404).json({ success: false, message: "데이터를 찾을 수 없습니다." });
|
||||
|
||||
Reference in New Issue
Block a user