Spaces:
Configuration error
Configuration error
| import { NextRequest, NextResponse } from 'next/server' | |
| import prisma from '@/lib/prisma' | |
| import { getAuthUser } from '@/lib/auth' | |
| // GET /api/notifications — get current user's notifications | |
| export async function GET(request: NextRequest) { | |
| try { | |
| const authUser = await getAuthUser() | |
| if (!authUser) { | |
| return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) | |
| } | |
| const { searchParams } = new URL(request.url) | |
| const unreadOnly = searchParams.get('unread') === 'true' | |
| const limit = Math.min(parseInt(searchParams.get('limit') || '20'), 50) | |
| const where: Record<string, unknown> = { userId: authUser.id } | |
| if (unreadOnly) { | |
| where.isRead = false | |
| } | |
| const [notifications, unreadCount] = await Promise.all([ | |
| prisma.notification.findMany({ | |
| where, | |
| orderBy: { createdAt: 'desc' }, | |
| take: limit, | |
| }), | |
| prisma.notification.count({ | |
| where: { userId: authUser.id, isRead: false }, | |
| }), | |
| ]) | |
| return NextResponse.json({ notifications, unreadCount }) | |
| } catch (error) { | |
| console.error('Error fetching notifications:', error) | |
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | |
| } | |
| } | |
| // PATCH /api/notifications — mark notifications as read | |
| export async function PATCH(request: NextRequest) { | |
| try { | |
| const authUser = await getAuthUser() | |
| if (!authUser) { | |
| return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) | |
| } | |
| const body = await request.json() | |
| const { notificationIds, markAllRead } = body | |
| if (markAllRead) { | |
| await prisma.notification.updateMany({ | |
| where: { userId: authUser.id, isRead: false }, | |
| data: { isRead: true }, | |
| }) | |
| } else if (notificationIds && Array.isArray(notificationIds)) { | |
| await prisma.notification.updateMany({ | |
| where: { | |
| id: { in: notificationIds }, | |
| userId: authUser.id, | |
| }, | |
| data: { isRead: true }, | |
| }) | |
| } | |
| return NextResponse.json({ success: true }) | |
| } catch (error) { | |
| console.error('Error updating notifications:', error) | |
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | |
| } | |
| } | |