Refactored pages/api/assignments to mongodb

This commit is contained in:
Carlos Mesquita
2024-09-07 15:13:41 +01:00
parent e8b7c5ff80
commit 6f7ef1abef
14 changed files with 820 additions and 462 deletions

View File

@@ -1,20 +1,21 @@
import type {NextApiRequest, NextApiResponse} from "next";
import {app, storage} from "@/firebase";
import {getFirestore, doc, getDoc, updateDoc, getDocs, query, collection, where, documentId} from "firebase/firestore";
import {withIronSessionApiRoute} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import type { NextApiRequest, NextApiResponse } from "next";
import { app, storage } from "@/firebase";
import client from "@/lib/mongodb";
import { ObjectId } from 'mongodb';
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/lib/session";
import ReactPDF from "@react-pdf/renderer";
import GroupTestReport from "@/exams/pdf/group.test.report";
import {ref, uploadBytes, getDownloadURL} from "firebase/storage";
import {Stat, CorporateUser} from "@/interfaces/user";
import {User, DemographicInformation} from "@/interfaces/user";
import {Module} from "@/interfaces";
import {ModuleScore, StudentData} from "@/interfaces/module.scores";
import {SkillExamDetails} from "@/exams/pdf/details/skill.exam";
import {LevelExamDetails} from "@/exams/pdf/details/level.exam";
import {calculateBandScore, getLevelScore} from "@/utils/score";
import {generateQRCode, getRadialProgressPNG, streamToBuffer} from "@/utils/pdf";
import {Group} from "@/interfaces/user";
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
import { Stat, CorporateUser } from "@/interfaces/user";
import { User, DemographicInformation } from "@/interfaces/user";
import { Module } from "@/interfaces";
import { ModuleScore, StudentData } from "@/interfaces/module.scores";
import { SkillExamDetails } from "@/exams/pdf/details/skill.exam";
import { LevelExamDetails } from "@/exams/pdf/details/level.exam";
import { calculateBandScore, getLevelScore } from "@/utils/score";
import { generateQRCode, getRadialProgressPNG, streamToBuffer } from "@/utils/pdf";
import { Group } from "@/interfaces/user";
import moment from "moment-timezone";
interface GroupScoreSummaryHelper {
@@ -22,7 +23,7 @@ interface GroupScoreSummaryHelper {
label: string;
sessions: string[];
}
const db = getFirestore(app);
const db = client.db(process.env.MONGODB_DB);
export default withIronSessionApiRoute(handler, sessionOptions);
@@ -78,14 +79,14 @@ const getPerformanceSummary = (module: Module, score: number) => {
const getScoreAndTotal = (stats: Stat[]) => {
return stats.reduce(
(acc, {score}) => {
(acc, { score }) => {
return {
...acc,
correct: acc.correct + score.correct,
total: acc.total + score.total,
};
},
{correct: 0, total: 0},
{ correct: 0, total: 0 },
);
};
@@ -97,20 +98,21 @@ const getLevelScoreForUserExams = (bandScore: number) => {
async function post(req: NextApiRequest, res: NextApiResponse) {
// verify if it's a logged user that is trying to export
if (req.session.user) {
const {id} = req.query as {id: string};
const { id } = req.query as { id: string };
const docSnap = await getDoc(doc(db, "assignments", id));
const data = docSnap.data() as {
const data = await db.collection("assignments").findOne({ _id: new ObjectId(id) }) as {
_id: ObjectId;
assigner: string;
assignees: string[];
results: any;
exams: {module: Module}[];
exams: { module: Module }[];
startDate: string;
pdf: {
path: string,
version: string,
},
};
} | null;
if (!data) {
res.status(400).end();
return;
@@ -125,16 +127,15 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
}
try {
const docUser = await getDoc(doc(db, "users", req.session.user.id));
if (docUser.exists()) {
// we'll need the user in order to get the user data (name, email, focus, etc);
const user = docUser.data() as User;
const user = await db.collection("users").findOne<User>({ _id: new ObjectId(req.session.user.id) });
// we'll need the user in order to get the user data (name, email, focus, etc);
if (user) {
// generate the QR code for the report
const qrcode = await generateQRCode((req.headers.origin || "") + req.url);
if (!qrcode) {
res.status(500).json({ok: false});
res.status(500).json({ ok: false });
return;
}
@@ -143,17 +144,15 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
return [...accm, ...stats];
}, []) as Stat[];
const docsSnap = await getDocs(query(collection(db, "users"), where(documentId(), "in", data.assignees)));
const users = docsSnap.docs.map((d) => ({
...d.data(),
id: d.id,
})) as User[];
const users = await db.collection("users").find<User>({
_id: { $in: data.assignees.map(id => new ObjectId(id)) }
}).toArray();
const flattenResultsWithGrade = flattenResults.map((e) => {
const focus = users.find((u) => u.id === e.user)?.focus || "academic";
const bandScore = calculateBandScore(e.score.correct, e.score.total, e.module, focus);
return {...e, bandScore};
return { ...e, bandScore };
});
// in order to make sure we are using unique modules, generate the set based on them
@@ -162,7 +161,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
const moduleResults = flattenResultsWithGrade.filter((e) => e.module === module);
const baseBandScore = moduleResults.reduce((accm, curr) => accm + curr.bandScore, 0) / moduleResults.length;
const bandScore = isNaN(baseBandScore) ? 0 : baseBandScore;
const {correct, total} = getScoreAndTotal(moduleResults);
const { correct, total } = getScoreAndTotal(moduleResults);
const png = getRadialProgressPNG("azul", correct, total);
return {
@@ -175,7 +174,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
};
}) as ModuleScore[];
const {correct: overallCorrect, total: overallTotal} = getScoreAndTotal(flattenResults);
const { correct: overallCorrect, total: overallTotal } = getScoreAndTotal(flattenResults);
const baseOverallResult = overallCorrect / overallTotal;
const overallResult = isNaN(baseOverallResult) ? 0 : baseOverallResult;
@@ -216,7 +215,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
};
};
const {title, details} = getCustomData();
const { title, details } = getCustomData();
const numberOfStudents = data.assignees.length;
@@ -228,13 +227,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
exams.length === 0
? "N/A"
: new Date(exams[0].date).toLocaleDateString(undefined, {
year: "numeric",
month: "numeric",
day: "numeric",
});
year: "numeric",
month: "numeric",
day: "numeric",
});
const bandScore = exams.length === 0 ? 0 : exams.reduce((accm, curr) => accm + curr.bandScore, 0) / exams.length;
const {correct, total} = getScoreAndTotal(exams);
const { correct, total } = getScoreAndTotal(exams);
const result = exams.length === 0 ? "N/A" : `${correct}/${total}`;
@@ -258,7 +257,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
const getGroupScoreSummary = () => {
const resultHelper = studentsData.reduce((accm: GroupScoreSummaryHelper[], curr) => {
const {bandScore, id} = curr;
const { bandScore, id } = curr;
const flooredScore = Math.floor(bandScore);
@@ -286,7 +285,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
];
}, []) as GroupScoreSummaryHelper[];
const result = resultHelper.map(({score, label, sessions}) => {
const result = resultHelper.map(({ score, label, sessions }) => {
const finalLabel = showLevel ? getLevelScore(score[0])[1] : label;
return {
label: finalLabel,
@@ -300,36 +299,20 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
const getInstitution = async () => {
try {
// due to database inconsistencies, I'll be overprotective here
const assignerUserSnap = await getDoc(doc(db, "users", data.assigner));
if (assignerUserSnap.exists()) {
// we'll need the user in order to get the user data (name, email, focus, etc);
const assignerUser = assignerUserSnap.data() as User;
const assignerUser = await db.collection("users").findOne<User>({ _id: new ObjectId(data.assigner) });
// we'll need the user in order to get the user data (name, email, focus, etc);
if (assignerUser) {
if (assignerUser.type === "teacher") {
// also search for groups where this user belongs
const queryGroups = query(collection(db, "groups"), where("participants", "array-contains", assignerUser.id));
const groupSnapshot = await getDocs(queryGroups);
const groups = groupSnapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as Group[];
const groups = await db.collection("groups")
.find<Group>({ participants: assignerUser.id })
.toArray();
if (groups.length > 0) {
const adminQuery = query(
collection(db, "users"),
where(
documentId(),
"in",
groups.map((g) => g.admin),
),
);
const adminUsersSnap = await getDocs(adminQuery);
const admins = adminUsersSnap.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as CorporateUser[];
const admins = await db.collection("users")
.find<CorporateUser>({ _id: { $in: groups.map(g => g.admin).map(id => new ObjectId(id))} })
.toArray();
const adminData = admins.find((a) => a.corporateInformation?.companyInformation?.name);
if (adminData) {
@@ -388,39 +371,44 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
});
// update the stats entries with the pdf url to prevent duplication
await updateDoc(docSnap.ref, {
pdf: {
path: refName,
version: process.env.PDF_VERSION,
},
});
await db.collection("assignments").updateOne(
{ _id: new ObjectId(data._id) },
{
$set: {
pdf: {
path: refName,
version: process.env.PDF_VERSION,
}
}
}
);
const url = await getDownloadURL(fileRef);
res.status(200).end(url);
return;
}
res.status(401).json({ok: false});
res.status(401).json({ ok: false });
return;
} catch (err) {
console.error(err);
res.status(500).json({ok: false});
res.status(500).json({ ok: false });
return;
}
}
}
async function get(req: NextApiRequest, res: NextApiResponse) {
if (req.session.user) {
const {id} = req.query as {id: string};
const { id } = req.query as { id: string };
const docSnap = await getDoc(doc(db, "assignments", id));
const data = docSnap.data();
const data = await db.collection("assignments").findOne({ _id: new ObjectId(id) });
if (!data) {
res.status(400).end();
return;
}
if (data.assigner !== req.session.user.id) {
res.status(401).json({ok: false});
res.status(401).json({ ok: false });
return;
}
@@ -434,6 +422,6 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
return;
}
res.status(401).json({ok: false});
res.status(401).json({ ok: false });
return;
}