@@ -214,36 +214,36 @@ async function startQuiz() {
214214 if ( user ) {
215215 const email = user . email ;
216216
217- // Reference to the specific quiz results
218- const userQuizRef = doc ( db , "user_assessment_results" , email , "assessment_results" , assessmentKey ) ;
217+ // Reference to the user's assessment results
218+ const userResultsRef = doc ( db , "user_assessment_results" , email ) ;
219219
220- // Prepare the new attempt data
220+ // New attempt data
221221 const newAttempt = {
222- score : {
223- score : score ,
224- timestamp : new Date ( ) ,
225- } ,
222+ quizCode : assessmentKey , // Quiz identifier
223+ score : score ,
224+ percentage : percentage ,
225+ timestamp : new Date ( ) ,
226226 user_questions_with_answers : currentSubject . questions . map ( ( q , index ) => {
227227 const selectedAnswerIndex = selectedAnswers [ index ] ;
228228 const selectedAnswerText = selectedAnswerIndex !== undefined ? q . answers [ selectedAnswerIndex ] : null ;
229229 return {
230230 question : q . question ,
231- answer : selectedAnswerText , // Save the full text of the selected answer
231+ answer : selectedAnswerText , // Save full text of the selected answer
232232 } ;
233233 } ) ,
234234 } ;
235235
236- // Check if the document for this quiz already exists
237- const existingDoc = await getDoc ( userQuizRef ) ;
236+ // Fetch existing results
237+ const existingDoc = await getDoc ( userResultsRef ) ;
238238
239239 if ( existingDoc . exists ( ) ) {
240- // Update the attempts array
240+ // Append new attempt to the existing results array
241241 const existingData = existingDoc . data ( ) ;
242- const updatedAttempts = [ ...( existingData . attempts || [ ] ) , newAttempt ] ;
243- await setDoc ( userQuizRef , { attempts : updatedAttempts } ) ;
242+ const updatedResults = [ ...( existingData . results || [ ] ) , newAttempt ] ;
243+ await setDoc ( userResultsRef , { results : updatedResults } , { merge : true } ) ;
244244 } else {
245245 // Create a new document with the first attempt
246- await setDoc ( userQuizRef , { attempts : [ newAttempt ] } ) ;
246+ await setDoc ( userResultsRef , { results : [ newAttempt ] } ) ;
247247 }
248248
249249 console . log ( "Quiz attempt saved successfully." ) ;
@@ -256,6 +256,7 @@ async function startQuiz() {
256256 }
257257
258258
259+
259260
260261 function startTimer ( ) {
261262 timerInterval = setInterval ( ( ) => {
0 commit comments