feat(tksupport): Sprint 001 Section C — 전사 휴가관리 구현

- 전사 휴가 부여/관리 (company-holidays) CRUD + 연차차감 트랜잭션
- 전체 휴가관리 대시보드 (vacation-dashboard) 부서별/직원별 현황
- 내 휴가 현황 개선 (/my-status) balance_type별 카드, 전사 휴가일
- requireSupportTeam 미들웨어, 부서명 JOIN, 마이그레이션 002 추가
- 사이드바 roles 기반 메뉴 필터링 (하위호환 유지)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-03-23 08:16:50 +09:00
parent a3f7a324b1
commit 36391c02e1
19 changed files with 1040 additions and 62 deletions

View File

@@ -0,0 +1,93 @@
const { getPool } = require('../middleware/auth');
const companyHolidayModel = {
async getByYear(year) {
const db = getPool();
const [rows] = await db.query(
'SELECT * FROM company_holidays WHERE YEAR(holiday_date) = ? ORDER BY holiday_date',
[year]
);
return rows;
},
async getById(id) {
const db = getPool();
const [rows] = await db.query('SELECT * FROM company_holidays WHERE id = ?', [id]);
return rows.length > 0 ? rows[0] : null;
},
async create(data) {
const db = getPool();
const [result] = await db.query(
'INSERT INTO company_holidays (holiday_date, holiday_name, holiday_type, description, created_by) VALUES (?, ?, ?, ?, ?)',
[data.holiday_date, data.holiday_name, data.holiday_type, data.description || null, data.created_by]
);
return result;
},
async delete(id) {
const db = getPool();
const [result] = await db.query('DELETE FROM company_holidays WHERE id = ?', [id]);
return result;
},
async applyAnnualDeduction(holidayId) {
const db = getPool();
const conn = await db.getConnection();
try {
await conn.beginTransaction();
// 1. SELECT FOR UPDATE — 동시 실행 방지
const [holidays] = await conn.query(
'SELECT * FROM company_holidays WHERE id = ? FOR UPDATE', [holidayId]
);
if (holidays.length === 0) {
throw new Error('해당 전사 휴가를 찾을 수 없습니다');
}
const holiday = holidays[0];
if (holiday.holiday_type !== 'ANNUAL_DEDUCT') {
throw new Error('연차차감 유형의 휴가만 차감할 수 있습니다');
}
if (holiday.deduction_applied_at) {
throw new Error('이미 차감이 실행된 휴가입니다');
}
// 2. vacation_types에서 ANNUAL_FULL id 조회
const [types] = await conn.query(
"SELECT id FROM vacation_types WHERE type_code = 'ANNUAL_FULL'"
);
if (types.length === 0) {
throw new Error('연차(ANNUAL_FULL) 유형이 존재하지 않습니다');
}
const typeId = types[0].id;
// 3. 전 활성 사원 연차 차감 (잔여일 무관 일괄 적용)
const [result] = await conn.query(`
UPDATE sp_vacation_balances SET used_days = used_days + 1, updated_at = NOW()
WHERE vacation_type_id = ? AND year = YEAR(?) AND balance_type = 'AUTO'
AND user_id IN (SELECT user_id FROM sso_users WHERE is_active = 1)
`, [typeId, holiday.holiday_date]);
// 4. 차감 완료 표시
await conn.query(
'UPDATE company_holidays SET deduction_applied_at = NOW() WHERE id = ?', [holidayId]
);
// 5. balance 없는 사원 수 체크 (경고용)
const [activeUsers] = await conn.query(
'SELECT COUNT(*) as cnt FROM sso_users WHERE is_active = 1'
);
const missing = activeUsers[0].cnt - result.affectedRows;
await conn.commit();
return { affected_count: result.affectedRows, missing_balance_count: missing };
} catch (err) {
await conn.rollback();
throw err;
} finally {
conn.release();
}
}
};
module.exports = companyHolidayModel;

View File

