From f7af21878e6efdf930cbb4c3e90f26c1b36ad07e Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Thu, 7 Dec 2023 18:20:11 +0000 Subject: [PATCH] Separate get exam bussiness logic into a backend asset --- src/pages/api/exam/[module]/index.ts | 36 ++++--------------- src/utils/exams.be.ts | 52 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 src/utils/exams.be.ts diff --git a/src/pages/api/exam/[module]/index.ts b/src/pages/api/exam/[module]/index.ts index 87fd150b..c48a3d78 100644 --- a/src/pages/api/exam/[module]/index.ts +++ b/src/pages/api/exam/[module]/index.ts @@ -1,14 +1,11 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type {NextApiRequest, NextApiResponse} from "next"; import {app} from "@/firebase"; -import {getFirestore, collection, getDocs, query, where, setDoc, doc} from "firebase/firestore"; +import {getFirestore, setDoc, doc} from "firebase/firestore"; import {withIronSessionApiRoute} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; -import {shuffle} from "lodash"; import {Exam} from "@/interfaces/exam"; -import {Stat} from "@/interfaces/user"; -import {v4} from "uuid"; - +import { getExams } from "@/utils/exams.be"; const db = getFirestore(app); export default withIronSessionApiRoute(handler, sessionOptions); @@ -26,31 +23,12 @@ async function GET(req: NextApiRequest, res: NextApiResponse) { return; } - const {module, avoidRepeated} = req.query as {module: string; avoidRepeated: string}; - const moduleRef = collection(db, module); - - const q = query(moduleRef, where("isDiagnostic", "==", false)); - const snapshot = await getDocs(q); - - const exams: Exam[] = shuffle( - snapshot.docs.map((doc) => ({ - id: doc.id, - ...doc.data(), - module, - })), - ) as Exam[]; - - if (avoidRepeated === "true") { - const statsQ = query(collection(db, "stats"), where("user", "==", req.session.user.id)); - const statsSnapshot = await getDocs(statsQ); - - const stats: Stat[] = statsSnapshot.docs.map((doc) => ({id: doc.id, ...doc.data()})) as unknown as Stat[]; - const filteredExams = exams.filter((x) => !stats.map((s) => s.exam).includes(x.id)); - - res.status(200).json(filteredExams.length > 0 ? filteredExams : exams); - return; - } + const { + module, + avoidRepeated, + } = req.query as {module: string; avoidRepeated: string}; + const exams: Exam[] = await getExams(db, module, avoidRepeated, req.session.user.id); res.status(200).json(exams); } diff --git a/src/utils/exams.be.ts b/src/utils/exams.be.ts new file mode 100644 index 00000000..ee9832f3 --- /dev/null +++ b/src/utils/exams.be.ts @@ -0,0 +1,52 @@ +import { + collection, + getDocs, + query, + where, + setDoc, + doc, + Firestore, +} from "firebase/firestore"; +import { shuffle } from "lodash"; +import { Exam } from "@/interfaces/exam"; +import { Stat } from "@/interfaces/user"; + +export const getExams = async ( + db: Firestore, + module: string, + avoidRepeated: string, + // added userId as due to assignments being set from the teacher to the student + // we need to make sure we are serving exams not executed by the user and not + // by the teacher that performed the request + userId: string +): Promise => { + const moduleRef = collection(db, module); + + const q = query(moduleRef, where("isDiagnostic", "==", false)); + const snapshot = await getDocs(q); + + const exams: Exam[] = shuffle( + snapshot.docs.map((doc) => ({ + id: doc.id, + ...doc.data(), + module, + })) + ) as Exam[]; + + if (avoidRepeated === "true") { + const statsQ = query(collection(db, "stats"), where("user", "==", userId)); + const statsSnapshot = await getDocs(statsQ); + + const stats: Stat[] = statsSnapshot.docs.map((doc) => ({ + id: doc.id, + ...doc.data(), + })) as unknown as Stat[]; + const filteredExams = exams.filter( + (x) => !stats.map((s) => s.exam).includes(x.id) + ); + + return filteredExams.length > 0 ? filteredExams : exams; + } + + return exams; +};