Skip to content

Commit 5fc8c55

Browse files
committed
fix: Resolve quiz not found error after generation
1 parent 7382b8c commit 5fc8c55

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

app/actions/exercise-helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const tryGenerateAndCacheExercise = async (
9696
});
9797
}
9898
console.log(`[API:tryGenerate] Generated and cached exercise ID: ${exerciseId}`);
99+
99100
return success<{ content: ExerciseContent; id: number }, ActionError>({
100101
content: generatedExercise,
101102
id: exerciseId,

lib/repositories/quizRepository.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ const mockDb = db as unknown as {
2828

2929
// Sample data for testing
3030
const mockQuizContent = {
31-
passage: 'Test passage',
32-
question: 'Test question?',
33-
options: { A: 'Option A', B: 'Option B', C: 'Option C', D: 'Option D' },
34-
answer: 'A',
35-
explanation: 'Test explanation',
31+
paragraph: 'Questo è il testo del paragrafo.',
32+
question: 'Qual è la domanda?',
33+
options: { A: 'Opzione A', B: 'Opzione B', C: 'Opzione C', D: 'Opzione D' },
34+
correctAnswer: 'B',
35+
allExplanations: {
36+
A: 'Spiegazione A',
37+
B: 'Spiegazione B',
38+
C: 'Spiegazione C',
39+
D: 'Spiegazione D',
40+
},
41+
topic: 'Test Topic',
42+
relevantText: 'Testo rilevante.',
3643
};
3744
const mockQuizContentString = JSON.stringify(mockQuizContent);
3845

lib/repositories/quizRepository.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ import { z } from 'zod';
22
import db from '@/lib/db'; // Import db
33

44
// Schema for the data structure stored in the quiz table's content column
5-
// This should align with PartialQuizData or QuizData from domain schemas
5+
// This should align with the actual structure generated by AI and expected by QuizDataSchema
66
export const QuizContentSchema = z.object({
7-
passage: z.string(),
7+
paragraph: z.string(), // Changed from passage
8+
topic: z.string().optional().nullable(), // Added optional topic
89
question: z.string(),
9-
options: z.object({ A: z.string(), B: z.string(), C: z.string(), D: z.string() }),
10-
answer: z.string(), // The correct option key (e.g., 'A')
11-
explanation: z.string(),
10+
options: z.object({
11+
A: z.string(),
12+
B: z.string(),
13+
C: z.string(),
14+
D: z.string(),
15+
}),
16+
correctAnswer: z.string(), // Changed from answer
17+
allExplanations: z.object({
18+
// Changed from explanation: string
19+
A: z.string(),
20+
B: z.string(),
21+
C: z.string(),
22+
D: z.string(),
23+
}),
24+
relevantText: z.string().optional().nullable(), // Added optional relevantText
1225
});
1326

1427
// Schema for the raw row returned by the database
@@ -33,9 +46,12 @@ export type Quiz = z.infer<typeof QuizSchema>;
3346

3447
// Finds a quiz by its ID
3548
export const findQuizById = (id: number): Quiz | null => {
49+
// console.log(`[QuizRepository:findQuizById] Attempting to find quiz with ID: ${id}`);
3650
try {
3751
const row = db.prepare('SELECT * FROM quiz WHERE id = ?').get(id) as QuizRow | undefined;
52+
// console.log('[QuizRepository:findQuizById] Raw row from DB:', row);
3853
if (!row) {
54+
// console.log(`[QuizRepository:findQuizById] No row found for ID: ${id}`);
3955
return null;
4056
}
4157
// Validate the base row structure

0 commit comments

Comments
 (0)