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:
Hyungi Ahn
2026-03-13 20:27:14 +09:00
parent 7e10a90a1a
commit e236883c64
5 changed files with 47 additions and 73 deletions

View File

@@ -77,13 +77,25 @@ async function loadWorkers() {
return;
}
// 개별 입력 탭 - 작업자 셀렉트 박스
// 개별 입력 탭 - 작업자 셀렉트 박스 (부서별 그룹)
const selectWorker = document.getElementById('individualWorker');
const byDept = {};
workers.forEach(worker => {
const option = document.createElement('option');
option.value = worker.user_id;
option.textContent = `${worker.worker_name} (${worker.employment_status === 'employed' ? '재직' : '퇴사'})`;
selectWorker.appendChild(option);
const dept = worker.department_name || '부서 미지정';
if (!byDept[dept]) byDept[dept] = [];
byDept[dept].push(worker);
});
Object.keys(byDept).sort().forEach(dept => {
const group = document.createElement('optgroup');
group.label = dept;
byDept[dept].forEach(worker => {
const option = document.createElement('option');
option.value = worker.user_id;
option.textContent = `${worker.worker_name} (${worker.employment_status === 'employed' ? '재직' : '퇴사'})`;
group.appendChild(option);
});
selectWorker.appendChild(group);
});
console.log(`Loaded ${workers.length} workers successfully`);
} catch (error) {

View File

@@ -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);
}

View File

@@ -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 };

View File

@@ -465,12 +465,6 @@
<label class="block text-xs font-medium text-gray-600 mb-1">부서명 <span class="text-red-400">*</span></label>
<input type="text" id="newDeptName" class="input-field w-full px-3 py-1.5 rounded-lg text-sm" placeholder="부서 이름" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">상위부서</label>
<select id="newDeptParent" class="input-field w-full px-3 py-1.5 rounded-lg text-sm">
<option value="">없음 (최상위)</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">설명</label>
<input type="text" id="newDeptDescription" class="input-field w-full px-3 py-1.5 rounded-lg text-sm" placeholder="부서 설명">
@@ -1087,28 +1081,13 @@
<label class="block text-xs font-medium text-gray-600 mb-1">부서명</label>
<input type="text" id="editDeptName" class="input-field w-full px-3 py-1.5 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">상위부서</label>
<select id="editDeptParent" class="input-field w-full px-3 py-1.5 rounded-lg text-sm">
<option value="">없음 (최상위)</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">설명</label>
<input type="text" id="editDeptDescription" class="input-field w-full px-3 py-1.5 rounded-lg text-sm">
</div>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">표시순서</label>
<input type="number" id="editDeptOrder" class="input-field w-full px-3 py-1.5 rounded-lg text-sm" min="0">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">활성</label>
<select id="editDeptActive" class="input-field w-full px-3 py-1.5 rounded-lg text-sm">
<option value="1">활성</option>
<option value="0">비활성</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">표시순서</label>
<input type="number" id="editDeptOrder" class="input-field w-full px-3 py-1.5 rounded-lg text-sm" min="0">
</div>
<div class="flex gap-3 pt-3">
<button type="button" onclick="closeDepartmentModal()" class="flex-1 px-4 py-2 border border-gray-300 rounded-lg hover:bg-gray-50 text-sm">취소</button>

View File

