Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: time based quizzes #3761

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
Loading