NeahNew/app/api/notifications/count/route.ts
2025-05-05 13:04:01 +02:00

33 lines
1.2 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options";
import { NotificationService } from '@/lib/services/notifications/notification-service';
// GET /api/notifications/count
export async function GET(request: Request) {
try {
// Authenticate user
const session = await getServerSession(authOptions);
if (!session || !session.user?.id) {
return NextResponse.json(
{ error: "Not authenticated" },
{ status: 401 }
);
}
const userId = session.user.id;
const notificationService = NotificationService.getInstance();
const counts = await notificationService.getNotificationCount(userId);
// Add Cache-Control header to help with client-side caching
const response = NextResponse.json(counts);
response.headers.set('Cache-Control', 'private, max-age=10'); // Cache for 10 seconds on client
return response;
} catch (error: any) {
console.error('Error in notification count API:', error);
return NextResponse.json(
{ error: "Internal server error", message: error.message },
{ status: 500 }
);
}
}