107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
import { query, execute, queryOne } from '../../../../utils/db'
|
|
|
|
const ADMIN_EMAIL = 'coziny@gmail.com'
|
|
|
|
/**
|
|
* 주간보고 수정
|
|
* PUT /api/report/weekly/[id]/update
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const userId = getCookie(event, 'user_id')
|
|
if (!userId) {
|
|
throw createError({ statusCode: 401, message: '로그인이 필요합니다.' })
|
|
}
|
|
|
|
const reportId = getRouterParam(event, 'id')
|
|
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 isAdmin = userEmail === ADMIN_EMAIL
|
|
|
|
// 보고서 조회 및 권한 체크
|
|
const report = await queryOne<any>(`
|
|
SELECT report_id, author_id, report_status FROM wr_weekly_report WHERE report_id = $1
|
|
`, [reportId])
|
|
|
|
if (!report) {
|
|
throw createError({ statusCode: 404, message: '보고서를 찾을 수 없습니다.' })
|
|
}
|
|
|
|
// 관리자가 아니면 본인 보고서만 수정 가능
|
|
if (!isAdmin && report.author_id !== parseInt(userId)) {
|
|
throw createError({ statusCode: 403, message: '본인의 보고서만 수정할 수 있습니다.' })
|
|
}
|
|
|
|
// 취합완료된 보고서는 수정 불가 (관리자도)
|
|
if (report.report_status === 'AGGREGATED') {
|
|
throw createError({ statusCode: 400, message: '취합완료된 보고서는 수정할 수 없습니다.' })
|
|
}
|
|
|
|
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.tasks || body.tasks.length === 0) {
|
|
throw createError({ statusCode: 400, message: '최소 1개 이상의 Task가 필요합니다.' })
|
|
}
|
|
|
|
// 마스터 수정
|
|
await execute(`
|
|
UPDATE wr_weekly_report SET
|
|
report_year = COALESCE($1, report_year),
|
|
report_week = COALESCE($2, report_week),
|
|
week_start_date = COALESCE($3, week_start_date),
|
|
week_end_date = COALESCE($4, week_end_date),
|
|
issue_description = $5,
|
|
vacation_description = $6,
|
|
remark_description = $7,
|
|
updated_at = NOW(),
|
|
updated_ip = $8,
|
|
updated_email = $9
|
|
WHERE report_id = $10
|
|
`, [
|
|
body.reportYear || null,
|
|
body.reportWeek || null,
|
|
body.weekStartDate || null,
|
|
body.weekEndDate || null,
|
|
body.issueDescription || null,
|
|
body.vacationDescription || null,
|
|
body.remarkDescription || null,
|
|
clientIp, userEmail, reportId
|
|
])
|
|
|
|
// 기존 Task 삭제 후 재등록
|
|
await execute(`DELETE FROM wr_weekly_report_task WHERE report_id = $1`, [reportId])
|
|
|
|
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,
|
|
message: '주간보고가 수정되었습니다.'
|
|
}
|
|
})
|