Skip to content

Commit

Permalink
fix: time based quizzes (#3761)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasburtey authored Dec 28, 2023
1 parent 3eb3ed3 commit 59d7ae9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
22 changes: 16 additions & 6 deletions core/api/src/domain/quiz/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ type FillQuizInformationResult = {
}[]
}

const lastSectionCompleted = (quizzesCompleted: QuizCompleted[]): number => {
const quizzesCompletedIds = quizzesCompleted.map((quiz) => quiz.quizId)
const lastQuizCompleted = quizzesCompletedIds[quizzesCompletedIds.length - 1]
const orderedKeys = Object.keys(QuizzesValue)

const reorderQuizzes = (quizzesCompleted: QuizCompleted[]) => {
return quizzesCompleted.sort((a, b) => {
const indexA = orderedKeys.indexOf(a.quizId)
const indexB = orderedKeys.indexOf(b.quizId)
return indexA - indexB
})
}

const lastSectionCompleted = (orderedQuizzes: QuizCompleted[]): number => {
const lastQuizCompleted = orderedQuizzes[orderedQuizzes.length - 1]?.quizId

// which section are we?
const index = QuizzesSections.findIndex((section) =>
Expand All @@ -43,10 +52,11 @@ const lastSectionCompleted = (quizzesCompleted: QuizCompleted[]): number => {
export const fillQuizInformation = (
quizzesCompleted: QuizCompleted[],
): FillQuizInformationResult => {
const currentSection = lastSectionCompleted(quizzesCompleted)
const orderedQuizzes = reorderQuizzes(quizzesCompleted)
const currentSection = lastSectionCompleted(orderedQuizzes)

const quizzes = Object.entries(QuizzesValue).map(([id, amount]) => {
const quizCompleted = quizzesCompleted.find((quiz) => quiz.quizId === id)
const quizCompleted = orderedQuizzes.find((quiz) => quiz.quizId === id)
const section =
QuizzesSections.find((section) => section.quiz.includes(id as QuizQuestionId))
?.order ?? NaN
Expand All @@ -56,7 +66,7 @@ export const fillQuizInformation = (
let notBefore: Date | undefined = undefined
if (section !== 0 && !completed) {
const lastQuizCreatedAt =
quizzesCompleted[quizzesCompleted.length - 1]?.createdAt ?? new Date()
orderedQuizzes[orderedQuizzes.length - 1]?.createdAt ?? new Date()

notBefore = new Date(lastQuizCreatedAt.getTime() + milliSecondsBetweenSections)
}
Expand Down
58 changes: 58 additions & 0 deletions core/api/test/unit/domain/quiz/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,62 @@ describe("quiz", () => {
-3,
)
})

it("can only do next session when Date is older than 12 hours", () => {
const dateFromLastWeek = new Date()
dateFromLastWeek.setDate(dateFromLastWeek.getDate() - 7)

const quizzesCompleted = [
{ quizId: "whatIsBitcoin" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "sat" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "whereBitcoinExist" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "whoControlsBitcoin" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "copyBitcoin" as QuizQuestionId, createdAt: dateFromLastWeek },
]
const filledInfo = fillQuizInformation(quizzesCompleted)
expect(filledInfo.currentSection).toBe(1)

expect(filledInfo.quizzes[4].notBefore).toBeUndefined()

const currentQuiz = filledInfo.quizzes[5]
expect(currentQuiz.notBefore?.getTime()).toBeCloseTo(
dateFromLastWeek.getTime() + 12 * 60 * 60 * 1000,
-3,
)
expect(currentQuiz.notBefore && currentQuiz.notBefore > new Date()).toBe(false)
})

it("check that the algo use most recent date properly", () => {
const dateFromLastWeek = new Date()
dateFromLastWeek.setDate(dateFromLastWeek.getDate() - 7)

const dateFromLastHour = new Date()
dateFromLastHour.setHours(dateFromLastHour.getHours() - 1)

const quizzesCompleted = [
{ quizId: "sat" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "whereBitcoinExist" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "whoControlsBitcoin" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "copyBitcoin" as QuizQuestionId, createdAt: dateFromLastWeek },
{ quizId: "moneySocialAgreement" as QuizQuestionId, createdAt: dateFromLastHour },
{ quizId: "coincidenceOfWants" as QuizQuestionId, createdAt: dateFromLastHour },
{ quizId: "moneyEvolution" as QuizQuestionId, createdAt: dateFromLastHour },
{ quizId: "whyStonesShellGold" as QuizQuestionId, createdAt: dateFromLastHour },
{ quizId: "moneyIsImportant" as QuizQuestionId, createdAt: dateFromLastHour },
{
quizId: "moneyImportantGovernement" as QuizQuestionId,
createdAt: dateFromLastHour,
},
{ quizId: "whatIsBitcoin" as QuizQuestionId, createdAt: dateFromLastWeek },
]
const filledInfo = fillQuizInformation(quizzesCompleted)
expect(filledInfo.currentSection).toBe(2)

const currentQuiz = filledInfo.quizzes[11]
expect(currentQuiz.notBefore?.getTime()).toBeCloseTo(
dateFromLastHour.getTime() + 12 * 60 * 60 * 1000,
-3,
)
expect(currentQuiz.notBefore && currentQuiz.notBefore > new Date()).toBe(true)
})
})

0 comments on commit 59d7ae9

Please sign in to comment.