20 lines
1.1 KiB
JavaScript
20 lines
1.1 KiB
JavaScript
import { getDb } from '../utils/db.js'
|
|
import { requireAuth } from '../utils/authHelpers.js'
|
|
import { validateDeviceBody, rowToDevice, sanitizeDeviceForResponse } from '../utils/deviceUtils.js'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
requireAuth(event, { role: 'adminOrLeader' })
|
|
const body = await readBody(event).catch(() => ({}))
|
|
const { name, device_type, vendor, lat, lng, stream_url, source_type, config } = validateDeviceBody(body)
|
|
const id = crypto.randomUUID()
|
|
const { run, get } = await getDb()
|
|
await run(
|
|
'INSERT INTO devices (id, name, device_type, vendor, lat, lng, stream_url, source_type, config) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
[id, name, device_type, vendor, lat, lng, stream_url, source_type, config],
|
|
)
|
|
const row = await get('SELECT id, name, device_type, vendor, lat, lng, stream_url, source_type, config FROM devices WHERE id = ?', [id])
|
|
const device = rowToDevice(row)
|
|
if (!device) throw createError({ statusCode: 500, message: 'Device not found after insert' })
|
|
return sanitizeDeviceForResponse(device)
|
|
})
|