|
| 1 | +import { createClient } from '@/lib/supabase/server'; |
| 2 | +import { successResponse, errorResponse, handleApiError } from '@/lib/api'; |
| 3 | + |
| 4 | +interface RouteParams { |
| 5 | + params: Promise<{ sessionId: string }>; |
| 6 | +} |
| 7 | + |
| 8 | +// Types for database rows (not yet in generated Supabase types) |
| 9 | +interface ParticipantRow { |
| 10 | + id: string; |
| 11 | + display_name: string; |
| 12 | + role: string; |
| 13 | + control_state: string; |
| 14 | + joined_at: string; |
| 15 | + left_at: string | null; |
| 16 | + session_id: string; |
| 17 | +} |
| 18 | + |
| 19 | +interface SessionRow { |
| 20 | + id: string; |
| 21 | + join_code: string; |
| 22 | + status: string; |
| 23 | + settings: Record<string, unknown>; |
| 24 | + created_at: string; |
| 25 | + session_participants: ParticipantRow[]; |
| 26 | +} |
| 27 | + |
| 28 | +// GET /api/sessions/[sessionId]/guest?p=participantId - Get session for guest viewer |
| 29 | +export async function GET(request: Request, { params }: RouteParams) { |
| 30 | + try { |
| 31 | + const { sessionId } = await params; |
| 32 | + const url = new URL(request.url); |
| 33 | + const participantId = url.searchParams.get('p'); |
| 34 | + |
| 35 | + if (!participantId) { |
| 36 | + return errorResponse('Participant ID required', 400); |
| 37 | + } |
| 38 | + |
| 39 | + const supabase = await createClient(); |
| 40 | + |
| 41 | + // Validate participant exists and belongs to this session |
| 42 | + const { data: participant, error: participantError } = (await supabase |
| 43 | + .from('session_participants') |
| 44 | + .select('id, display_name, role, control_state, joined_at, left_at, session_id') |
| 45 | + .eq('id', participantId) |
| 46 | + .eq('session_id', sessionId) |
| 47 | + .single()) as { data: ParticipantRow | null; error: unknown }; |
| 48 | + |
| 49 | + if (participantError || !participant) { |
| 50 | + return errorResponse('Invalid participant or session', 403); |
| 51 | + } |
| 52 | + |
| 53 | + // Check participant hasn't left |
| 54 | + if (participant.left_at) { |
| 55 | + return errorResponse('You have left this session', 403); |
| 56 | + } |
| 57 | + |
| 58 | + // Get session details |
| 59 | + const { data: session, error: sessionError } = (await supabase |
| 60 | + .from('sessions') |
| 61 | + .select( |
| 62 | + ` |
| 63 | + id, |
| 64 | + join_code, |
| 65 | + status, |
| 66 | + settings, |
| 67 | + created_at, |
| 68 | + session_participants ( |
| 69 | + id, |
| 70 | + display_name, |
| 71 | + role, |
| 72 | + control_state, |
| 73 | + joined_at, |
| 74 | + left_at |
| 75 | + ) |
| 76 | + ` |
| 77 | + ) |
| 78 | + .eq('id', sessionId) |
| 79 | + .neq('status', 'ended') |
| 80 | + .single()) as { data: SessionRow | null; error: unknown }; |
| 81 | + |
| 82 | + if (sessionError || !session) { |
| 83 | + return errorResponse('Session not found or has ended', 404); |
| 84 | + } |
| 85 | + |
| 86 | + // Filter to only active participants |
| 87 | + const activeParticipants = session.session_participants.filter((p) => !p.left_at); |
| 88 | + |
| 89 | + return successResponse({ |
| 90 | + session: { |
| 91 | + id: session.id, |
| 92 | + join_code: session.join_code, |
| 93 | + status: session.status, |
| 94 | + settings: session.settings, |
| 95 | + created_at: session.created_at, |
| 96 | + session_participants: activeParticipants, |
| 97 | + }, |
| 98 | + participant: { |
| 99 | + id: participant.id, |
| 100 | + display_name: participant.display_name, |
| 101 | + role: participant.role, |
| 102 | + control_state: participant.control_state, |
| 103 | + joined_at: participant.joined_at, |
| 104 | + }, |
| 105 | + }); |
| 106 | + } catch (error) { |
| 107 | + return handleApiError(error); |
| 108 | + } |
| 109 | +} |
0 commit comments