import { insertReturning, query } from '../../utils/db' import { getCurrentUserId } from '../../utils/user' interface TaskItem { requestDate: string | null requestTitle: string requestContent: string | null requesterName: string | null requesterContact: string | null taskType: string priority: string resolutionContent: string | null isDuplicate?: boolean selected?: boolean } interface BulkCreateBody { projectId: number batchId: number tasks: TaskItem[] } /** * 유지보수 업무 일괄 등록 * POST /api/maintenance/bulk-create */ export default defineEventHandler(async (event) => { const body = await readBody(event) const userId = await getCurrentUserId(event) if (!body.projectId) { throw createError({ statusCode: 400, message: '프로젝트를 선택해주세요.' }) } if (!body.tasks || body.tasks.length === 0) { throw createError({ statusCode: 400, message: '등록할 업무가 없습니다.' }) } // 선택된 항목만 필터 (selected가 false가 아닌 것) const tasksToInsert = body.tasks.filter(t => t.selected !== false && !t.isDuplicate) if (tasksToInsert.length === 0) { throw createError({ statusCode: 400, message: '등록할 업무가 없습니다. (모두 제외되었거나 중복)' }) } const inserted: number[] = [] const errors: string[] = [] for (const task of tasksToInsert) { try { const result = await insertReturning(` INSERT INTO wr_maintenance_task ( project_id, batch_id, request_date, request_title, request_content, requester_name, requester_contact, task_type, priority, status, resolution_content, created_by ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 'PENDING', $10, $11) RETURNING task_id `, [ body.projectId, body.batchId, task.requestDate || null, task.requestTitle, task.requestContent || null, task.requesterName || null, task.requesterContact || null, task.taskType || 'other', task.priority || 'medium', task.resolutionContent || null, userId ]) inserted.push(result.task_id) } catch (e: any) { errors.push(`${task.requestTitle}: ${e.message}`) } } return { success: true, insertedCount: inserted.length, errorCount: errors.length, errors: errors.length > 0 ? errors : undefined, taskIds: inserted } })