Skip to content

Commit

Permalink
fix: cleanup existed questions in test only when test have questions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun969 committed Oct 21, 2023
1 parent d1afd0b commit 3fb2685
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/_game/helpers/getQuestions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { desc, eq, inArray } from 'drizzle-orm'
import { desc, eq, inArray, sql } from 'drizzle-orm'

import { db } from '~/db'
import { fetchQuestionCount } from '~/db/queries'
Expand Down Expand Up @@ -73,7 +73,15 @@ const generateRandomQuestions = async ({
.insert(tests)
.values({ id: testId })
.onDuplicateKeyUpdate({ set: { id: testId } })
await db.delete(questionsToTests).where(eq(questionsToTests.testId, testId))
const testQuestionCount = (
await db
.select({ count: sql<number>`count(*)` })
.from(questionsToTests)
.where(eq(questionsToTests.testId, testId))
)[0]?.count
if (testQuestionCount && testQuestionCount > 0) {
await db.delete(questionsToTests).where(eq(questionsToTests.testId, testId))
}
await db
.insert(questionsToTests)
.values(randomIds.map((questionId) => ({ questionId, testId })))
Expand Down

0 comments on commit 3fb2685

Please sign in to comment.