추가
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
import { insertReturning, queryOne } from '../../../utils/db'
|
||||
import { query, insertReturning, execute } from '../../../utils/db'
|
||||
import { getWeekInfo } from '../../../utils/week-calc'
|
||||
import { getClientIp } from '../../../utils/ip'
|
||||
import { getCurrentUserEmail } from '../../../utils/user'
|
||||
|
||||
interface CreateReportBody {
|
||||
interface ProjectItem {
|
||||
projectId: number
|
||||
reportYear?: number
|
||||
reportWeek?: number
|
||||
workDescription?: string
|
||||
planDescription?: string
|
||||
}
|
||||
|
||||
interface CreateReportBody {
|
||||
reportYear?: number
|
||||
reportWeek?: number
|
||||
projects: ProjectItem[]
|
||||
issueDescription?: string
|
||||
vacationDescription?: string
|
||||
remarkDescription?: string
|
||||
workHours?: number
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,9 +29,11 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
const body = await readBody<CreateReportBody>(event)
|
||||
const clientIp = getClientIp(event)
|
||||
const userEmail = await getCurrentUserEmail(event)
|
||||
|
||||
if (!body.projectId) {
|
||||
throw createError({ statusCode: 400, message: '프로젝트를 선택해주세요.' })
|
||||
if (!body.projects || body.projects.length === 0) {
|
||||
throw createError({ statusCode: 400, message: '최소 1개 이상의 프로젝트를 추가해주세요.' })
|
||||
}
|
||||
|
||||
// 주차 정보 (기본값: 이번 주)
|
||||
@@ -34,40 +42,57 @@ export default defineEventHandler(async (event) => {
|
||||
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])
|
||||
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) {
|
||||
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_detail (
|
||||
project_id, author_id, report_year, report_week,
|
||||
INSERT INTO wr_weekly_report (
|
||||
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')
|
||||
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 *
|
||||
`, [
|
||||
body.projectId,
|
||||
parseInt(userId),
|
||||
year,
|
||||
week,
|
||||
dates.startDateStr,
|
||||
dates.endDateStr,
|
||||
body.workDescription || null,
|
||||
body.planDescription || null,
|
||||
body.issueDescription || null,
|
||||
body.vacationDescription || null,
|
||||
body.remarkDescription || null,
|
||||
body.workHours || 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
|
||||
|
||||
Reference in New Issue
Block a user