31 lines
855 B
TypeScript
31 lines
855 B
TypeScript
|
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
||
|
|
import { prisma } from '@/lib/prisma'
|
||
|
|
import { requireAuth } from '@/lib/auth'
|
||
|
|
|
||
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||
|
|
const user = await requireAuth(req, res)
|
||
|
|
if (!user) return
|
||
|
|
|
||
|
|
if (req.method === 'GET') {
|
||
|
|
const [data, unread] = await Promise.all([
|
||
|
|
prisma.notification.findMany({
|
||
|
|
where: { userId: user.id },
|
||
|
|
orderBy: { createdAt: 'desc' },
|
||
|
|
take: 20,
|
||
|
|
}),
|
||
|
|
prisma.notification.count({ where: { userId: user.id, read: false } }),
|
||
|
|
])
|
||
|
|
return res.json({ data, unread })
|
||
|
|
}
|
||
|
|
|
||
|
|
if (req.method === 'PATCH') {
|
||
|
|
await prisma.notification.updateMany({
|
||
|
|
where: { userId: user.id, read: false },
|
||
|
|
data: { read: true },
|
||
|
|
})
|
||
|
|
return res.json({ ok: true })
|
||
|
|
}
|
||
|
|
|
||
|
|
res.status(405).end()
|
||
|
|
}
|