NeahStable/app/api/notifications/count/route.ts
2026-01-16 00:12:15 +01:00

47 lines
1.6 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options";
import { NotificationRegistry } from '@/lib/services/notifications/notification-registry';
import { logger } from '@/lib/logger';
// 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 registry = NotificationRegistry.getInstance();
// If force refresh, invalidate cache first
if (forceRefresh) {
await registry.invalidateCache(userId);
logger.debug('[NOTIFICATIONS_COUNT_API] Cache invalidated for force refresh', { userId });
}
// Get aggregated count from registry
const counts = await registry.getCount(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');
return response;
} catch (error: any) {
logger.error('[NOTIFICATIONS_COUNT_API] Error', {
error: error instanceof Error ? error.message : String(error),
});
return NextResponse.json(
{ error: "Internal server error", message: error.message },
{ status: 500 }
);
}
}