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

@@ -18,18 +18,18 @@ export const getExams = async (
variant?: Variant, variant?: Variant,
instructorGender?: InstructorGender, instructorGender?: InstructorGender,
): Promise<Exam[]> => { ): Promise<Exam[]> => {
const allExams = await db
.collection(module)
.find<Exam>({
isDiagnostic: false,
})
.toArray();
const allExams = await db.collection(module).find<Exam>({ const shuffledPublicExams = shuffle(
isDiagnostic: false
}).toArray();
const shuffledPublicExams = (
shuffle(
allExams.map((doc) => ({ allExams.map((doc) => ({
...doc, ...doc,
module, module,
})) as Exam[], })) as Exam[],
)
).filter((x) => !x.private); ).filter((x) => !x.private);
let exams: Exam[] = await filterByOwners(shuffledPublicExams, userId); let exams: Exam[] = await filterByOwners(shuffledPublicExams, userId);
@@ -39,9 +39,12 @@ export const getExams = async (
exams = await filterByPreference(db, exams, module, userId); exams = await filterByPreference(db, exams, module, userId);
if (avoidRepeated === "true") { if (avoidRepeated === "true") {
const stats = await db.collection("stats").find<Stat>({ const stats = await db
user: userId .collection("stats")
}).toArray(); .find<Stat>({
user: userId,
})
.toArray();
const filteredExams = exams.filter((x) => !stats.map((s) => s.exam).includes(x.id)); 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) => { const filterByDifficulty = async (db: Db, exams: Exam[], module: Module, userID?: string) => {
if (!userID) return exams; 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; if (!user) return exams;
const difficulty = user.levels[module] <= 3 ? "easy" : user.levels[module] <= 6 ? "medium" : "hard"; 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; 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 (!user) return exams;
if (!["developer", "student"].includes(user.type)) return exams; if (!["developer", "student"].includes(user.type)) return exams;