Files
tk-factory-services/user-management/api/models/departmentModel.js
Hyungi Ahn e236883c64 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>
2026-03-13 20:27:14 +09:00

76 lines
1.9 KiB
JavaScript

/**
* Department Model
*
* departments 테이블 CRUD (MariaDB)
*/
const { getPool } = require('./userModel');
async function getAll() {
const db = getPool();
const [rows] = await db.query(
`SELECT d.*
FROM departments d
ORDER BY d.display_order ASC, d.department_id ASC`
);
return rows;
}
async function getById(id) {
const db = getPool();
const [rows] = await db.query(
`SELECT d.*
FROM departments d
WHERE d.department_id = ?`,
[id]
);
return rows[0] || null;
}
async function create({ department_name, description, display_order }) {
const db = getPool();
const [result] = await db.query(
`INSERT INTO departments (department_name, description, display_order)
VALUES (?, ?, ?)`,
[department_name, description || null, display_order || 0]
);
return getById(result.insertId);
}
async function update(id, data) {
const db = getPool();
const fields = [];
const values = [];
if (data.department_name !== undefined) { fields.push('department_name = ?'); values.push(data.department_name); }
if (data.description !== undefined) { fields.push('description = ?'); values.push(data.description || null); }
if (data.display_order !== undefined) { fields.push('display_order = ?'); values.push(data.display_order); }
if (fields.length === 0) return getById(id);
values.push(id);
await db.query(
`UPDATE departments SET ${fields.join(', ')} WHERE department_id = ?`,
values
);
return getById(id);
}
async function remove(id) {
const db = getPool();
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, remove };