Separate get exam bussiness logic into a backend asset
This commit is contained in:
@@ -1,14 +1,11 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {app} from "@/firebase";
|
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 {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {shuffle} from "lodash";
|
|
||||||
import {Exam} from "@/interfaces/exam";
|
import {Exam} from "@/interfaces/exam";
|
||||||
import {Stat} from "@/interfaces/user";
|
import { getExams } from "@/utils/exams.be";
|
||||||
import {v4} from "uuid";
|
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
@@ -26,31 +23,12 @@ async function GET(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {module, avoidRepeated} = req.query as {module: string; avoidRepeated: string};
|
const {
|
||||||
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,
|
module,
|
||||||
})),
|
avoidRepeated,
|
||||||
) as Exam[];
|
} = req.query as {module: string; avoidRepeated: string};
|
||||||
|
|
||||||
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 exams: Exam[] = await getExams(db, module, avoidRepeated, req.session.user.id);
|
||||||
res.status(200).json(exams);
|
res.status(200).json(exams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
52
src/utils/exams.be.ts
Normal file
52
src/utils/exams.be.ts
Normal file
@@ -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<Exam[]> => {
|
||||||
|
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;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user