Refactored /api/paypal, /api/permissions, /api/reset /api/sessions, /api/stats
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
// 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, getDoc, deleteDoc} 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";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import client from "@/lib/mongodb";
|
||||
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);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
@@ -19,33 +18,28 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
const snapshot = await db.collection("stats").find<Stat>({}).toArray();
|
||||
|
||||
const q = query(collection(db, "stats"));
|
||||
|
||||
const snapshot = await getDocs(q);
|
||||
|
||||
res.status(200).json(
|
||||
snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})),
|
||||
);
|
||||
res.status(200).json(snapshot);
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
const stats = req.body as Stat[];
|
||||
await stats.forEach(async (stat) => await setDoc(doc(db, "stats", stat.id), stat));
|
||||
await stats.forEach(async (stat) => {
|
||||
const sessionDoc = await getDoc(doc(db, "sessions", stat.session));
|
||||
if (sessionDoc.exists()) await deleteDoc(sessionDoc.ref);
|
||||
stats.forEach(async (stat) => await db.collection("stats").updateOne(
|
||||
{ id: stat.id },
|
||||
{ $set: stat },
|
||||
{ upsert: true }
|
||||
));
|
||||
stats.forEach(async (stat) => {
|
||||
await db.collection("sessions").deleteOne({ id: stat.session })
|
||||
});
|
||||
|
||||
const groupedStatsByAssignment = groupBy(
|
||||
@@ -55,22 +49,23 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (Object.keys(groupedStatsByAssignment).length > 0) {
|
||||
const assignments = Object.keys(groupedStatsByAssignment);
|
||||
|
||||
await assignments.forEach(async (assignmentId) => {
|
||||
assignments.forEach(async (assignmentId) => {
|
||||
const assignmentStats = groupedStatsByAssignment[assignmentId] as Stat[];
|
||||
|
||||
const assignmentSnapshot = await getDoc(doc(db, "assignments", assignmentId));
|
||||
await setDoc(
|
||||
doc(db, "assignments", assignmentId),
|
||||
const assignmentSnapshot = await db.collection("assignments").findOne<Assignment>({ id: assignmentId });
|
||||
await db.collection("assignments").updateOne(
|
||||
{ id: assignmentId },
|
||||
{
|
||||
results: [
|
||||
...(assignmentSnapshot.data() as Assignment).results,
|
||||
{user: req.session.user?.id, type: req.session.user?.focus, stats: assignmentStats},
|
||||
],
|
||||
},
|
||||
{merge: true},
|
||||
$set: {
|
||||
results: [
|
||||
...assignmentSnapshot ? assignmentSnapshot.results : [],
|
||||
{ user: req.session.user?.id, type: req.session.user?.focus, stats: assignmentStats },
|
||||
],
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
res.status(200).json({ ok: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user