update flash card, test
This commit is contained in:
@@ -1,22 +1,47 @@
|
||||
import { supabase } from '@/lib/supabase'
|
||||
|
||||
const ANSWER_VALUES = ['A', 'B', 'C', 'D'] as const
|
||||
|
||||
interface TestResultData {
|
||||
partId: number
|
||||
partName: string
|
||||
testId: number | null
|
||||
selectedParts: number[]
|
||||
score: number
|
||||
total: number
|
||||
timeUsed: number
|
||||
answers: { questionId: string; selected: number | null; correct: boolean }[]
|
||||
answers: { questionId: number; selected: number | null; correct: boolean }[]
|
||||
}
|
||||
|
||||
/** Fire-and-forget: save test result. Failures are logged but don't block UI. */
|
||||
export async function saveTestResult(userId: string, data: TestResultData): Promise<void> {
|
||||
const { error } = await supabase.from('user_progress').insert({
|
||||
user_id: userId,
|
||||
type: 'test',
|
||||
data,
|
||||
})
|
||||
if (error) console.error('Failed to save test result:', error.message)
|
||||
const { data: attempt, error: attemptError } = await supabase
|
||||
.from('user_test_attempt')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
test_id: data.testId,
|
||||
selected_parts: data.selectedParts,
|
||||
time_limit_minutes: 10,
|
||||
submitted_at: new Date().toISOString(),
|
||||
time_spent_seconds: data.timeUsed,
|
||||
total_correct: data.score,
|
||||
total_questions: data.total,
|
||||
})
|
||||
.select('id')
|
||||
.single()
|
||||
|
||||
if (attemptError) {
|
||||
console.error('Failed to save test attempt:', attemptError.message)
|
||||
return
|
||||
}
|
||||
|
||||
const answerRows = data.answers.map(a => ({
|
||||
attempt_id: attempt.id,
|
||||
question_id: a.questionId,
|
||||
selected_value: a.selected !== null ? ANSWER_VALUES[a.selected] : null,
|
||||
is_correct: a.correct,
|
||||
}))
|
||||
|
||||
const { error: answersError } = await supabase.from('user_answer').insert(answerRows)
|
||||
if (answersError) console.error('Failed to save answers:', answersError.message)
|
||||
}
|
||||
|
||||
/** Fire-and-forget: save writing submission with AI feedback. */
|
||||
@@ -48,10 +73,9 @@ export async function countTodayWritingSubmissions(userId: string): Promise<numb
|
||||
/** Fetch test history for a user (most recent first, max 20). */
|
||||
export async function fetchTestHistory(userId: string) {
|
||||
const { data, error } = await supabase
|
||||
.from('user_progress')
|
||||
.select('*')
|
||||
.from('user_test_attempt')
|
||||
.select('id, selected_parts, time_spent_seconds, total_correct, total_questions, score, submitted_at, created_at')
|
||||
.eq('user_id', userId)
|
||||
.eq('type', 'test')
|
||||
.order('created_at', { ascending: false })
|
||||
.limit(20)
|
||||
if (error) throw error
|
||||
|
||||
Reference in New Issue
Block a user