Solved a problem with the _id
This commit is contained in:
@@ -5,7 +5,7 @@ import {DeveloperUser, Stat, StudentUser, User} from "@/interfaces/user";
|
|||||||
import {Module} from "@/interfaces";
|
import {Module} from "@/interfaces";
|
||||||
import {getCorporateUser} from "@/resources/user";
|
import {getCorporateUser} from "@/resources/user";
|
||||||
import {getUserCorporate} from "./groups.be";
|
import {getUserCorporate} from "./groups.be";
|
||||||
import { Db, ObjectId } from "mongodb";
|
import {Db, ObjectId} from "mongodb";
|
||||||
|
|
||||||
export const getExams = async (
|
export const getExams = async (
|
||||||
db: Db,
|
db: Db,
|
||||||
@@ -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
|
allExams.map((doc) => ({
|
||||||
}).toArray();
|
...doc,
|
||||||
|
module,
|
||||||
const shuffledPublicExams = (
|
})) as Exam[],
|
||||||
shuffle(
|
|
||||||
allExams.map((doc) => ({
|
|
||||||
...doc,
|
|
||||||
module,
|
|
||||||
})) 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;
|
||||||
|
|||||||
Reference in New Issue
Block a user