68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const supabase = await createClient()
|
|
const db = supabase as any
|
|
|
|
const body = await request.json()
|
|
const { short_code, first_name, last_name } = body
|
|
|
|
if (!short_code || !first_name || !last_name) {
|
|
return NextResponse.json(
|
|
{ error: 'Code session, prénom et nom sont requis' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data: session, error: sessionError } = await db
|
|
.from('sessions')
|
|
.select('id, is_active, quiz_id')
|
|
.eq('short_code', short_code.toUpperCase())
|
|
.single()
|
|
|
|
if (sessionError || !session) {
|
|
return NextResponse.json({ error: 'Session introuvable' }, { status: 404 })
|
|
}
|
|
|
|
if (!session.is_active) {
|
|
return NextResponse.json(
|
|
{ error: 'Ce quiz est terminé', code: 'SESSION_INACTIVE' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
const { data: participation, error: participationError } = await db
|
|
.from('student_participations')
|
|
.insert({
|
|
session_id: session.id,
|
|
first_name: first_name.trim(),
|
|
last_name: last_name.trim(),
|
|
status: 'in_progress',
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (participationError || !participation) {
|
|
return NextResponse.json(
|
|
{ error: 'Erreur lors de la création de la participation', details: participationError?.message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
participation: {
|
|
id: participation.id,
|
|
session_id: participation.session_id,
|
|
quiz_id: session.quiz_id,
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('[student/join]', error)
|
|
return NextResponse.json({ error: 'Erreur serveur interne' }, { status: 500 })
|
|
}
|
|
}
|