30 lines
1001 B
JavaScript
30 lines
1001 B
JavaScript
import { Router } from 'express'
|
|
import { prisma } from '../db.js'
|
|
import { authRequired } from '../middleware.js'
|
|
|
|
const router = Router()
|
|
router.use(authRequired)
|
|
|
|
// 报警列表: 按时间最新优先, cursor 分页 (cursor = id)
|
|
router.get('/alerts', async (req, res, next) => {
|
|
try {
|
|
const rawLimit = Number(req.query.limit)
|
|
const limit = Number.isFinite(rawLimit) && rawLimit >= 1 ? Math.min(Math.floor(rawLimit), 50) : 10
|
|
const rawCursor = Number(req.query.cursor)
|
|
const cursor = Number.isInteger(rawCursor) && rawCursor > 0 ? rawCursor : null
|
|
const where = cursor ? { id: { lt: cursor } } : {}
|
|
const [items, total] = await Promise.all([
|
|
prisma.alert.findMany({
|
|
where,
|
|
orderBy: { id: 'desc' },
|
|
take: limit,
|
|
}),
|
|
prisma.alert.count(),
|
|
])
|
|
const nextCursor = items.length === limit ? String(items[items.length - 1].id) : null
|
|
res.json({ items, nextCursor, total })
|
|
} catch (e) { next(e) }
|
|
})
|
|
|
|
export default router
|