Enabled a way for students to do assigned tasks
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
// 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, doc, setDoc, addDoc} from "firebase/firestore";
|
||||
import {getFirestore, collection, getDocs, query, where, doc, setDoc, addDoc, getDoc} from "firebase/firestore";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {Stat} from "@/interfaces/user";
|
||||
import {Assignment} from "@/interfaces/results";
|
||||
import {groupBy} from "lodash";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
@@ -42,5 +44,29 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const stats = req.body as Stat[];
|
||||
await stats.forEach(async (stat) => await addDoc(collection(db, "stats"), stat));
|
||||
|
||||
const groupedStatsByAssignment = groupBy(
|
||||
stats.filter((x) => !!x.assignment),
|
||||
"assignment",
|
||||
);
|
||||
if (Object.keys(groupedStatsByAssignment).length > 0) {
|
||||
const assignments = Object.keys(groupedStatsByAssignment);
|
||||
|
||||
await assignments.forEach(async (assignmentId) => {
|
||||
const assignmentStats = groupedStatsByAssignment[assignmentId] as Stat[];
|
||||
|
||||
const assignmentSnapshot = await getDoc(doc(db, "assignments", assignmentId));
|
||||
await setDoc(
|
||||
doc(db, "assignments", assignmentId),
|
||||
{
|
||||
results: [
|
||||
...(assignmentSnapshot.data() as Assignment).results,
|
||||
{user: req.session.user?.id, type: req.session.user?.focus, stats: assignmentStats},
|
||||
],
|
||||
},
|
||||
{merge: true},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user