76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { insertReturning, queryOne } from '../../../utils/db'
|
|
import { getWeekInfo } from '../../../utils/week-calc'
|
|
|
|
interface CreateReportBody {
|
|
projectId: number
|
|
reportYear?: number
|
|
reportWeek?: number
|
|
workDescription?: string
|
|
planDescription?: string
|
|
issueDescription?: string
|
|
remarkDescription?: string
|
|
workHours?: number
|
|
}
|
|
|
|
/**
|
|
* 주간보고 작성
|
|
* POST /api/report/weekly/create
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const userId = getCookie(event, 'user_id')
|
|
if (!userId) {
|
|
throw createError({ statusCode: 401, message: '로그인이 필요합니다.' })
|
|
}
|
|
|
|
const body = await readBody<CreateReportBody>(event)
|
|
|
|
if (!body.projectId) {
|
|
throw createError({ statusCode: 400, message: '프로젝트를 선택해주세요.' })
|
|
}
|
|
|
|
// 주차 정보 (기본값: 이번 주)
|
|
const weekInfo = getWeekInfo()
|
|
const year = body.reportYear || weekInfo.year
|
|
const week = body.reportWeek || weekInfo.week
|
|
|
|
// 중복 체크
|
|
const existing = await queryOne(`
|
|
SELECT report_id FROM wr_weekly_report_detail
|
|
WHERE project_id = $1 AND author_id = $2 AND report_year = $3 AND report_week = $4
|
|
`, [body.projectId, parseInt(userId), year, week])
|
|
|
|
if (existing) {
|
|
throw createError({ statusCode: 409, message: '이미 해당 주차 보고서가 존재합니다.' })
|
|
}
|
|
|
|
// 주차 날짜 계산
|
|
const dates = getWeekInfo(new Date(year, 0, 4 + (week - 1) * 7))
|
|
|
|
const report = await insertReturning(`
|
|
INSERT INTO wr_weekly_report_detail (
|
|
project_id, author_id, report_year, report_week,
|
|
week_start_date, week_end_date,
|
|
work_description, plan_description, issue_description, remark_description,
|
|
work_hours, report_status
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, 'DRAFT')
|
|
RETURNING *
|
|
`, [
|
|
body.projectId,
|
|
parseInt(userId),
|
|
year,
|
|
week,
|
|
dates.startDateStr,
|
|
dates.endDateStr,
|
|
body.workDescription || null,
|
|
body.planDescription || null,
|
|
body.issueDescription || null,
|
|
body.remarkDescription || null,
|
|
body.workHours || null
|
|
])
|
|
|
|
return {
|
|
success: true,
|
|
reportId: report.report_id
|
|
}
|
|
})
|