feat(tkuser): Sprint 001 Section A — 연차/휴가 백엔드 전환 (DB + API)
workers/vacation_balance_details → sso_users/sp_vacation_balances 기반 전환. 부서장 설정, 승인권한 CRUD, vacation_settings 테이블, 장기근속 자동부여, startup migration 추가. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Department Model
|
||||
*
|
||||
* departments 테이블 CRUD (MariaDB)
|
||||
* departments 테이블 CRUD + 승인권한 관리 (MariaDB)
|
||||
*/
|
||||
|
||||
const { getPool } = require('./userModel');
|
||||
@@ -9,8 +9,9 @@ const { getPool } = require('./userModel');
|
||||
async function getAll() {
|
||||
const db = getPool();
|
||||
const [rows] = await db.query(
|
||||
`SELECT d.*
|
||||
`SELECT d.*, su.name AS leader_name
|
||||
FROM departments d
|
||||
LEFT JOIN sso_users su ON d.leader_user_id = su.user_id
|
||||
ORDER BY d.display_order ASC, d.department_id ASC`
|
||||
);
|
||||
return rows;
|
||||
@@ -19,8 +20,9 @@ async function getAll() {
|
||||
async function getById(id) {
|
||||
const db = getPool();
|
||||
const [rows] = await db.query(
|
||||
`SELECT d.*
|
||||
`SELECT d.*, su.name AS leader_name
|
||||
FROM departments d
|
||||
LEFT JOIN sso_users su ON d.leader_user_id = su.user_id
|
||||
WHERE d.department_id = ?`,
|
||||
[id]
|
||||
);
|
||||
@@ -45,6 +47,7 @@ async function update(id, data) {
|
||||
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 (data.leader_user_id !== undefined) { fields.push('leader_user_id = ?'); values.push(data.leader_user_id || null); }
|
||||
|
||||
if (fields.length === 0) return getById(id);
|
||||
|
||||
@@ -61,6 +64,8 @@ async function remove(id) {
|
||||
const conn = await db.getConnection();
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
// CASCADE가 있지만 방어적으로 명시 삭제
|
||||
await conn.query('DELETE FROM dept_approval_authority WHERE department_id = ?', [id]);
|
||||
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();
|
||||
@@ -72,4 +77,57 @@ async function remove(id) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { getAll, getById, create, update, remove };
|
||||
/* ===== 승인권한 (Approval Authority) ===== */
|
||||
|
||||
async function getApprovalAuthorities(departmentId) {
|
||||
const db = getPool();
|
||||
const [rows] = await db.query(
|
||||
`SELECT aa.*, su.name AS approver_name
|
||||
FROM dept_approval_authority aa
|
||||
JOIN sso_users su ON aa.approver_user_id = su.user_id
|
||||
WHERE aa.department_id = ? AND aa.is_active = TRUE
|
||||
ORDER BY aa.approval_type ASC, aa.priority ASC`,
|
||||
[departmentId]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
async function createApprovalAuthority({ department_id, approval_type, approver_user_id, priority }) {
|
||||
const db = getPool();
|
||||
const [result] = await db.query(
|
||||
`INSERT INTO dept_approval_authority (department_id, approval_type, approver_user_id, priority)
|
||||
VALUES (?, ?, ?, ?)`,
|
||||
[department_id, approval_type, approver_user_id, priority || 1]
|
||||
);
|
||||
const [rows] = await db.query(
|
||||
`SELECT aa.*, su.name AS approver_name
|
||||
FROM dept_approval_authority aa
|
||||
JOIN sso_users su ON aa.approver_user_id = su.user_id
|
||||
WHERE aa.id = ?`,
|
||||
[result.insertId]
|
||||
);
|
||||
return rows[0] || null;
|
||||
}
|
||||
|
||||
async function deleteApprovalAuthority(id) {
|
||||
const db = getPool();
|
||||
await db.query('DELETE FROM dept_approval_authority WHERE id = ?', [id]);
|
||||
}
|
||||
|
||||
async function getApproversByType(departmentId, approvalType) {
|
||||
const db = getPool();
|
||||
const [rows] = await db.query(
|
||||
`SELECT aa.*, su.name AS approver_name
|
||||
FROM dept_approval_authority aa
|
||||
JOIN sso_users su ON aa.approver_user_id = su.user_id
|
||||
WHERE aa.department_id = ? AND aa.approval_type = ? AND aa.is_active = TRUE
|
||||
ORDER BY aa.priority ASC`,
|
||||
[departmentId, approvalType]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAll, getById, create, update, remove,
|
||||
getApprovalAuthorities, createApprovalAuthority, deleteApprovalAuthority, getApproversByType
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user