Skip to content

bug: Quiz score is not persisted to the database after submission β€” refreshing the results page loses all score data and breaks the analytics dashboardΒ #455

Description

@divyanshim27

πŸ› Problem Statement

VidyaSetu supports Practice, Test, and Revision quiz modes. After completing a quiz and viewing the results screen, refreshing the page causes the score to be lost entirely. Based on the documented tech stack (Next.js + Prisma + PostgreSQL) and the src/ structure, quiz results are held only in React component state post-submission rather than being written to the database immediately on completion.

This directly breaks the Student Dashboard & Analytics Views feature β€” analytics cannot reflect historical quiz performance if scores are never persisted.

Current Behavior

  1. Student completes a 10-question quiz in Test mode.
  2. Results page renders with score (e.g., 8/10, 80%).
  3. Student refreshes the page β†’ score is gone.
  4. Student's dashboard shows no record of the completed quiz.

Expected Behavior

  • On quiz completion, the score, time taken, quiz mode, chapter ID, and per-question responses must be written to the database via a Prisma mutation.
  • The results page should load data from the database (not component state) so it survives refresh.
  • The dashboard analytics view should reflect this persisted attempt history.

Proposed Fix

1. Create a QuizAttempt model in prisma/schema.prisma:

model QuizAttempt {
  id          String   @id @default(cuid())
  userId      String
  chapterId   String
  quizMode    String   // "practice" | "test" | "revision"
  score       Int
  totalQ      Int
  timeTakenMs Int
  completedAt DateTime @default(now())

  user    User    @relation(fields: [userId], references: [id], onDelete: Cascade)
  chapter Chapter @relation(fields: [chapterId], references: [id])
}

2. Add API route POST /api/quiz/submit in src/app/api/quiz/submit/route.ts:

import { prisma } from "@/lib/prisma";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";

export async function POST(req: Request) {
  const session = await getServerSession(authOptions);
  if (!session?.user?.id) return new Response("Unauthorized", { status: 401 });

  const { chapterId, quizMode, score, totalQ, timeTakenMs } = await req.json();

  const attempt = await prisma.quizAttempt.create({
    data: {
      userId: session.user.id,
      chapterId,
      quizMode,
      score,
      totalQ,
      timeTakenMs,
    },
  });

  return Response.json({ attemptId: attempt.id });
}

3. In the quiz submission handler (frontend), call this API before navigating to results.

4. Update the results page to accept attemptId as a query param and fetch from DB.

Files to Modify / Create

File Change
prisma/schema.prisma Add QuizAttempt model
prisma/migrations/ Generate migration
src/app/api/quiz/submit/route.ts New POST endpoint
src/ (quiz submission handler) Call API on completion
src/ (results page) Load from DB via attemptId

πŸ“Š Impact

Without score persistence, every quiz taken is effectively thrown away. The analytics dashboard becomes meaningless, and students cannot track their chapter-wise performance over time β€” which is VidyaSetu's core differentiation over static NCERT materials.

Suggested labels: bug, backend, database, gssoc:level2

I would like to work on this. Could you please assign it to me?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions