Prevented sessions from appearing when an assignment is done

This commit is contained in:
Tiago Ribeiro
2024-09-05 16:52:16 +01:00
parent b6b5f3a9f1
commit c8be2f1255

View File

@@ -4,6 +4,8 @@ import {app} from "@/firebase";
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 {Session} from "@/hooks/useSessions";
import moment from "moment";
const db = getFirestore(app);
@@ -24,12 +26,17 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
const q = user ? query(collection(db, "sessions"), where("user", "==", user)) : collection(db, "sessions");
const snapshot = await getDocs(q);
res.status(200).json(
snapshot.docs.map((doc) => ({
const sessions = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})),
})) as Session[];
res.status(200).json(
sessions.filter((x) => {
if (!x.assignment) return true;
if (x.assignment.results.filter((y) => y.user === user).length > 0) return false;
return moment().isAfter(moment(x.assignment.endDate));
}),
);
}