92 lines
2.5 KiB
TypeScript
92 lines
2.5 KiB
TypeScript
import { query, insertReturning } from '../../utils/db'
|
|
import { getClientIp } from '../../utils/ip'
|
|
import { getCurrentUserEmail } from '../../utils/user'
|
|
|
|
interface CreateProjectBody {
|
|
projectName: string
|
|
projectType?: string // SI / SM
|
|
clientName?: string
|
|
projectDescription?: string
|
|
startDate?: string
|
|
endDate?: string
|
|
contractAmount?: number
|
|
}
|
|
|
|
/**
|
|
* 프로젝트 코드 자동 생성 (년도-일련번호)
|
|
*/
|
|
async function generateProjectCode(): Promise<string> {
|
|
const year = new Date().getFullYear()
|
|
const prefix = `${year}-`
|
|
|
|
// 해당 연도의 마지막 코드 조회
|
|
const result = await query<{ project_code: string }>(`
|
|
SELECT project_code FROM wr_project_info
|
|
WHERE project_code LIKE $1
|
|
ORDER BY project_code DESC
|
|
LIMIT 1
|
|
`, [`${prefix}%`])
|
|
|
|
let nextNum = 1
|
|
if (result.length > 0 && result[0].project_code) {
|
|
const lastCode = result[0].project_code
|
|
const lastNum = parseInt(lastCode.split('-')[1]) || 0
|
|
nextNum = lastNum + 1
|
|
}
|
|
|
|
return `${prefix}${String(nextNum).padStart(3, '0')}`
|
|
}
|
|
|
|
/**
|
|
* 프로젝트 등록
|
|
* POST /api/project/create
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody<CreateProjectBody>(event)
|
|
const clientIp = getClientIp(event)
|
|
const userEmail = await getCurrentUserEmail(event)
|
|
|
|
if (!body.projectName) {
|
|
throw createError({ statusCode: 400, message: '프로젝트명을 입력해주세요.' })
|
|
}
|
|
|
|
// 프로젝트 유형 검증
|
|
const projectType = body.projectType || 'SI'
|
|
if (!['SI', 'SM'].includes(projectType)) {
|
|
throw createError({ statusCode: 400, message: '프로젝트 유형은 SI 또는 SM이어야 합니다.' })
|
|
}
|
|
|
|
// 프로젝트 코드 자동 생성
|
|
const projectCode = await generateProjectCode()
|
|
|
|
const project = await insertReturning(`
|
|
INSERT INTO wr_project_info (
|
|
project_code, project_name, project_type, client_name, project_description,
|
|
start_date, end_date, contract_amount,
|
|
created_ip, created_email, updated_ip, updated_email
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $9, $10)
|
|
RETURNING *
|
|
`, [
|
|
projectCode,
|
|
body.projectName,
|
|
projectType,
|
|
body.clientName || null,
|
|
body.projectDescription || null,
|
|
body.startDate || null,
|
|
body.endDate || null,
|
|
body.contractAmount || null,
|
|
clientIp,
|
|
userEmail
|
|
])
|
|
|
|
return {
|
|
success: true,
|
|
project: {
|
|
projectId: project.project_id,
|
|
projectCode: project.project_code,
|
|
projectName: project.project_name,
|
|
projectType: project.project_type
|
|
}
|
|
}
|
|
})
|