diff --git a/src/Quiz/quiz.schema.ts b/src/Quiz/quiz.schema.ts index 269487a..dc30086 100644 --- a/src/Quiz/quiz.schema.ts +++ b/src/Quiz/quiz.schema.ts @@ -1,5 +1,5 @@ import crypto from 'crypto' -import { Inject, Injectable } from '@nestjs/common' +import { Inject, Injectable, Logger } from '@nestjs/common' import { AnswerType, Role } from '@prisma/client' import { QuestionType } from '@prisma/client' import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos' @@ -195,8 +195,12 @@ export class QuizSchema extends PothosSchema { }, }) // get amount of questions using nrOfQuestions and random index based on random - const randomIndex = Math.floor(random * (quizzes[0]?.nrOfQuestions ?? 1)) - return quizzes.slice(randomIndex, randomIndex + (quizzes[0]?.nrOfQuestions ?? 1)) + const randomIndex = quizzes.length > 0 + ? Math.floor(random * quizzes.length) + : 0 + const nrOfQuestions = quizzes[0]?.nrOfQuestions ?? 1 + const result = quizzes.slice(randomIndex, randomIndex + nrOfQuestions) + return result } // use case 2: center mentor or center owner