diff --git a/src/interfaces/exam.ts b/src/interfaces/exam.ts index e69c6050..3e2051b6 100644 --- a/src/interfaces/exam.ts +++ b/src/interfaces/exam.ts @@ -4,7 +4,7 @@ export type Exam = ReadingExam | ListeningExam | WritingExam | SpeakingExam | Le export type Variant = "full" | "partial"; export type InstructorGender = "male" | "female" | "varied"; -export type Difficulty = BasicDifficulty | CEFRLevels; +export type Difficulty = BasicDifficulty | CEFRLevels; // Left easy, medium and hard to support older exam versions export type BasicDifficulty = "easy" | "medium" | "hard"; @@ -19,6 +19,7 @@ export interface ExamBase { variant?: Variant; difficulty?: Difficulty; owners?: string[]; + entities?: string[] shuffle?: boolean; createdBy?: string; // option as it has been added later createdAt?: string; // option as it has been added later @@ -51,7 +52,7 @@ export interface LevelExam extends ExamBase { } export interface LevelPart extends Section { - // to support old exams that have reading passage mc on context + // to support old exams that have reading passage mc on context context?: string; exercises: Exercise[]; audio?: { diff --git a/src/pages/api/exam/[module]/index.ts b/src/pages/api/exam/[module]/index.ts index d54e7835..563b953b 100644 --- a/src/pages/api/exam/[module]/index.ts +++ b/src/pages/api/exam/[module]/index.ts @@ -7,6 +7,9 @@ import { Exam, ExamBase, InstructorGender, Variant } from "@/interfaces/exam"; import { getExams } from "@/utils/exams.be"; import { Module } from "@/interfaces"; 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); @@ -37,25 +40,20 @@ async function GET(req: NextApiRequest, res: NextApiResponse) { } async function POST(req: NextApiRequest, res: NextApiResponse) { - if (!req.session.user) { - res.status(401).json({ ok: false }); - return; - } + const user = await requestUser(req, res) + if (!user) return res.status(401).json({ ok: false }); const { module } = req.query as { module: string }; - const corporate = await getUserCorporate(req.session.user.id); const session = client.startSession(); + const entities = isAdmin(user) ? [] : mapBy(user.entities, 'id') try { const exam = { ...req.body, module: module, - owners: [ - ...(["mastercorporate", "corporate"].includes(req.session.user.type) ? [req.session.user.id] : []), - ...(!!corporate ? [corporate.id] : []), - ], - createdBy: req.session.user.id, + entities, + createdBy: user.id, createdAt: new Date().toISOString(), }; diff --git a/src/utils/exams.be.ts b/src/utils/exams.be.ts index 38c285b5..94edd0a2 100644 --- a/src/utils/exams.be.ts +++ b/src/utils/exams.be.ts @@ -9,6 +9,7 @@ import { Db, ObjectId } from "mongodb"; import client from "@/lib/mongodb"; import { MODULE_ARRAY } from "./moduleUtils"; import { mapBy } from "."; +import { getUser } from "./users.be"; const db = client.db(process.env.MONGODB_DB); @@ -76,7 +77,7 @@ export const getExams = async ( })) as Exam[], ).filter((x) => !x.private); - let exams: Exam[] = await filterByOwners(shuffledPublicExams, userId); + let exams: Exam[] = await filterByEntities(shuffledPublicExams, userId); exams = filterByVariant(exams, variant); exams = filterByInstructorGender(exams, instructorGender); exams = await filterByDifficulty(db, exams, module, userId); @@ -109,16 +110,17 @@ const filterByVariant = (exams: Exam[], variant?: Variant) => { return filtered.length > 0 ? filtered : exams; }; -const filterByOwners = async (exams: Exam[], userID?: string) => { - if (!userID) return exams.filter((x) => !x.owners || x.owners.length === 0); +const filterByEntities = async (exams: Exam[], userID?: string) => { + if (!userID) return exams.filter((x) => !x.entities || x.entities.length === 0); + + const user = await getUser(userID) + return await Promise.all( exams.filter(async (x) => { - if (!x.owners) return true; - if (x.owners.length === 0) return true; - if (x.owners.includes(userID)) return true; + if (!x.entities) return true; + if (x.entities.length === 0) return true; - const corporate = await getUserCorporate(userID); - return !corporate ? false : x.owners.includes(corporate.id); + return mapBy(user?.entities || [], 'id').some(e => x.entities!.includes(e)) }), ); };