/api/groups and /api/invites refactored, fixed some inserts/updates in which I didn't include the id
This commit is contained in:
@@ -1,21 +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,
|
||||
getDoc,
|
||||
doc,
|
||||
deleteDoc,
|
||||
setDoc,
|
||||
} from "firebase/firestore";
|
||||
import client from "@/lib/mongodb";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { Group } from "@/interfaces/user";
|
||||
import { updateExpiryDateOnGroup } from "@/utils/groups.be";
|
||||
|
||||
const db = getFirestore(app);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
@@ -35,10 +26,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
const snapshot = await getDoc(doc(db, "groups", id));
|
||||
const snapshot = await db.collection("groups").findOne({ id: id});
|
||||
|
||||
if (snapshot.exists()) {
|
||||
res.status(200).json({ ...snapshot.data(), id: snapshot.id });
|
||||
if (snapshot) {
|
||||
res.status(200).json({ ...snapshot });
|
||||
} else {
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
@@ -51,9 +42,12 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const group = await db.collection("groups").findOne<Group>({id: id});
|
||||
|
||||
const snapshot = await getDoc(doc(db, "groups", id));
|
||||
const group = { ...snapshot.data(), id: snapshot.id } as Group;
|
||||
if (!group) {
|
||||
res.status(404);
|
||||
return;
|
||||
}
|
||||
|
||||
const user = req.session.user;
|
||||
if (
|
||||
@@ -61,7 +55,7 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
||||
user.type === "developer" ||
|
||||
user.id === group.admin
|
||||
) {
|
||||
await deleteDoc(snapshot.ref);
|
||||
await db.collection("groups").deleteOne({ id: id });
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
return;
|
||||
@@ -78,8 +72,11 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
const snapshot = await getDoc(doc(db, "groups", id));
|
||||
const group = { ...snapshot.data(), id: snapshot.id } as Group;
|
||||
const group = await db.collection("groups").findOne<Group>({id: id});
|
||||
if (!group) {
|
||||
res.status(404);
|
||||
return;
|
||||
}
|
||||
|
||||
const user = req.session.user;
|
||||
if (
|
||||
@@ -98,7 +95,10 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
||||
);
|
||||
}
|
||||
|
||||
await setDoc(snapshot.ref, req.body, { merge: true });
|
||||
await db.collection("grading").updateOne(
|
||||
{ id: req.session.user.id },
|
||||
{ $set: req.body}
|
||||
);
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user