89 lines
2.9 KiB
TypeScript
89 lines
2.9 KiB
TypeScript
import { query, execute, queryOne } from '../../../utils/db'
|
|
|
|
/**
|
|
* 주간보고 작성
|
|
* POST /api/report/weekly/create
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const userId = getCookie(event, 'user_id')
|
|
if (!userId) {
|
|
throw createError({ statusCode: 401, message: '로그인이 필요합니다.' })
|
|
}
|
|
|
|
const clientIp = getHeader(event, 'x-forwarded-for') || 'unknown'
|
|
const user = await queryOne<any>(`SELECT employee_email FROM wr_employee_info WHERE employee_id = $1`, [userId])
|
|
const userEmail = user?.employee_email || ''
|
|
|
|
const body = await readBody<{
|
|
reportYear: number
|
|
reportWeek: number
|
|
weekStartDate: string
|
|
weekEndDate: string
|
|
tasks: {
|
|
projectId: number
|
|
taskType: 'WORK' | 'PLAN'
|
|
taskDescription: string
|
|
taskHours: number
|
|
isCompleted?: boolean
|
|
}[]
|
|
issueDescription?: string
|
|
vacationDescription?: string
|
|
remarkDescription?: string
|
|
}>(event)
|
|
|
|
// 필수값 체크
|
|
if (!body.reportYear || !body.reportWeek || !body.weekStartDate || !body.weekEndDate) {
|
|
throw createError({ statusCode: 400, message: '주차 정보가 필요합니다.' })
|
|
}
|
|
|
|
if (!body.tasks || body.tasks.length === 0) {
|
|
throw createError({ statusCode: 400, message: '최소 1개 이상의 Task가 필요합니다.' })
|
|
}
|
|
|
|
// 중복 체크
|
|
const existing = await queryOne<any>(`
|
|
SELECT report_id FROM wr_weekly_report
|
|
WHERE author_id = $1 AND report_year = $2 AND report_week = $3
|
|
`, [userId, body.reportYear, body.reportWeek])
|
|
|
|
if (existing) {
|
|
throw createError({ statusCode: 409, message: '해당 주차에 이미 작성된 보고서가 있습니다.' })
|
|
}
|
|
|
|
// 마스터 등록
|
|
const result = await queryOne<any>(`
|
|
INSERT INTO wr_weekly_report (
|
|
author_id, report_year, report_week, week_start_date, week_end_date,
|
|
issue_description, vacation_description, remark_description,
|
|
report_status, created_ip, created_email, updated_ip, updated_email
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'DRAFT', $9, $10, $9, $10)
|
|
RETURNING report_id
|
|
`, [
|
|
userId, body.reportYear, body.reportWeek, body.weekStartDate, body.weekEndDate,
|
|
body.issueDescription || null, body.vacationDescription || null, body.remarkDescription || null,
|
|
clientIp, userEmail
|
|
])
|
|
|
|
const reportId = result.report_id
|
|
|
|
// Task 등록
|
|
for (const task of body.tasks) {
|
|
await execute(`
|
|
INSERT INTO wr_weekly_report_task (
|
|
report_id, project_id, task_type, task_description, task_hours, is_completed,
|
|
created_ip, created_email, updated_ip, updated_email
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $7, $8)
|
|
`, [
|
|
reportId, task.projectId, task.taskType, task.taskDescription, task.taskHours || 0,
|
|
task.taskType === 'WORK' ? (task.isCompleted !== false) : null,
|
|
clientIp, userEmail
|
|
])
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
reportId,
|
|
message: '주간보고가 작성되었습니다.'
|
|
}
|
|
})
|