69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import { queryOne, execute } from '../../../utils/db'
|
|
import { getCurrentUserId } from '../../../utils/user'
|
|
|
|
interface UpdateBusinessBody {
|
|
businessName: string
|
|
businessCode?: string
|
|
clientName?: string
|
|
contractStartDate?: string
|
|
contractEndDate?: string
|
|
businessStatus?: string
|
|
description?: string
|
|
}
|
|
|
|
/**
|
|
* 사업 수정
|
|
* PUT /api/business/[id]/update
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const businessId = Number(getRouterParam(event, 'id'))
|
|
const body = await readBody<UpdateBusinessBody>(event)
|
|
const userId = await getCurrentUserId(event)
|
|
|
|
if (!businessId) {
|
|
throw createError({ statusCode: 400, message: '사업 ID가 필요합니다.' })
|
|
}
|
|
|
|
if (!body.businessName) {
|
|
throw createError({ statusCode: 400, message: '사업명은 필수입니다.' })
|
|
}
|
|
|
|
const existing = await queryOne(`
|
|
SELECT business_id FROM wr_business WHERE business_id = $1
|
|
`, [businessId])
|
|
|
|
if (!existing) {
|
|
throw createError({ statusCode: 404, message: '사업을 찾을 수 없습니다.' })
|
|
}
|
|
|
|
await execute(`
|
|
UPDATE wr_business SET
|
|
business_name = $1,
|
|
business_code = $2,
|
|
client_name = $3,
|
|
contract_start_date = $4,
|
|
contract_end_date = $5,
|
|
business_status = $6,
|
|
description = $7,
|
|
updated_at = NOW(),
|
|
updated_by = $8
|
|
WHERE business_id = $9
|
|
`, [
|
|
body.businessName,
|
|
body.businessCode || null,
|
|
body.clientName || null,
|
|
body.contractStartDate || null,
|
|
body.contractEndDate || null,
|
|
body.businessStatus || 'active',
|
|
body.description || null,
|
|
userId,
|
|
businessId
|
|
])
|
|
|
|
return {
|
|
success: true,
|
|
businessId,
|
|
message: '사업이 수정되었습니다.'
|
|
}
|
|
})
|