2026-07-23 11:00:05 -04:00
|
|
|
import { users } from '../../database/schema'
|
|
|
|
|
import { eq } from 'drizzle-orm'
|
2026-07-24 04:55:39 -04:00
|
|
|
import { getTokenFromEvent, verifyToken } from '../../utils/auth'
|
2026-07-23 11:00:05 -04:00
|
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
|
const db = useDb()
|
|
|
|
|
const body = await readBody(event)
|
|
|
|
|
const token = getTokenFromEvent(event)
|
|
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
|
throw createError({ statusCode: 401, message: 'Chưa đăng nhập' })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payload = verifyToken(token)
|
|
|
|
|
if (!payload) {
|
|
|
|
|
throw createError({ statusCode: 401, message: 'Token không hợp lệ' })
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-24 04:55:39 -04:00
|
|
|
const updates: Record<string, any> = {}
|
2026-07-23 11:00:05 -04:00
|
|
|
|
2026-07-24 04:55:39 -04:00
|
|
|
const name = typeof body.name === 'string' ? body.name.trim() : ''
|
|
|
|
|
if (name) {
|
|
|
|
|
if (name.length > 50) {
|
|
|
|
|
throw createError({ statusCode: 400, message: 'Tên hiển thị tối đa 50 ký tự' })
|
|
|
|
|
}
|
|
|
|
|
updates.name = name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (['male', 'female', 'other', ''].includes(body.gender)) {
|
|
|
|
|
updates.gender = body.gender || null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Object.keys(updates).length) {
|
|
|
|
|
throw createError({ statusCode: 400, message: 'Không có dữ liệu cập nhật' })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.update(users).set(updates).where(eq(users.id, payload.id))
|
|
|
|
|
|
|
|
|
|
return { message: 'Cập nhật thành công!' }
|
2026-07-23 11:00:05 -04:00
|
|
|
})
|