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

50 lines
1.7 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?limit=20
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 limit = parseInt(searchParams.get('limit') || '20', 10);
// Validate parameters
if (isNaN(limit) || limit < 1 || limit > 100) {
return NextResponse.json(
{ error: "Invalid limit parameter, must be between 1 and 100" },
{ status: 400 }
);
}
const registry = NotificationRegistry.getInstance();
const notifications = await registry.getNotifications(userId, limit);
// Add Cache-Control header - rely on server-side cache, minimal client cache
const response = NextResponse.json({
notifications,
total: notifications.length
});
response.headers.set('Cache-Control', 'private, max-age=0, must-revalidate');
return response;
} catch (error: any) {
logger.error('[NOTIFICATIONS_API] Error', {
error: error instanceof Error ? error.message : String(error),
});
return NextResponse.json(
{ error: "Internal server error", message: error.message },
{ status: 500 }
);
}
}