1ㅊㅏ완료
This commit is contained in:
@@ -1,29 +1,27 @@
|
||||
import { query, queryOne, insertReturning, execute } from '../../utils/db'
|
||||
import { getClientIp } from '../../utils/ip'
|
||||
import { query, execute, queryOne } from '../../utils/db'
|
||||
|
||||
const ADMIN_EMAIL = 'coziny@gmail.com'
|
||||
|
||||
interface TaskInput {
|
||||
description: string
|
||||
hours: number
|
||||
}
|
||||
|
||||
interface ProjectInput {
|
||||
projectId: number | null // null이면 신규 생성
|
||||
projectId: number | null
|
||||
projectName: string
|
||||
workDescription: string | null
|
||||
planDescription: string | null
|
||||
workTasks: TaskInput[]
|
||||
planTasks: TaskInput[]
|
||||
}
|
||||
|
||||
interface ReportInput {
|
||||
employeeId: number
|
||||
employeeId: number | null
|
||||
employeeName: string
|
||||
employeeEmail: string
|
||||
projects: ProjectInput[]
|
||||
issueDescription: string | null
|
||||
vacationDescription: string | null
|
||||
remarkDescription: string | null
|
||||
}
|
||||
|
||||
interface BulkRegisterBody {
|
||||
reportYear: number
|
||||
reportWeek: number
|
||||
weekStartDate: string
|
||||
weekEndDate: string
|
||||
reports: ReportInput[]
|
||||
issueDescription?: string
|
||||
vacationDescription?: string
|
||||
remarkDescription?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,167 +35,152 @@ export default defineEventHandler(async (event) => {
|
||||
throw createError({ statusCode: 401, message: '로그인이 필요합니다.' })
|
||||
}
|
||||
|
||||
const currentUser = await queryOne<any>(`
|
||||
const clientIp = getHeader(event, 'x-forwarded-for') || 'unknown'
|
||||
|
||||
const currentUser = await query<any>(`
|
||||
SELECT employee_email FROM wr_employee_info WHERE employee_id = $1
|
||||
`, [userId])
|
||||
|
||||
if (!currentUser || currentUser.employee_email !== ADMIN_EMAIL) {
|
||||
if (!currentUser[0] || currentUser[0].employee_email !== ADMIN_EMAIL) {
|
||||
throw createError({ statusCode: 403, message: '관리자만 사용할 수 있습니다.' })
|
||||
}
|
||||
|
||||
const body = await readBody<BulkRegisterBody>(event)
|
||||
const clientIp = getClientIp(event)
|
||||
const adminEmail = currentUser[0].employee_email
|
||||
|
||||
if (!body.reports || body.reports.length === 0) {
|
||||
throw createError({ statusCode: 400, message: '등록할 보고서가 없습니다.' })
|
||||
}
|
||||
const body = await readBody<{
|
||||
reportYear: number
|
||||
reportWeek: number
|
||||
weekStartDate: string
|
||||
weekEndDate: string
|
||||
reports: ReportInput[]
|
||||
}>(event)
|
||||
|
||||
const results: any[] = []
|
||||
|
||||
for (const report of body.reports) {
|
||||
try {
|
||||
// 1. 프로젝트 처리 (신규 생성 또는 기존 사용)
|
||||
const projectIds: number[] = []
|
||||
let employeeId = report.employeeId
|
||||
let isNewEmployee = false
|
||||
const newProjects: string[] = []
|
||||
|
||||
// 신규 직원 생성
|
||||
if (!employeeId && report.employeeName && report.employeeEmail) {
|
||||
const newEmp = await queryOne<any>(`
|
||||
INSERT INTO wr_employee_info (employee_name, employee_email, is_active, created_ip, created_email, updated_ip, updated_email)
|
||||
VALUES ($1, $2, true, $3, $4, $3, $4)
|
||||
RETURNING employee_id
|
||||
`, [report.employeeName, report.employeeEmail, clientIp, adminEmail])
|
||||
employeeId = newEmp.employee_id
|
||||
isNewEmployee = true
|
||||
}
|
||||
|
||||
if (!employeeId) {
|
||||
results.push({
|
||||
success: false,
|
||||
employeeName: report.employeeName,
|
||||
employeeEmail: report.employeeEmail,
|
||||
error: '직원 정보가 없습니다.'
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// 기존 보고서 확인 및 삭제 (덮어쓰기)
|
||||
const existing = await queryOne<any>(`
|
||||
SELECT report_id FROM wr_weekly_report
|
||||
WHERE author_id = $1 AND report_year = $2 AND report_week = $3
|
||||
`, [employeeId, body.reportYear, body.reportWeek])
|
||||
|
||||
let isUpdate = false
|
||||
if (existing) {
|
||||
await execute(`DELETE FROM wr_weekly_report_task WHERE report_id = $1`, [existing.report_id])
|
||||
await execute(`DELETE FROM wr_weekly_report WHERE report_id = $1`, [existing.report_id])
|
||||
isUpdate = true
|
||||
}
|
||||
|
||||
// 주간보고 마스터 등록
|
||||
const newReport = await queryOne<any>(`
|
||||
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, submitted_at, created_ip, created_email, updated_ip, updated_email
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'SUBMITTED', NOW(), $9, $10, $9, $10)
|
||||
RETURNING report_id
|
||||
`, [
|
||||
employeeId, body.reportYear, body.reportWeek, body.weekStartDate, body.weekEndDate,
|
||||
report.issueDescription || null, report.vacationDescription || null, report.remarkDescription || null,
|
||||
clientIp, adminEmail
|
||||
])
|
||||
|
||||
const reportId = newReport.report_id
|
||||
|
||||
// 프로젝트별 Task 등록
|
||||
for (const proj of report.projects) {
|
||||
let projectId = proj.projectId
|
||||
|
||||
if (!projectId) {
|
||||
// 신규 프로젝트 생성
|
||||
// 신규 프로젝트 생성
|
||||
if (!projectId && proj.projectName) {
|
||||
const year = new Date().getFullYear()
|
||||
const lastProject = await queryOne<any>(`
|
||||
SELECT project_code FROM wr_project_info
|
||||
WHERE project_code LIKE $1
|
||||
ORDER BY project_code DESC LIMIT 1
|
||||
const codeResult = await queryOne<any>(`
|
||||
SELECT COALESCE(MAX(CAST(SUBSTRING(project_code FROM 6) AS INTEGER)), 0) + 1 as next_num
|
||||
FROM wr_project_info WHERE project_code LIKE $1
|
||||
`, [`${year}-%`])
|
||||
const projectCode = `${year}-${String(codeResult.next_num).padStart(3, '0')}`
|
||||
|
||||
let nextNum = 1
|
||||
if (lastProject?.project_code) {
|
||||
const lastNum = parseInt(lastProject.project_code.split('-')[1]) || 0
|
||||
nextNum = lastNum + 1
|
||||
}
|
||||
const newCode = `${year}-${String(nextNum).padStart(3, '0')}`
|
||||
|
||||
const newProject = await insertReturning(`
|
||||
INSERT INTO wr_project_info (
|
||||
project_code, project_name, project_type,
|
||||
created_ip, created_email, updated_ip, updated_email
|
||||
) VALUES ($1, $2, 'SI', $3, $4, $3, $4)
|
||||
const newProj = await queryOne<any>(`
|
||||
INSERT INTO wr_project_info (project_code, project_name, project_status, created_ip, created_email, updated_ip, updated_email)
|
||||
VALUES ($1, $2, 'IN_PROGRESS', $3, $4, $3, $4)
|
||||
RETURNING project_id
|
||||
`, [newCode, proj.projectName, clientIp, ADMIN_EMAIL])
|
||||
|
||||
projectId = newProject.project_id
|
||||
`, [projectCode, proj.projectName, clientIp, adminEmail])
|
||||
projectId = newProj.project_id
|
||||
newProjects.push(proj.projectName)
|
||||
}
|
||||
|
||||
projectIds.push(projectId)
|
||||
}
|
||||
|
||||
// 2. 기존 주간보고 확인 (덮어쓰기)
|
||||
const existingReport = await queryOne<any>(`
|
||||
SELECT report_id FROM wr_weekly_report
|
||||
WHERE author_id = $1 AND report_year = $2 AND report_week = $3
|
||||
`, [report.employeeId, body.reportYear, body.reportWeek])
|
||||
|
||||
let reportId: number
|
||||
|
||||
if (existingReport) {
|
||||
// 기존 보고서 업데이트
|
||||
reportId = existingReport.report_id
|
||||
if (!projectId) continue
|
||||
|
||||
// 기존 프로젝트 실적 삭제
|
||||
await execute(`DELETE FROM wr_weekly_report_project WHERE report_id = $1`, [reportId])
|
||||
// 금주실적 Task 등록
|
||||
for (const task of proj.workTasks || []) {
|
||||
await execute(`
|
||||
INSERT INTO wr_weekly_report_task (
|
||||
report_id, project_id, task_type, task_description, task_hours, is_completed,
|
||||
created_ip, created_email, updated_ip, updated_email
|
||||
) VALUES ($1, $2, 'WORK', $3, $4, $5, $6, $7, $6, $7)
|
||||
`, [reportId, projectId, task.description, task.hours || 0, task.isCompleted !== false, clientIp, adminEmail])
|
||||
}
|
||||
|
||||
// 마스터 업데이트
|
||||
await execute(`
|
||||
UPDATE wr_weekly_report SET
|
||||
issue_description = $1,
|
||||
vacation_description = $2,
|
||||
remark_description = $3,
|
||||
report_status = 'SUBMITTED',
|
||||
submitted_at = NOW(),
|
||||
updated_at = NOW(),
|
||||
updated_ip = $4,
|
||||
updated_email = $5
|
||||
WHERE report_id = $6
|
||||
`, [
|
||||
report.issueDescription,
|
||||
report.vacationDescription,
|
||||
report.remarkDescription,
|
||||
clientIp,
|
||||
ADMIN_EMAIL,
|
||||
reportId
|
||||
])
|
||||
} else {
|
||||
// 신규 보고서 생성
|
||||
const newReport = 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, submitted_at,
|
||||
created_ip, created_email, updated_ip, updated_email
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'SUBMITTED', NOW(), $9, $10, $9, $10)
|
||||
RETURNING report_id
|
||||
`, [
|
||||
report.employeeId,
|
||||
body.reportYear,
|
||||
body.reportWeek,
|
||||
body.weekStartDate,
|
||||
body.weekEndDate,
|
||||
report.issueDescription,
|
||||
report.vacationDescription,
|
||||
report.remarkDescription,
|
||||
clientIp,
|
||||
ADMIN_EMAIL
|
||||
])
|
||||
reportId = newReport.report_id
|
||||
// 차주계획 Task 등록
|
||||
for (const task of proj.planTasks || []) {
|
||||
await execute(`
|
||||
INSERT INTO wr_weekly_report_task (
|
||||
report_id, project_id, task_type, task_description, task_hours,
|
||||
created_ip, created_email, updated_ip, updated_email
|
||||
) VALUES ($1, $2, 'PLAN', $3, $4, $5, $6, $5, $6)
|
||||
`, [reportId, projectId, task.description, task.hours || 0, clientIp, adminEmail])
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 프로젝트별 실적 등록
|
||||
for (let i = 0; i < report.projects.length; i++) {
|
||||
const proj = report.projects[i]
|
||||
const projectId = projectIds[i]
|
||||
|
||||
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)
|
||||
`, [
|
||||
reportId,
|
||||
projectId,
|
||||
proj.workDescription,
|
||||
proj.planDescription,
|
||||
clientIp,
|
||||
ADMIN_EMAIL
|
||||
])
|
||||
}
|
||||
|
||||
// 직원 정보 조회
|
||||
const employee = await queryOne<any>(`
|
||||
SELECT employee_name FROM wr_employee_info WHERE employee_id = $1
|
||||
`, [report.employeeId])
|
||||
|
||||
results.push({
|
||||
success: true,
|
||||
employeeId: report.employeeId,
|
||||
employeeName: employee?.employee_name,
|
||||
employeeId,
|
||||
employeeName: report.employeeName,
|
||||
employeeEmail: report.employeeEmail,
|
||||
reportId,
|
||||
isUpdate: !!existingReport
|
||||
isUpdate,
|
||||
isNewEmployee,
|
||||
newProjects
|
||||
})
|
||||
|
||||
} catch (err: any) {
|
||||
} catch (e: any) {
|
||||
results.push({
|
||||
success: false,
|
||||
employeeId: report.employeeId,
|
||||
error: err.message
|
||||
employeeName: report.employeeName,
|
||||
employeeEmail: report.employeeEmail,
|
||||
error: e.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
totalCount: body.reports.length,
|
||||
totalCount: results.length,
|
||||
successCount: results.filter(r => r.success).length,
|
||||
results
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user