/api/groups and /api/invites refactored, fixed some inserts/updates in which I didn't include the id
This commit is contained in:
@@ -1,19 +1,11 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { app } from "@/firebase";
|
||||
import {
|
||||
getFirestore,
|
||||
getDoc,
|
||||
doc,
|
||||
deleteDoc,
|
||||
setDoc,
|
||||
} from "firebase/firestore";
|
||||
import client from "@/lib/mongodb";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { Ticket } from "@/interfaces/ticket";
|
||||
import { Invite } from "@/interfaces/invite";
|
||||
|
||||
const db = getFirestore(app);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
@@ -33,10 +25,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
const snapshot = await db.collection("invites").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);
|
||||
}
|
||||
@@ -50,12 +42,15 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
const data = snapshot.data() as Invite;
|
||||
const snapshot = await db.collection("invites").findOne<Invite>({ id: id });
|
||||
if(!snapshot){
|
||||
res.status(404);
|
||||
return;
|
||||
}
|
||||
|
||||
const user = req.session.user;
|
||||
if (user.type === "admin" || user.type === "developer") {
|
||||
await deleteDoc(snapshot.ref);
|
||||
await db.collection("invites").deleteOne({ id: id });
|
||||
res.status(200).json({ ok: true });
|
||||
return;
|
||||
}
|
||||
@@ -70,11 +65,14 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
|
||||
const user = req.session.user;
|
||||
|
||||
if (user.type === "admin" || user.type === "developer") {
|
||||
await setDoc(snapshot.ref, req.body, { merge: true });
|
||||
await db.collection("invites").updateOne(
|
||||
{ id: id },
|
||||
{ $set: {id: id, ...req.body} },
|
||||
{ upsert: true }
|
||||
);
|
||||
return res.status(200).json({ ok: true });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user