feat: Add BOM version activation feature and enhance BOM management

- Implemented the `activateBomVersion` function in the BOM controller to allow activation of specific BOM versions.
- Updated BOM routes to include the new activation endpoint for BOM versions.
- Enhanced the BOM service to handle version activation logic, including status updates and BOM header version changes.
- Improved the BOM version modal to support version activation with user confirmation and feedback.
- Added checks to prevent deletion of active BOM versions, ensuring data integrity.
This commit is contained in:
DDD1542
2026-02-25 16:18:46 +09:00
parent 18cf5e3269
commit 0f3ec495a5
7 changed files with 235 additions and 61 deletions

View File

@@ -93,6 +93,19 @@ export async function loadBomVersion(req: Request, res: Response) {
}
}
export async function activateBomVersion(req: Request, res: Response) {
try {
const { bomId, versionId } = req.params;
const { tableName } = req.body || {};
const result = await bomService.activateBomVersion(bomId, versionId, tableName);
res.json({ success: true, data: result });
} catch (error: any) {
logger.error("BOM 버전 사용 확정 실패", { error: error.message });
res.status(500).json({ success: false, message: error.message });
}
}
export async function deleteBomVersion(req: Request, res: Response) {
try {
const { bomId, versionId } = req.params;

View File

@@ -18,6 +18,7 @@ router.post("/:bomId/history", bomController.addBomHistory);
router.get("/:bomId/versions", bomController.getBomVersions);
router.post("/:bomId/versions", bomController.createBomVersion);
router.post("/:bomId/versions/:versionId/load", bomController.loadBomVersion);
router.post("/:bomId/versions/:versionId/activate", bomController.activateBomVersion);
router.delete("/:bomId/versions/:versionId", bomController.deleteBomVersion);
export default router;

View File

@@ -112,6 +112,9 @@ export async function createBomVersion(
companyCode,
]);
// BOM 헤더의 version 필드도 업데이트
await client.query(`UPDATE bom SET version = $1 WHERE id = $2`, [versionName, bomId]);
logger.info("BOM 버전 생성", { bomId, versionName, companyCode, vTable, dTable });
return result.rows[0];
});
@@ -140,6 +143,7 @@ export async function loadBomVersion(
await client.query(`DELETE FROM ${snapshotDetailTable} WHERE bom_id = $1`, [bomId]);
const b = snapshot.bom;
const loadedVersionName = verRow.rows[0].version_name;
await client.query(
`UPDATE bom SET base_qty = $1, unit = $2, revision = $3, remark = $4 WHERE id = $5`,
[b.base_qty || null, b.unit || null, b.revision || null, b.remark || null, bomId],
@@ -169,12 +173,50 @@ export async function loadBomVersion(
}
logger.info("BOM 버전 불러오기 완료", { bomId, versionId, vTable, snapshotDetailTable });
return { restored: true, versionName: verRow.rows[0].version_name };
return { restored: true, versionName: loadedVersionName };
});
}
export async function activateBomVersion(bomId: string, versionId: string, tableName?: string) {
const table = safeTableName(tableName || "", "bom_version");
return transaction(async (client) => {
const verRow = await client.query(
`SELECT version_name FROM ${table} WHERE id = $1 AND bom_id = $2`,
[versionId, bomId],
);
if (verRow.rows.length === 0) throw new Error("버전을 찾을 수 없습니다");
// 기존 active -> inactive
await client.query(
`UPDATE ${table} SET status = 'inactive' WHERE bom_id = $1 AND status = 'active'`,
[bomId],
);
// 선택한 버전 -> active
await client.query(
`UPDATE ${table} SET status = 'active' WHERE id = $1`,
[versionId],
);
// BOM 헤더 version도 갱신
const versionName = verRow.rows[0].version_name;
await client.query(
`UPDATE bom SET version = $1 WHERE id = $2`,
[versionName, bomId],
);
logger.info("BOM 버전 사용 확정", { bomId, versionId, versionName });
return { activated: true, versionName };
});
}
export async function deleteBomVersion(bomId: string, versionId: string, tableName?: string) {
const table = safeTableName(tableName || "", "bom_version");
// active 상태 버전은 삭제 불가
const checkSql = `SELECT status FROM ${table} WHERE id = $1 AND bom_id = $2`;
const checkResult = await query(checkSql, [versionId, bomId]);
if (checkResult.length > 0 && checkResult[0].status === "active") {
throw new Error("사용중인 버전은 삭제할 수 없습니다");
}
const sql = `DELETE FROM ${table} WHERE id = $1 AND bom_id = $2 RETURNING id`;
const result = await query(sql, [versionId, bomId]);
return result.length > 0;