@@ -5,25 +5,12 @@ async function loadDepartments() {
try {
const r = await api('/departments'); departments = r.data || r;
departmentsLoaded = true;
populateParentDeptSelects();
displayDepartments();
} catch (err) {
document.getElementById('departmentList').innerHTML = `<div class="text-red-500 text-center py-6"><i class="fas fa-exclamation-triangle text-xl"></i><p class="text-sm mt-2">${err.message}</p></div>`;
}
}
function populateParentDeptSelects() {
['newDeptParent','editDeptParent'].forEach(id => {
const sel = document.getElementById(id); if (!sel) return;
const val = sel.value;
sel.innerHTML = '<option value="">없음 (최상위)</option>';
departments.filter(d => d.is_active !== 0 && d.is_active !== false).forEach(d => {
const o = document.createElement('option'); o.value = d.department_id; o.textContent = d.department_name; sel.appendChild(o);
});
sel.value = val;
});
}
let selectedDeptForMembers = null;
function displayDepartments() {
@@ -34,14 +21,12 @@ function displayDepartments() {
<div class="flex-1 min-w-0">
<div class="text-sm font-medium text-gray-800 truncate"><i class="fas fa-sitemap mr-1.5 text-gray-400 text-xs"></i>${d.department_name}</div>
<div class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 flex-wrap">
${d.parent_name ? `<span class="px-1.5 py-0.5 rounded bg-slate-50 text-slate-500">상위: ${d.parent_name}</span>` : '<span class="px-1.5 py-0.5 rounded bg-indigo-50 text-indigo-500">최상위</span>'}
<span class="text-gray-400">순서: ${d.display_order || 0}</span>
${d.is_active === 0 || d.is_active === false ? '<span class="px-1.5 py-0.5 rounded bg-gray-100 text-gray-400">비활성</span>' : '<span class="px-1.5 py-0.5 rounded bg-emerald-50 text-emerald-600">활성</span>'}
</div>
</div>
<div class="flex gap-1 ml-2 flex-shrink-0" onclick="event.stopPropagation()">
<button onclick="editDepartment(${d.department_id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="편집"><i class="fas fa-pen-to-square text-xs"></i></button>
${d.is_active !== 0 && d.is_active !== false ? `<button onclick="deactivateDepartment(${d.department_id},'${(d.department_name||'').replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="비활성화"><i class="fas fa-ban text-xs"></i></button>` : ''}
<button onclick="deleteDepartment(${d.department_id},'${(d.department_name||'').replace(/'/g,"\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="삭제"><i class="fas fa-trash text-xs"></i></button>
</div>
</div>`).join('');
}
@@ -96,7 +81,6 @@ document.getElementById('addDepartmentForm').addEventListener('submit', async e
try {
await api('/departments', { method: 'POST', body: JSON.stringify({
department_name: document.getElementById('newDeptName').value.trim(),
parent_id: document.getElementById('newDeptParent').value ? parseInt(document.getElementById('newDeptParent').value) : null,
description: document.getElementById('newDeptDescription').value.trim() || null,
display_order: parseInt(document.getElementById('newDeptOrder').value) || 0
})});
@@ -110,9 +94,6 @@ function editDepartment(id) {
document.getElementById('editDeptName').value = d.department_name;
document.getElementById('editDeptDescription').value = d.description || '';
document.getElementById('editDeptOrder').value = d.display_order || 0;
document.getElementById('editDeptActive').value = (d.is_active === 0 || d.is_active === false) ? '0' : '1';
populateParentDeptSelects();
document.getElementById('editDeptParent').value = d.parent_id || '';
document.getElementById('editDepartmentModal').classList.remove('hidden');
}
function closeDepartmentModal() { document.getElementById('editDepartmentModal').classList.add('hidden'); }
@@ -122,17 +103,15 @@ document.getElementById('editDepartmentForm').addEventListener('submit', async e
try {
await api(`/departments/${document.getElementById('editDeptId').value}`, { method: 'PUT', body: JSON.stringify({
department_name: document.getElementById('editDeptName').value.trim(),
parent_id: document.getElementById('editDeptParent').value ? parseInt(document.getElementById('editDeptParent').value) : null,
description: document.getElementById('editDeptDescription').value.trim() || null,
display_order: parseInt(document.getElementById('editDeptOrder').value) || 0,
is_active: document.getElementById('editDeptActive').value === '1'
display_order: parseInt(document.getElementById('editDeptOrder').value) || 0
})});
showToast('수정되었습니다.'); closeDepartmentModal(); await loadDepartments();
await loadDepartmentsForSelect();
} catch(e) { showToast(e.message, 'error'); }
});
async function deactivateDepartment(id, name) {
if (!confirm(`"${name}" 부서를 비활성화?`)) return;
try { await api(`/departments/${id}`, { method: 'DELETE' }); showToast('부서 비활성화 완료'); await loadDepartments(); } catch(e) { showToast(e.message, 'error'); }
async function deleteDepartment(id, name) {
if (!confirm(`"${name}" 부서를 삭제하시겠습니까? 소속 인원은 부서 미지정으로 변경됩니다.`)) return;
try { await api(`/departments/${id}`, { method: 'DELETE' }); showToast('부서가 삭제되었습니다'); await loadDepartments(); } catch(e) { showToast(e.message, 'error'); }
}