@@ -6,6 +6,8 @@ const vacationBalanceModel = {
const [rows] = await db.query(`
SELECT
vb.*,
vb.balance_type,
vb.expires_at,
vt.type_name,
vt.type_code,
vt.priority,
@@ -13,7 +15,7 @@ const vacationBalanceModel = {
(vb.total_days - vb.used_days) as remaining_days
FROM sp_vacation_balances vb
INNER JOIN vacation_types vt ON vb.vacation_type_id = vt.id
WHERE vb.user_id = ? AND vb.year = ?
WHERE vb.user_id = ? AND (vb.year = ? OR (vb.balance_type = 'LONG_SERVICE' AND vb.expires_at IS NULL))
ORDER BY vt.priority ASC, vt.type_name ASC
`, [userId, year]);
return rows;
@@ -24,9 +26,12 @@ const vacationBalanceModel = {
const [rows] = await db.query(`
SELECT
vb.*,
vb.balance_type,
su.name as user_name,
su.username,
su.hire_date,
su.department_id,
COALESCE(d.department_name, '미배정') as department_name,
vt.type_name,
vt.type_code,
vt.priority,
@@ -34,7 +39,8 @@ const vacationBalanceModel = {
FROM sp_vacation_balances vb
INNER JOIN sso_users su ON vb.user_id = su.user_id
INNER JOIN vacation_types vt ON vb.vacation_type_id = vt.id
WHERE vb.year = ? AND su.is_active = 1
LEFT JOIN departments d ON su.department_id = d.department_id
WHERE (vb.year = ? OR (vb.balance_type = 'LONG_SERVICE' AND vb.expires_at IS NULL)) AND su.is_active = 1
ORDER BY su.name ASC, vt.priority ASC
`, [year]);
return rows;
@@ -42,14 +48,16 @@ const vacationBalanceModel = {
async allocate(data) {
const db = getPool();
const balanceType = data.balance_type || 'AUTO';
const expiresAt = data.expires_at || null;
const [result] = await db.query(`
INSERT INTO sp_vacation_balances (user_id, vacation_type_id, year, total_days, used_days, notes, created_by)
VALUES (?, ?, ?, ?, 0, ?, ?)
INSERT INTO sp_vacation_balances (user_id, vacation_type_id, year, total_days, used_days, notes, created_by, balance_type, expires_at)
VALUES (?, ?, ?, ?, 0, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
total_days = VALUES(total_days),
notes = VALUES(notes),
updated_at = NOW()
`, [data.user_id, data.vacation_type_id, data.year, data.total_days, data.notes || null, data.created_by]);
`, [data.user_id, data.vacation_type_id, data.year, data.total_days, data.notes || null, data.created_by, balanceType, expiresAt]);
return result;
},

View File

@@ -0,0 +1,55 @@
const { getPool } = require('../middleware/auth');
const vacationDashboardModel = {
async getSummary(year) {
const db = getPool();
const [rows] = await db.query(`
SELECT
d.department_id, COALESCE(d.department_name, '미배정') as department_name,
COUNT(DISTINCT su.user_id) as employee_count,
AVG(vb.total_days - vb.used_days) as avg_remaining,
SUM(CASE WHEN (vb.total_days - vb.used_days) <= 2 THEN 1 ELSE 0 END) as low_balance_count
FROM sso_users su
LEFT JOIN departments d ON su.department_id = d.department_id
LEFT JOIN sp_vacation_balances vb ON su.user_id = vb.user_id
AND vb.year = ? AND vb.balance_type = 'AUTO'
WHERE su.is_active = 1
GROUP BY d.department_id
`, [year]);
return rows;
},
async getEmployeeList(year, filters = {}) {
const db = getPool();
let query = `
SELECT su.user_id, su.name, su.username, su.hire_date, su.department_id,
COALESCE(d.department_name, '미배정') as department_name,
vb.id, vb.balance_type, vb.total_days, vb.used_days,
(vb.total_days - vb.used_days) as remaining_days,
vb.expires_at, vt.type_name, vt.type_code
FROM sso_users su
LEFT JOIN departments d ON su.department_id = d.department_id
LEFT JOIN sp_vacation_balances vb ON su.user_id = vb.user_id
AND (vb.year = ? OR (vb.balance_type = 'LONG_SERVICE' AND vb.expires_at IS NULL))
LEFT JOIN vacation_types vt ON vb.vacation_type_id = vt.id
WHERE su.is_active = 1
`;
const params = [year];
if (filters.department_id) {
query += ' AND su.department_id = ?';
params.push(filters.department_id);
}
if (filters.search_name) {
query += ' AND su.name LIKE ?';
params.push(`%${filters.search_name}%`);
}
query += ' ORDER BY su.name ASC';
const [rows] = await db.query(query, params);
return rows;
}
};
module.exports = vacationDashboardModel;

View File

@@ -14,11 +14,13 @@ const vacationRequestModel = {
vr.*,
su.name as user_name,
su.username,
COALESCE(d.department_name, '미배정') as department_name,
vt.type_name as vacation_type_name,
vt.type_code,
reviewer.name as reviewer_name
FROM sp_vacation_requests vr
INNER JOIN sso_users su ON vr.user_id = su.user_id
LEFT JOIN departments d ON su.department_id = d.department_id
INNER JOIN vacation_types vt ON vr.vacation_type_id = vt.id
LEFT JOIN sso_users reviewer ON vr.reviewed_by = reviewer.user_id
WHERE 1=1
@@ -45,6 +47,10 @@ const vacationRequestModel = {
query += ' AND vr.vacation_type_id = ?';
params.push(filters.vacation_type_id);
}
if (filters.department_id) {
query += ' AND su.department_id = ?';
params.push(filters.department_id);
}
query += ' ORDER BY vr.created_at DESC';
@@ -59,11 +65,13 @@ const vacationRequestModel = {
vr.*,
su.name as user_name,
su.username,
COALESCE(d.department_name, '미배정') as department_name,
vt.type_name as vacation_type_name,
vt.type_code,
reviewer.name as reviewer_name
FROM sp_vacation_requests vr
INNER JOIN sso_users su ON vr.user_id = su.user_id
LEFT JOIN departments d ON su.department_id = d.department_id
INNER JOIN vacation_types vt ON vr.vacation_type_id = vt.id
LEFT JOIN sso_users reviewer ON vr.reviewed_by = reviewer.user_id
WHERE vr.request_id = ?
@@ -113,10 +121,12 @@ const vacationRequestModel = {
vr.*,
su.name as user_name,
su.username,
COALESCE(d.department_name, '미배정') as department_name,
vt.type_name as vacation_type_name,
vt.type_code
FROM sp_vacation_requests vr
INNER JOIN sso_users su ON vr.user_id = su.user_id
LEFT JOIN departments d ON su.department_id = d.department_id
INNER JOIN vacation_types vt ON vr.vacation_type_id = vt.id
WHERE vr.status = 'pending'
ORDER BY vr.created_at ASC
@@ -128,17 +138,21 @@ const vacationRequestModel = {
const db = getPool();
const fs = require('fs');
const path = require('path');
const sqlFile = path.join(__dirname, '..', 'db', 'migrations', '001_create_sp_tables.sql');
const sql = fs.readFileSync(sqlFile, 'utf8');
const statements = sql.split(';').map(s => s.trim()).filter(s => s.length > 0);
for (const stmt of statements) {
try {
await db.query(stmt);
} catch (err) {
if (err.code === 'ER_DUP_FIELDNAME' || err.code === 'ER_TABLE_EXISTS_ERROR') {
// Already migrated
} else {
throw err;
const migrationFiles = ['001_create_sp_tables.sql', '002_section_c_additions.sql'];
for (const file of migrationFiles) {
const sqlFile = path.join(__dirname, '..', 'db', 'migrations', file);
if (!fs.existsSync(sqlFile)) continue;
const sql = fs.readFileSync(sqlFile, 'utf8');
const statements = sql.split(';').map(s => s.trim()).filter(s => s.length > 0);
for (const stmt of statements) {
try {
await db.query(stmt);
} catch (err) {
if (err.code === 'ER_DUP_FIELDNAME' || err.code === 'ER_TABLE_EXISTS_ERROR') {
// Already migrated
} else {
throw err;
}
}
}
}