/** * Debug API to check session links */ import { NextResponse } from 'next/server'; import { getAdminAuth, getAdminDb } from '@/lib/firebase/admin'; export async function GET(request: Request) { try { const authHeader = request.headers.get('Authorization'); if (!authHeader?.startsWith('Bearer ')) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const idToken = authHeader.split('Bearer ')[1]; const adminAuth = getAdminAuth(); const adminDb = getAdminDb(); let userId: string; try { const decodedToken = await adminAuth.verifyIdToken(idToken); userId = decodedToken.uid; } catch (error) { return NextResponse.json({ error: 'Invalid token' }, { status: 401 }); } // Get all user's sessions const sessionsSnapshot = await adminDb .collection('sessions') .where('userId', '==', userId) .get(); const linked: any[] = []; const unlinked: any[] = []; sessionsSnapshot.docs.forEach(doc => { const data = doc.data(); const sessionInfo = { id: doc.id, workspaceName: data.workspaceName || 'Unknown', workspacePath: data.workspacePath, projectId: data.projectId, needsProjectAssociation: data.needsProjectAssociation, createdAt: data.createdAt?.toDate?.() || data.createdAt, }; if (data.projectId) { linked.push(sessionInfo); } else { unlinked.push(sessionInfo); } }); // Get all user's projects const projectsSnapshot = await adminDb .collection('projects') .where('userId', '==', userId) .get(); const projects = projectsSnapshot.docs.map(doc => ({ id: doc.id, name: doc.data().productName || doc.data().name, workspacePath: doc.data().workspacePath, })); return NextResponse.json({ summary: { totalSessions: sessionsSnapshot.size, linkedSessions: linked.length, unlinkedSessions: unlinked.length, totalProjects: projects.length, }, linked, unlinked, projects, }); } catch (error) { console.error('Debug check error:', error); return NextResponse.json( { error: 'Failed to check links', details: error instanceof Error ? error.message : String(error), }, { status: 500 } ); } }