diff --git a/src/utils/exams.be.ts b/src/utils/exams.be.ts index 18621bc6..8b7b83df 100644 --- a/src/utils/exams.be.ts +++ b/src/utils/exams.be.ts @@ -5,7 +5,7 @@ import {DeveloperUser, Stat, StudentUser, User} from "@/interfaces/user"; import {Module} from "@/interfaces"; import {getCorporateUser} from "@/resources/user"; import {getUserCorporate} from "./groups.be"; -import { Db, ObjectId } from "mongodb"; +import {Db, ObjectId} from "mongodb"; export const getExams = async ( db: Db, @@ -18,18 +18,18 @@ export const getExams = async ( variant?: Variant, instructorGender?: InstructorGender, ): Promise => { + const allExams = await db + .collection(module) + .find({ + isDiagnostic: false, + }) + .toArray(); - const allExams = await db.collection(module).find({ - isDiagnostic: false - }).toArray(); - - const shuffledPublicExams = ( - shuffle( - allExams.map((doc) => ({ - ...doc, - module, - })) as Exam[], - ) + const shuffledPublicExams = shuffle( + allExams.map((doc) => ({ + ...doc, + module, + })) as Exam[], ).filter((x) => !x.private); let exams: Exam[] = await filterByOwners(shuffledPublicExams, userId); @@ -39,9 +39,12 @@ export const getExams = async ( exams = await filterByPreference(db, exams, module, userId); if (avoidRepeated === "true") { - const stats = await db.collection("stats").find({ - user: userId - }).toArray(); + const stats = await db + .collection("stats") + .find({ + user: userId, + }) + .toArray(); const filteredExams = exams.filter((x) => !stats.map((s) => s.exam).includes(x.id)); @@ -78,7 +81,7 @@ const filterByOwners = async (exams: Exam[], userID?: string) => { const filterByDifficulty = async (db: Db, exams: Exam[], module: Module, userID?: string) => { if (!userID) return exams; - const user = await db.collection("users").findOne({ _id: new ObjectId(userID) }); + const user = await db.collection("users").findOne({id: userID}); if (!user) return exams; const difficulty = user.levels[module] <= 3 ? "easy" : user.levels[module] <= 6 ? "medium" : "hard"; @@ -92,7 +95,7 @@ const filterByPreference = async (db: Db, exams: Exam[], module: Module, userID? if (!userID) return exams; - const user = await db.collection("users").findOne({ _id: new ObjectId(userID) }); + const user = await db.collection("users").findOne({id: userID}); if (!user) return exams; if (!["developer", "student"].includes(user.type)) return exams;