// ═══════════════════════════════════════════════════════════════════════════ // SESSION API - Create/Join/End Sessions // ═══════════════════════════════════════════════════════════════════════════ import { NextRequest, NextResponse } from 'next/server'; import { sessionManager } from '@/lib/session-manager'; import { v4 as uuidv4 } from 'uuid'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // Create a new session export async function POST(request: NextRequest) { try { const body = await request.json(); const { action, teacherId, teacherName, sessionName, slideUrls, userId, userName, sessionId } = body; // CREATE SESSION if (action === 'create') { if (!teacherId || !teacherName || !sessionName) { return NextResponse.json({ success: false, error: 'Missing required fields' }, { status: 400 }); } const session = await sessionManager.createSession( teacherId, teacherName, sessionName, slideUrls || [] ); // Auto-join teacher to session sessionManager.joinSession(session.id, teacherId, teacherName, 'teacher'); return NextResponse.json({ success: true, data: session }); } // JOIN SESSION if (action === 'join') { if (!sessionId || !userId || !userName) { return NextResponse.json({ success: false, error: 'Missing required fields' }, { status: 400 }); } const session = sessionManager.getSession(sessionId); if (!session) { return NextResponse.json({ success: false, error: 'Session not found' }, { status: 404 }); } if (!session.isActive) { return NextResponse.json({ success: false, error: 'Session is not active' }, { status: 400 }); } const user = sessionManager.joinSession(sessionId, userId, userName, 'student'); return NextResponse.json({ success: true, data: { session, user, currentSlide: session.currentSlide, slideUrl: session.slideUrls[session.currentSlide] || null, whiteboardStrokes: sessionManager.getWhiteboardStrokes(sessionId), reactions: sessionManager.getReactions(sessionId) } }); } // END SESSION if (action === 'end') { if (!sessionId) { return NextResponse.json({ success: false, error: 'Missing sessionId' }, { status: 400 }); } await sessionManager.endSession(sessionId); return NextResponse.json({ success: true, message: 'Session ended' }); } // GET SESSION INFO if (action === 'get') { if (!sessionId) { return NextResponse.json({ success: false, error: 'Missing sessionId' }, { status: 400 }); } const session = sessionManager.getSession(sessionId); if (!session) { return NextResponse.json({ success: false, error: 'Session not found' }, { status: 404 }); } return NextResponse.json({ success: true, data: { session, currentSlide: session.currentSlide, slideUrl: session.slideUrls[session.currentSlide] || null, whiteboardStrokes: sessionManager.getWhiteboardStrokes(sessionId), reactions: sessionManager.getReactions(sessionId), doubts: sessionManager.getDoubts(sessionId), activePoll: sessionManager.getActivePoll(sessionId), onlineUsers: sessionManager.getOnlineUsers(sessionId) } }); } return NextResponse.json({ success: false, error: 'Invalid action' }, { status: 400 }); } catch (error) { console.error('Session API error:', error); return NextResponse.json({ success: false, error: 'Internal server error' }, { status: 500 }); } }