'use server'; import { createClient } from '@/utils/supabase/server'; import { revalidatePath } from 'next/cache'; export async function toggleCorrect(formData: FormData) { const supabase = createClient(); const answerId = Number(formData.get('answerId')); const newCorrectValue = formData.get('newCorrectValue') === 'true'; const { data: existingAnswer, error: fetchError } = await supabase .from('quiz_answers') .select('*') .eq('id', answerId) .single(); if (fetchError || !existingAnswer) { console.error('Error fetching existing answer:', fetchError); return; } const { question_number } = existingAnswer; let points = 0; if (question_number !== null) { if (question_number >= 1 && question_number <= 10) { if (newCorrectValue) points = 1; } else if (question_number >= 11 && question_number <= 19) { if (newCorrectValue) points = 2; } } const { error } = await supabase .from('quiz_answers') .update({ correct: newCorrectValue, points }) .eq('id', answerId); if (error) { console.error('Error updating correctness:', error); return; } revalidatePath('/quiz/answers'); } export async function updatePoints(formData: FormData) { const supabase = createClient(); // Get the form data with validation const answerId = Number(formData.get('answerId')); if (isNaN(answerId) || answerId <= 0) { console.error('Invalid answer ID'); return { error: 'Invalid answer ID' }; } let customPoints = Number(formData.get('customPoints')); if (isNaN(customPoints)) { customPoints = 0; } // Ensure points are non-negative customPoints = Math.max(0, customPoints); try { // First check if the answer exists const { data: existingAnswer, error: fetchError } = await supabase .from('quiz_answers') .select('*') .eq('id', answerId) .single(); if (fetchError) { console.error('Error fetching existing answer:', fetchError); return { error: fetchError.message }; } if (!existingAnswer) { console.error('Answer not found'); return { error: 'Answer not found' }; } // Determine correct status based on question number let newCorrectValue = existingAnswer.correct; if ( existingAnswer.question_number !== null && existingAnswer.question_number >= 20 && existingAnswer.question_number <= 35 ) { newCorrectValue = customPoints > 0; // Mark as correct if points are given } // Update the answer const { error: updateError } = await supabase .from('quiz_answers') .update({ correct: newCorrectValue, points: customPoints, }) .eq('id', answerId); if (updateError) { console.error('Error updating points:', updateError); return { error: updateError.message }; } // Revalidate paths to refresh data revalidatePath('/quiz/answers'); revalidatePath('/admin/quiz'); return { success: true, points: customPoints }; } catch (error) { console.error('Unexpected error updating points:', error); return { error: 'An unexpected error occurred' }; } } export async function deleteAllQuizAnswers() { 'use server'; const supabase = createClient(); const { error } = await supabase .from('quiz_answers') .delete() .neq('id', 0); // Delete all rows if (error) { console.error('Error deleting quiz answers:', error); throw new Error('Failed to delete quiz answers'); } revalidatePath('/quiz/answers'); }