Updated the exams to work based on entities
This commit is contained in:
@@ -19,6 +19,7 @@ export interface ExamBase {
|
|||||||
variant?: Variant;
|
variant?: Variant;
|
||||||
difficulty?: Difficulty;
|
difficulty?: Difficulty;
|
||||||
owners?: string[];
|
owners?: string[];
|
||||||
|
entities?: string[]
|
||||||
shuffle?: boolean;
|
shuffle?: boolean;
|
||||||
createdBy?: string; // option as it has been added later
|
createdBy?: string; // option as it has been added later
|
||||||
createdAt?: string; // option as it has been added later
|
createdAt?: string; // option as it has been added later
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import { Exam, ExamBase, InstructorGender, Variant } from "@/interfaces/exam";
|
|||||||
import { getExams } from "@/utils/exams.be";
|
import { getExams } from "@/utils/exams.be";
|
||||||
import { Module } from "@/interfaces";
|
import { Module } from "@/interfaces";
|
||||||
import { getUserCorporate } from "@/utils/groups.be";
|
import { getUserCorporate } from "@/utils/groups.be";
|
||||||
|
import { requestUser } from "@/utils/api";
|
||||||
|
import { isAdmin } from "@/utils/users";
|
||||||
|
import { mapBy } from "@/utils";
|
||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
@@ -37,25 +40,20 @@ async function GET(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
const user = await requestUser(req, res)
|
||||||
res.status(401).json({ ok: false });
|
if (!user) return res.status(401).json({ ok: false });
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { module } = req.query as { module: string };
|
const { module } = req.query as { module: string };
|
||||||
const corporate = await getUserCorporate(req.session.user.id);
|
|
||||||
|
|
||||||
const session = client.startSession();
|
const session = client.startSession();
|
||||||
|
const entities = isAdmin(user) ? [] : mapBy(user.entities, 'id')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const exam = {
|
const exam = {
|
||||||
...req.body,
|
...req.body,
|
||||||
module: module,
|
module: module,
|
||||||
owners: [
|
entities,
|
||||||
...(["mastercorporate", "corporate"].includes(req.session.user.type) ? [req.session.user.id] : []),
|
createdBy: user.id,
|
||||||
...(!!corporate ? [corporate.id] : []),
|
|
||||||
],
|
|
||||||
createdBy: req.session.user.id,
|
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { Db, ObjectId } from "mongodb";
|
|||||||
import client from "@/lib/mongodb";
|
import client from "@/lib/mongodb";
|
||||||
import { MODULE_ARRAY } from "./moduleUtils";
|
import { MODULE_ARRAY } from "./moduleUtils";
|
||||||
import { mapBy } from ".";
|
import { mapBy } from ".";
|
||||||
|
import { getUser } from "./users.be";
|
||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
@@ -76,7 +77,7 @@ export const getExams = async (
|
|||||||
})) as Exam[],
|
})) as Exam[],
|
||||||
).filter((x) => !x.private);
|
).filter((x) => !x.private);
|
||||||
|
|
||||||
let exams: Exam[] = await filterByOwners(shuffledPublicExams, userId);
|
let exams: Exam[] = await filterByEntities(shuffledPublicExams, userId);
|
||||||
exams = filterByVariant(exams, variant);
|
exams = filterByVariant(exams, variant);
|
||||||
exams = filterByInstructorGender(exams, instructorGender);
|
exams = filterByInstructorGender(exams, instructorGender);
|
||||||
exams = await filterByDifficulty(db, exams, module, userId);
|
exams = await filterByDifficulty(db, exams, module, userId);
|
||||||
@@ -109,16 +110,17 @@ const filterByVariant = (exams: Exam[], variant?: Variant) => {
|
|||||||
return filtered.length > 0 ? filtered : exams;
|
return filtered.length > 0 ? filtered : exams;
|
||||||
};
|
};
|
||||||
|
|
||||||
const filterByOwners = async (exams: Exam[], userID?: string) => {
|
const filterByEntities = async (exams: Exam[], userID?: string) => {
|
||||||
if (!userID) return exams.filter((x) => !x.owners || x.owners.length === 0);
|
if (!userID) return exams.filter((x) => !x.entities || x.entities.length === 0);
|
||||||
|
|
||||||
|
const user = await getUser(userID)
|
||||||
|
|
||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
exams.filter(async (x) => {
|
exams.filter(async (x) => {
|
||||||
if (!x.owners) return true;
|
if (!x.entities) return true;
|
||||||
if (x.owners.length === 0) return true;
|
if (x.entities.length === 0) return true;
|
||||||
if (x.owners.includes(userID)) return true;
|
|
||||||
|
|
||||||
const corporate = await getUserCorporate(userID);
|
return mapBy(user?.entities || [], 'id').some(e => x.entities!.includes(e))
|
||||||
return !corporate ? false : x.owners.includes(corporate.id);
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user