NeahStable/app/api/notifications/count/route.ts
2026-01-11 22:22:53 +01:00

42 lines
1.5 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 { searchParams } = new URL(request.url);
const forceRefresh = searchParams.get('force') === 'true';
const notificationService = NotificationService.getInstance();
// If force refresh, invalidate cache first
if (forceRefresh) {
await notificationService.invalidateCache(userId);
}
const counts = await notificationService.getNotificationCount(userId);
// Add Cache-Control header - rely on server-side cache, minimal client cache
const response = NextResponse.json(counts);
response.headers.set('Cache-Control', 'private, max-age=0, must-revalidate'); // No client cache, always revalidate
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 }
);
}
}