From 857ac320f1af1e07787093384d7934f153521c5f Mon Sep 17 00:00:00 2001 From: ectasy663 Date: Mon, 21 Sep 2026 12:15:13 +0530 Subject: [PATCH] Fix CBT session isolation and backend seed mock data exam pattern --- backend/seed.py | 10 +++++++--- src/app/cbt/[examId]/page.tsx | 4 ++-- src/store/cbtStore.ts | 14 ++++++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/backend/seed.py b/backend/seed.py index 8ea82ef..d9cd3f8 100644 --- a/backend/seed.py +++ b/backend/seed.py @@ -457,9 +457,12 @@ def seed_local_database(): subject = raw_questions[0].get("subject", "General") if raw_questions else "General" topic = raw_questions[0].get("topic", "Chapter 4") if raw_questions else "Chapter 4" - sheet_title = f"{subject} — {topic} DPS" + sheet_title = f"{subject} DPS" sheet_date = date.today() - timedelta(days=i) + is_neet = any(s in subject.lower() for s in ["botany", "zoology", "bio", "biology"]) + dps_pattern = models.ExamEnum.NEET if is_neet else models.ExamEnum.JEE_MAIN + # Create Question records from the JSON q_ids = [] for raw_q in raw_questions: @@ -475,7 +478,7 @@ def seed_local_database(): correct_answer=raw_q.get("correct_answer", {"value": "A"}), solution_text=raw_q.get("solution_text", "Refer to standard solution."), language=models.MediumEnum.ENGLISH, - exam=models.ExamEnum.JEE_MAIN, + exam=dps_pattern, created_by=admin_id, ) dps_session.add(q) @@ -499,7 +502,7 @@ def seed_local_database(): id=dps_id, title=sheet_title, type="DPS", - exam_pattern=models.ExamEnum.JEE_MAIN, + exam_pattern=dps_pattern, sections={"Practice": {"questions": q_ids, "time_limit": 30}}, total_time=30, difficulty="Medium", @@ -519,3 +522,4 @@ def seed_local_database(): if __name__ == "__main__": seed_local_database() + diff --git a/src/app/cbt/[examId]/page.tsx b/src/app/cbt/[examId]/page.tsx index 2f55268..a75190e 100644 --- a/src/app/cbt/[examId]/page.tsx +++ b/src/app/cbt/[examId]/page.tsx @@ -151,11 +151,11 @@ export default function CBTExamPage({ params }: { params: Promise<{ examId: stri }; }).filter(Boolean) as any[]; - initExam(examId, fetchedQuestions, testData.total_time * 60); + initExam(examId, fetchedQuestions, testData.total_time * 60, userId); setMounted(true); } catch (err) { console.error("Failed to load exam from network, falling back to dummy data:", err); - initExam(examId, DUMMY_QUESTIONS, 10800); + initExam(examId, DUMMY_QUESTIONS, 10800, userId); setMounted(true); } } diff --git a/src/store/cbtStore.ts b/src/store/cbtStore.ts index d8ee89e..e2362b2 100644 --- a/src/store/cbtStore.ts +++ b/src/store/cbtStore.ts @@ -46,6 +46,7 @@ export interface ExamAttemptPayload { } interface CBTState { + userId: string | null examId: string | null attemptId: string | null status: 'NOT_STARTED' | 'IN_PROGRESS' | 'SUBMITTED' | 'TIMED_OUT' @@ -56,7 +57,7 @@ interface CBTState { currentQuestionIndex: number // Actions - initExam: (examId: string, questions: any[], duration: number) => void + initExam: (examId: string, questions: any[], duration: number, userId: string) => void startExam: () => void selectAnswer: (questionId: string, answer: string) => void clearResponse: (questionId: string) => void @@ -72,6 +73,7 @@ interface CBTState { export const useCBTStore = create()( persist( (set, get) => ({ + userId: null, examId: null, attemptId: null, status: 'NOT_STARTED', @@ -81,12 +83,13 @@ export const useCBTStore = create()( answers: {}, currentQuestionIndex: 0, - initExam: (examId, questions, duration) => { + initExam: (examId, questions, duration, userId) => { const currentExamId = get().examId; + const currentUserId = get().userId; const currentQuestions = get().questions; - // Force reset if it's a new exam or the number of questions has changed - const needsReset = currentExamId !== examId || currentQuestions.length !== questions.length; + // Force reset if it's a new exam, new user, or the number of questions has changed + const needsReset = currentExamId !== examId || currentUserId !== userId || currentQuestions.length !== questions.length; if (needsReset) { const initialAnswers: Record = {}; @@ -100,6 +103,7 @@ export const useCBTStore = create()( }) set({ + userId, examId, attemptId: null, status: 'NOT_STARTED', @@ -271,6 +275,7 @@ export const useCBTStore = create()( resetExamState: () => { set({ + userId: null, examId: null, attemptId: null, status: 'NOT_STARTED', @@ -286,6 +291,7 @@ export const useCBTStore = create()( name: 'cbt-storage', // unique name storage: createJSONStorage(() => idbStorage), // use custom IndexedDB storage partialize: (state) => ({ + userId: state.userId, examId: state.examId, status: state.status, startTime: state.startTime,