Files
tk-factory-services/tksupport/api/models/vacationBalanceModel.js
Hyungi Ahn 3011495e6d feat(tksupport): 전사 행정지원 서비스 신규 구축 (Phase 1 - 휴가신청)
sso_users 기반 전사 휴가신청/승인/잔여일 관리 서비스.
기존 tkfb의 workers 종속 휴가 기능을 전사 확장.

- API: Express + MariaDB, SSO JWT 인증, 자동 마이그레이션
- Web: 대시보드, 휴가 신청/현황/승인 페이지 (보라색 테마)
- DB: sp_vacation_requests, sp_vacation_balances 신규 테이블
- Docker: API(30600), Web(30680) 포트 구성

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 15:39:59 +09:00

97 lines
3.1 KiB
JavaScript

const { getPool } = require('../middleware/auth');
const vacationBalanceModel = {
async getByUserAndYear(userId, year) {
const db = getPool();
const [rows] = await db.query(`
SELECT
vb.*,
vt.type_name,
vt.type_code,
vt.priority,
vt.is_special,
(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 = ?
ORDER BY vt.priority ASC, vt.type_name ASC
`, [userId, year]);
return rows;
},
async getAllByYear(year) {
const db = getPool();
const [rows] = await db.query(`
SELECT
vb.*,
su.name as user_name,
su.username,
su.hire_date,
vt.type_name,
vt.type_code,
vt.priority,
(vb.total_days - vb.used_days) as remaining_days
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
ORDER BY su.name ASC, vt.priority ASC
`, [year]);
return rows;
},
async allocate(data) {
const db = getPool();
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, ?, ?)
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]);
return result;
},
async deductDays(userId, vacationTypeId, year, daysToDeduct) {
const db = getPool();
const [result] = await db.query(`
UPDATE sp_vacation_balances
SET used_days = used_days + ?, updated_at = NOW()
WHERE user_id = ? AND vacation_type_id = ? AND year = ?
`, [daysToDeduct, userId, vacationTypeId, year]);
return result;
},
async restoreDays(userId, vacationTypeId, year, daysToRestore) {
const db = getPool();
const [result] = await db.query(`
UPDATE sp_vacation_balances
SET used_days = GREATEST(0, used_days - ?), updated_at = NOW()
WHERE user_id = ? AND vacation_type_id = ? AND year = ?
`, [daysToRestore, userId, vacationTypeId, year]);
return result;
},
calculateAnnualLeaveDays(hireDate, targetYear) {
const hire = new Date(hireDate);
const targetDate = new Date(targetYear, 0, 1);
const monthsDiff = (targetDate.getFullYear() - hire.getFullYear()) * 12
+ (targetDate.getMonth() - hire.getMonth());
if (monthsDiff < 12) {
return Math.floor(monthsDiff);
}
const yearsWorked = Math.floor(monthsDiff / 12);
const additionalDays = Math.floor((yearsWorked - 1) / 2);
return Math.min(15 + additionalDays, 25);
},
async getUserHireDate(userId) {
const db = getPool();
const [rows] = await db.query('SELECT hire_date FROM sso_users WHERE user_id = ?', [userId]);
return rows.length > 0 ? rows[0].hire_date : null;
}
};
module.exports = vacationBalanceModel;