import { query } from '../../utils/db' import { requireAuth } from '../../utils/session' /** * 내 주간 커밋 조회 (주간보고 작성용) * GET /api/commits/my-weekly * * Query params: * - projectId: 프로젝트 ID (필수) * - startDate: 주 시작일 (YYYY-MM-DD) * - endDate: 주 종료일 (YYYY-MM-DD) */ export default defineEventHandler(async (event) => { const employeeId = await requireAuth(event) const queryParams = getQuery(event) const projectId = queryParams.projectId ? parseInt(queryParams.projectId as string) : null const startDate = queryParams.startDate as string const endDate = queryParams.endDate as string if (!projectId) { throw createError({ statusCode: 400, message: '프로젝트 ID가 필요합니다.' }) } // 조건 빌드 const conditions = [ 'r.project_id = $1', 'c.employee_id = $2' ] const values: any[] = [projectId, employeeId] let paramIndex = 3 if (startDate) { conditions.push(`c.commit_date >= $${paramIndex++}`) values.push(startDate) } if (endDate) { conditions.push(`c.commit_date <= $${paramIndex++}::date + INTERVAL '1 day'`) values.push(endDate) } const whereClause = conditions.join(' AND ') // 내 커밋 목록 const commits = await query(` SELECT c.commit_id, c.commit_hash, c.commit_message, c.commit_date, c.files_changed, c.insertions, c.deletions, r.repo_id, r.repo_name, s.server_type FROM wr_commit_log c JOIN wr_repository r ON c.repo_id = r.repo_id JOIN wr_vcs_server s ON r.server_id = s.server_id WHERE ${whereClause} ORDER BY c.commit_date DESC LIMIT 100 `, values) // 통계 const statsResult = await query(` SELECT COUNT(*) as commit_count, COALESCE(SUM(c.insertions), 0) as total_insertions, COALESCE(SUM(c.deletions), 0) as total_deletions FROM wr_commit_log c JOIN wr_repository r ON c.repo_id = r.repo_id WHERE ${whereClause} `, values) return { commits: commits.map(c => ({ commitId: c.commit_id, commitHash: c.commit_hash?.substring(0, 8), commitMessage: c.commit_message, commitDate: c.commit_date, filesChanged: c.files_changed, insertions: c.insertions, deletions: c.deletions, repoId: c.repo_id, repoName: c.repo_name, serverType: c.server_type })), stats: { commitCount: parseInt(statsResult[0]?.commit_count || '0'), totalInsertions: parseInt(statsResult[0]?.total_insertions || '0'), totalDeletions: parseInt(statsResult[0]?.total_deletions || '0') } } })