Files
weeklyreport/backend/api/admin/role/[id]/update.put.ts

55 lines
1.4 KiB
TypeScript

import { queryOne, execute } from '../../../../utils/db'
import { requireAdmin } from '../../../../utils/session'
/**
* 권한 수정
* PUT /api/admin/role/[id]/update
*/
export default defineEventHandler(async (event) => {
await requireAdmin(event)
const roleId = getRouterParam(event, 'id')
if (!roleId) {
throw createError({ statusCode: 400, message: '권한 ID가 필요합니다.' })
}
const body = await readBody<{
roleName?: string
roleDescription?: string
isInternalIpOnly?: boolean
sortOrder?: number
isActive?: boolean
}>(event)
// 존재 여부 확인
const existing = await queryOne<any>(`
SELECT role_id, role_code FROM wr_role WHERE role_id = $1
`, [roleId])
if (!existing) {
throw createError({ statusCode: 404, message: '권한을 찾을 수 없습니다.' })
}
await execute(`
UPDATE wr_role SET
role_name = COALESCE($2, role_name),
role_description = COALESCE($3, role_description),
is_internal_ip_only = COALESCE($4, is_internal_ip_only),
sort_order = COALESCE($5, sort_order),
is_active = COALESCE($6, is_active),
updated_at = NOW()
WHERE role_id = $1
`, [
roleId,
body.roleName,
body.roleDescription,
body.isInternalIpOnly,
body.sortOrder,
body.isActive
])
const updated = await queryOne<any>(`SELECT * FROM wr_role WHERE role_id = $1`, [roleId])
return { success: true, role: updated }
})