import { execute, queryOne, insertReturning } from '../../../utils/db' import { formatDate } from '../../../utils/week-calc' interface AssignManagerBody { employeeId: number roleType: 'PM' | 'PL' startDate?: string changeReason?: string } /** * PM/PL 지정 (기존 담당자 종료 + 신규 등록) * POST /api/project/[id]/manager-assign */ export default defineEventHandler(async (event) => { const projectId = getRouterParam(event, 'id') const body = await readBody(event) if (!body.employeeId || !body.roleType) { throw createError({ statusCode: 400, message: '담당자와 역할을 선택해주세요.' }) } if (!['PM', 'PL'].includes(body.roleType)) { throw createError({ statusCode: 400, message: '역할은 PM 또는 PL만 가능합니다.' }) } const today = formatDate(new Date()) const startDate = body.startDate || today // 기존 담당자 종료 (end_date가 NULL인 경우) await execute(` UPDATE wr_project_manager_history SET end_date = $1, change_reason = COALESCE(change_reason || ' / ', '') || '신규 담당자 지정으로 종료' WHERE project_id = $2 AND role_type = $3 AND end_date IS NULL `, [startDate, projectId, body.roleType]) // 신규 담당자 등록 const history = await insertReturning(` INSERT INTO wr_project_manager_history ( project_id, employee_id, role_type, start_date, change_reason ) VALUES ($1, $2, $3, $4, $5) RETURNING * `, [projectId, body.employeeId, body.roleType, startDate, body.changeReason || null]) return { success: true, historyId: history.history_id } })