feat(tkuser): 부서 관리 개선 — 상위부서 제거, hard delete, 휴가 부서별 그룹
- 상위부서(parent_id) 필드 UI/API 전체 제거 - 부서 비활성화(soft delete) → 진짜 삭제(hard delete) 전환 (트랜잭션) - 소속 인원 있는 부서 삭제 시 department_id=NULL 처리 - 편집 모달에서 활성/비활성 필드 제거 - 휴가 발생 입력 작업자 select를 부서별 optgroup으로 표시 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -56,8 +56,8 @@ async function update(req, res, next) {
|
||||
async function remove(req, res, next) {
|
||||
try {
|
||||
const id = parseInt(req.params.id);
|
||||
await departmentModel.deactivate(id);
|
||||
res.json({ success: true, message: '부서가 비활성화되었습니다' });
|
||||
await departmentModel.remove(id);
|
||||
res.json({ success: true, message: '부서가 삭제되었습니다' });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
|
||||
@@ -9,9 +9,8 @@ const { getPool } = require('./userModel');
|
||||
async function getAll() {
|
||||
const db = getPool();
|
||||
const [rows] = await db.query(
|
||||
`SELECT d.*, p.department_name AS parent_name
|
||||
`SELECT d.*
|
||||
FROM departments d
|
||||
LEFT JOIN departments p ON d.parent_id = p.department_id
|
||||
ORDER BY d.display_order ASC, d.department_id ASC`
|
||||
);
|
||||
return rows;
|
||||
@@ -20,21 +19,20 @@ async function getAll() {
|
||||
async function getById(id) {
|
||||
const db = getPool();
|
||||
const [rows] = await db.query(
|
||||
`SELECT d.*, p.department_name AS parent_name
|
||||
`SELECT d.*
|
||||
FROM departments d
|
||||
LEFT JOIN departments p ON d.parent_id = p.department_id
|
||||
WHERE d.department_id = ?`,
|
||||
[id]
|
||||
);
|
||||
return rows[0] || null;
|
||||
}
|
||||
|
||||
async function create({ department_name, parent_id, description, display_order }) {
|
||||
async function create({ department_name, description, display_order }) {
|
||||
const db = getPool();
|
||||
const [result] = await db.query(
|
||||
`INSERT INTO departments (department_name, parent_id, description, display_order)
|
||||
VALUES (?, ?, ?, ?)`,
|
||||
[department_name, parent_id || null, description || null, display_order || 0]
|
||||
`INSERT INTO departments (department_name, description, display_order)
|
||||
VALUES (?, ?, ?)`,
|
||||
[department_name, description || null, display_order || 0]
|
||||
);
|
||||
return getById(result.insertId);
|
||||
}
|
||||
@@ -45,9 +43,7 @@ async function update(id, data) {
|
||||
const values = [];
|
||||
|
||||
if (data.department_name !== undefined) { fields.push('department_name = ?'); values.push(data.department_name); }
|
||||
if (data.parent_id !== undefined) { fields.push('parent_id = ?'); values.push(data.parent_id || null); }
|
||||
if (data.description !== undefined) { fields.push('description = ?'); values.push(data.description || null); }
|
||||
if (data.is_active !== undefined) { fields.push('is_active = ?'); values.push(data.is_active); }
|
||||
if (data.display_order !== undefined) { fields.push('display_order = ?'); values.push(data.display_order); }
|
||||
|
||||
if (fields.length === 0) return getById(id);
|
||||
@@ -60,12 +56,20 @@ async function update(id, data) {
|
||||
return getById(id);
|
||||
}
|
||||
|
||||
async function deactivate(id) {
|
||||
async function remove(id) {
|
||||
const db = getPool();
|
||||
await db.query(
|
||||
'UPDATE departments SET is_active = FALSE WHERE department_id = ?',
|
||||
[id]
|
||||
);
|
||||
const conn = await db.getConnection();
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
await conn.query('UPDATE users SET department_id = NULL WHERE department_id = ?', [id]);
|
||||
await conn.query('DELETE FROM departments WHERE department_id = ?', [id]);
|
||||
await conn.commit();
|
||||
} catch (e) {
|
||||
await conn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
conn.release();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { getAll, getById, create, update, deactivate };
|
||||
module.exports = { getAll, getById, create, update, remove };
|
||||
|
||||
Reference in New Issue
Block a user