Skip to content

Commit

Permalink
feat: generate quiz of the day with static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEscaleira committed Apr 15, 2024
1 parent 612bc72 commit 896b489
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ type Query {

"""Get the list of all quizzes based on multiple filter."""
quizList(country: String, status: QuizStatus): [QuizData!]!

"""Get the quiz of the day"""
quizOfTheDay: QuizData!
quizRating(quizId: String!): Int!

"""Get the list of all quizzes based on the country"""
Expand Down
29 changes: 29 additions & 0 deletions src/resolvers/quiz.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { differenceInCalendarDays } from 'date-fns';
import { Arg, Authorized, Ctx, Mutation, Query, registerEnumType, Resolver } from 'type-graphql';
import AttemptModel from '../models/attempt.model';
import QuizModel from '../models/quiz.model';
Expand Down Expand Up @@ -120,4 +121,32 @@ export default class QuizResolver {
errorHandler(error);
}
}

lastGeneratedQuiz: Date | undefined = undefined;
currentQuizOfDayId: any = undefined;

@Query(() => QuizData, { description: 'Get the quiz of the day' })
async quizOfTheDay() {
try {
// Generate the quiz of today if it's the first time
if (!this.lastGeneratedQuiz || !this.currentQuizOfDayId) {
const quizzes = await QuizModel.find();

this.lastGeneratedQuiz = new Date();
this.currentQuizOfDayId = quizzes[Math.floor(Math.random() * quizzes.length)];
}

// Generate the quiz of today
if (differenceInCalendarDays(new Date(), this.lastGeneratedQuiz) >= 1) {
const quizzes = await QuizModel.find();

this.lastGeneratedQuiz = new Date();
this.currentQuizOfDayId = quizzes[Math.floor(Math.random() * quizzes.length)];
}

return this.currentQuizOfDayId;
} catch (error) {
errorHandler(error);
}
}
}

0 comments on commit 896b489

Please sign in to comment.