111 lines
3.4 KiB
TypeScript
111 lines
3.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { QuizJsonFormat } from '@/lib/types/database'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const supabase = await createClient()
|
|
|
|
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
|
if (authError || !user) {
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
|
|
}
|
|
|
|
const formData = await request.formData()
|
|
const file = formData.get('file') as File | null
|
|
const subchapterId = formData.get('subchapter_id') as string | null
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ error: 'Fichier manquant' }, { status: 400 })
|
|
}
|
|
|
|
const text = await file.text()
|
|
let quizData: QuizJsonFormat
|
|
|
|
try {
|
|
quizData = JSON.parse(text) as QuizJsonFormat
|
|
} catch {
|
|
return NextResponse.json({ error: 'Format JSON invalide' }, { status: 400 })
|
|
}
|
|
|
|
if (!quizData.title || !Array.isArray(quizData.questions) || quizData.questions.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Le fichier JSON doit contenir un titre et des questions' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Créer le quiz
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const { data: quiz, error: quizError } = await (supabase as any)
|
|
.from('quizzes')
|
|
.insert({
|
|
title: quizData.title,
|
|
subchapter_id: subchapterId ?? null,
|
|
author_id: user.id,
|
|
raw_json_data: quizData,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (quizError || !quiz) {
|
|
return NextResponse.json({ error: 'Erreur création du quiz', details: quizError?.message }, { status: 500 })
|
|
}
|
|
|
|
// Insérer les questions en batch
|
|
const questionsToInsert = quizData.questions.map((q, index) => ({
|
|
quiz_id: quiz.id,
|
|
question_text: q.question,
|
|
explanation: q.explanation ?? null,
|
|
order: index,
|
|
}))
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const { data: insertedQuestions, error: questionsError } = await (supabase as any)
|
|
.from('questions')
|
|
.insert(questionsToInsert)
|
|
.select()
|
|
|
|
if (questionsError || !insertedQuestions) {
|
|
return NextResponse.json(
|
|
{ error: 'Erreur insertion des questions', details: questionsError?.message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
// Insérer les réponses en batch
|
|
const answersToInsert = (insertedQuestions as { id: string }[]).flatMap((question, index) => {
|
|
const originalQuestion = quizData.questions[index]
|
|
return originalQuestion.answers.map((a) => ({
|
|
question_id: question.id,
|
|
answer_text: a.text,
|
|
is_correct: a.correct,
|
|
}))
|
|
})
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const { error: answersError } = await (supabase as any)
|
|
.from('answers')
|
|
.insert(answersToInsert)
|
|
|
|
if (answersError) {
|
|
return NextResponse.json(
|
|
{ error: 'Erreur insertion des réponses', details: answersError.message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
quiz: {
|
|
id: quiz.id,
|
|
title: quiz.title,
|
|
questionCount: insertedQuestions.length,
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('[upload-quiz]', error)
|
|
return NextResponse.json({ error: 'Erreur serveur interne' }, { status: 500 })
|
|
}
|
|
}
|