101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
import { query, insertReturning, execute } from '../../../utils/db'
|
|
import { getWeekInfo } from '../../../utils/week-calc'
|
|
import { getClientIp } from '../../../utils/ip'
|
|
import { getCurrentUserEmail } from '../../../utils/user'
|
|
|
|
interface ProjectItem {
|
|
projectId: number
|
|
workDescription?: string
|
|
planDescription?: string
|
|
}
|
|
|
|
interface CreateReportBody {
|
|
reportYear?: number
|
|
reportWeek?: number
|
|
projects: ProjectItem[]
|
|
issueDescription?: string
|
|
vacationDescription?: string
|
|
remarkDescription?: string
|
|
}
|
|
|
|
/**
|
|
* 주간보고 작성
|
|
* 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)
|
|
const clientIp = getClientIp(event)
|
|
const userEmail = await getCurrentUserEmail(event)
|
|
|
|
if (!body.projects || body.projects.length === 0) {
|
|
throw createError({ statusCode: 400, message: '최소 1개 이상의 프로젝트를 추가해주세요.' })
|
|
}
|
|
|
|
// 주차 정보 (기본값: 이번 주)
|
|
const weekInfo = getWeekInfo()
|
|
const year = body.reportYear || weekInfo.year
|
|
const week = body.reportWeek || weekInfo.week
|
|
|
|
// 중복 체크
|
|
const existing = await query(`
|
|
SELECT report_id FROM wr_weekly_report
|
|
WHERE author_id = $1 AND report_year = $2 AND report_week = $3
|
|
`, [parseInt(userId), year, week])
|
|
|
|
if (existing.length > 0) {
|
|
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 (
|
|
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 *
|
|
`, [
|
|
parseInt(userId),
|
|
year,
|
|
week,
|
|
dates.startDateStr,
|
|
dates.endDateStr,
|
|
body.issueDescription || null,
|
|
body.vacationDescription || null,
|
|
body.remarkDescription || null,
|
|
clientIp,
|
|
userEmail
|
|
])
|
|
|
|
// 프로젝트별 실적 저장
|
|
for (const proj of body.projects) {
|
|
await execute(`
|
|
INSERT INTO wr_weekly_report_project (
|
|
report_id, project_id, work_description, plan_description,
|
|
created_ip, created_email, updated_ip, updated_email
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $5, $6)
|
|
`, [
|
|
report.report_id,
|
|
proj.projectId,
|
|
proj.workDescription || null,
|
|
proj.planDescription || null,
|
|
clientIp,
|
|
userEmail
|
|
])
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
reportId: report.report_id
|
|
}
|
|
})
|