Solved a problem with the _id

This commit is contained in:
Tiago Ribeiro
2024-09-08 19:25:14 +01:00
parent 49e8237e99
commit a5b3a7e94d

View File

@@ -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<Exam[]> => {
const allExams = await db
.collection(module)
.find<Exam>({
isDiagnostic: false,
})
.toArray();
const allExams = await db.collection(module).find<Exam>({
isDiagnostic: false
}).toArray();
const shuffledPublicExams = (
shuffle(
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<Stat>({
user: userId
}).toArray();
const stats = await db
.collection("stats")
.find<Stat>({
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<User>({ _id: new ObjectId(userID) });
const user = await db.collection("users").findOne<User>({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<StudentUser | DeveloperUser>({ _id: new ObjectId(userID) });
const user = await db.collection("users").findOne<StudentUser | DeveloperUser>({id: userID});
if (!user) return exams;
if (!["developer", "student"].includes(user.type)) return exams;