Added the ability for a user to select to avoid repeated exams
This commit is contained in:
@@ -5,6 +5,8 @@ import {getFirestore, collection, getDocs, query, where} from "firebase/firestor
|
||||
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";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
@@ -18,17 +20,28 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const {module, avoidRepeated} = req.query as {module: string; avoidRepeated: string};
|
||||
const moduleRef = collection(db, module);
|
||||
const q = query(moduleRef, where("isDiagnostic", "==", false));
|
||||
|
||||
const q = query(moduleRef, where("isDiagnostic", "==", false));
|
||||
const snapshot = await getDocs(q);
|
||||
|
||||
res.status(200).json(
|
||||
shuffle(
|
||||
snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
module,
|
||||
})),
|
||||
),
|
||||
);
|
||||
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;
|
||||
}
|
||||
|
||||
res.status(200).json(exams);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user