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/debug/notifications 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; console.log(`[DEBUG] Testing notifications for user ${userId}`); // Get environment variables status const envStatus = { LEANTIME_API_URL: process.env.LEANTIME_API_URL ? 'Set' : 'Not set', LEANTIME_TOKEN: process.env.LEANTIME_TOKEN ? `Set (length: ${process.env.LEANTIME_TOKEN.length})` : 'Not set', LEANTIME_API_KEY: process.env.LEANTIME_API_KEY ? `Set (length: ${process.env.LEANTIME_API_KEY.length})` : 'Not set', KEYCLOAK_BASE_URL: process.env.KEYCLOAK_BASE_URL ? 'Set' : 'Not set', KEYCLOAK_REALM: process.env.KEYCLOAK_REALM || 'Not set', KEYCLOAK_ADMIN_CLIENT_ID: process.env.KEYCLOAK_ADMIN_CLIENT_ID ? 'Set' : 'Not set', KEYCLOAK_ADMIN_CLIENT_SECRET: process.env.KEYCLOAK_ADMIN_CLIENT_SECRET ? 'Set (masked)' : 'Not set', }; // Get user information console.log(`[DEBUG] Getting user info for ${userId}`); let userInfo = { id: userId, email: session.user.email || 'Unknown' }; // Test notification service console.log(`[DEBUG] Testing notification service`); const notificationService = NotificationService.getInstance(); // Get notification count console.log(`[DEBUG] Getting notification count`); const startTimeCount = Date.now(); const notificationCount = await notificationService.getNotificationCount(userId); const timeForCount = Date.now() - startTimeCount; // Get notifications console.log(`[DEBUG] Getting notifications`); const startTimeNotifications = Date.now(); const notifications = await notificationService.getNotifications(userId, 1, 10); const timeForNotifications = Date.now() - startTimeNotifications; return NextResponse.json({ success: true, timestamp: new Date().toISOString(), userInfo, environmentVariables: envStatus, notificationServiceTest: { count: { result: notificationCount, timeMs: timeForCount }, notifications: { count: notifications.length, timeMs: timeForNotifications, samples: notifications.slice(0, 3) // Only return first 3 notifications as samples } } }); } catch (error: any) { console.error('[DEBUG] Error in debug notifications API:', error); return NextResponse.json( { error: "Internal server error", message: error.message, stack: process.env.NODE_ENV === 'development' ? error.stack : undefined }, { status: 500 } ); } }