대시보드와 주간보고 기능 업데이트

This commit is contained in:
2026-01-10 14:40:01 +09:00
parent 0dd4b561f0
commit e4627caa4c
26 changed files with 3329 additions and 1720 deletions

View File

@@ -1,17 +1,32 @@
import { query } from '../../utils/db'
import { getSession, getSessionIdFromCookie, deleteSessionCookie, SESSION_TIMEOUT_MINUTES } from '../../utils/session'
/**
* 본인 로그인 이력 조회
* GET /api/auth/login-history
*/
export default defineEventHandler(async (event) => {
const userId = getCookie(event, 'user_id')
const currentHistoryId = getCookie(event, 'login_history_id')
const sessionId = getSessionIdFromCookie(event)
if (!userId) {
if (!sessionId) {
throw createError({ statusCode: 401, message: '로그인이 필요합니다.' })
}
const session = await getSession(sessionId)
if (!session) {
deleteSessionCookie(event)
throw createError({ statusCode: 401, message: '세션이 만료되었습니다.' })
}
// 현재 활성 세션 ID 목록 조회
const activeSessions = await query<any>(`
SELECT login_history_id FROM wr_session
WHERE employee_id = $1 AND expires_at > NOW()
`, [session.employeeId])
const activeHistoryIds = new Set(activeSessions.map(s => s.login_history_id))
// 로그인 이력 조회
const history = await query<any>(`
SELECT
history_id,
@@ -24,17 +39,37 @@ export default defineEventHandler(async (event) => {
WHERE employee_id = $1
ORDER BY login_at DESC
LIMIT 50
`, [userId])
`, [session.employeeId])
const SESSION_TIMEOUT_MS = SESSION_TIMEOUT_MINUTES * 60 * 1000
const now = Date.now()
return {
history: history.map(h => ({
historyId: h.history_id,
loginAt: h.login_at,
loginIp: h.login_ip,
logoutAt: h.logout_at,
logoutIp: h.logout_ip,
lastActiveAt: h.last_active_at,
isCurrentSession: currentHistoryId && h.history_id === parseInt(currentHistoryId)
}))
history: history.map(h => {
const isCurrentSession = h.history_id === session.loginHistoryId
const isActiveSession = activeHistoryIds.has(h.history_id)
// 세션 상태 판단
let sessionStatus: 'active' | 'logout' | 'expired'
if (h.logout_at) {
sessionStatus = 'logout'
} else if (isActiveSession) {
sessionStatus = 'active'
} else {
// 활성 세션에 없으면 만료
sessionStatus = 'expired'
}
return {
historyId: h.history_id,
loginAt: h.login_at,
loginIp: h.login_ip,
logoutAt: h.logout_at,
logoutIp: h.logout_ip,
lastActiveAt: h.last_active_at,
isCurrentSession,
sessionStatus
}
})
}
})