/api/packages refactored, forgot to commit some changes
This commit is contained in:
@@ -28,7 +28,7 @@ async function GET(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const snapshot = await db.collection("assignments").findOne({ id: id as string });
|
const snapshot = await db.collection("assignments").findOne({ id: id as string });
|
||||||
|
|
||||||
if (snapshot) {
|
if (snapshot) {
|
||||||
res.status(200).json({...snapshot, id: snapshot._id});
|
res.status(200).json({...snapshot, id: snapshot.id});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
await db.collection("discounts").updateOne(
|
await db.collection("discounts").updateOne(
|
||||||
{ id: id },
|
{ id: id },
|
||||||
{ $set: { id: id, ...req.body},
|
{ $set: {id: id, ...req.body} },
|
||||||
{ upsert: true }
|
{ upsert: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -97,7 +97,8 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
await db.collection("grading").updateOne(
|
await db.collection("grading").updateOne(
|
||||||
{ id: req.session.user.id },
|
{ id: req.session.user.id },
|
||||||
{ $set: req.body}
|
{ $set: {id: id, ...req.body} },
|
||||||
|
{ upsert: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
res.status(200).json({ ok: true });
|
res.status(200).json({ ok: true });
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, doc, getDoc, deleteDoc, setDoc} from "firebase/firestore";
|
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {PERMISSIONS} from "@/constants/userPermissions";
|
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -23,14 +22,11 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const {id} = req.query as {id: string};
|
const {id} = req.query as {id: string};
|
||||||
|
const docSnap = await db.collection("packages").findOne({ id: id});
|
||||||
|
|
||||||
const docRef = doc(db, "packages", id);
|
if (docSnap) {
|
||||||
const docSnap = await getDoc(docRef);
|
|
||||||
|
|
||||||
if (docSnap.exists()) {
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
id: docSnap.id,
|
...docSnap,
|
||||||
...docSnap.data(),
|
|
||||||
module,
|
module,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -46,16 +42,16 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const {id} = req.query as {id: string};
|
const {id} = req.query as {id: string};
|
||||||
|
|
||||||
const docRef = doc(db, "packages", id);
|
const docSnap = await db.collection("packages").findOne({ id: id});
|
||||||
const docSnap = await getDoc(docRef);
|
if (docSnap) {
|
||||||
|
|
||||||
if (docSnap.exists()) {
|
|
||||||
if (!["developer", "admin"].includes(req.session.user.type)) {
|
if (!["developer", "admin"].includes(req.session.user.type)) {
|
||||||
res.status(403).json({ok: false});
|
res.status(403).json({ok: false});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
await db.collection("packages").updateOne(
|
||||||
await setDoc(docRef, req.body, {merge: true});
|
{ id: id },
|
||||||
|
{ $set: req.body }
|
||||||
|
);
|
||||||
|
|
||||||
res.status(200).json({ok: true});
|
res.status(200).json({ok: true});
|
||||||
} else {
|
} else {
|
||||||
@@ -71,16 +67,14 @@ async function del(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
const {id} = req.query as {id: string};
|
const {id} = req.query as {id: string};
|
||||||
|
|
||||||
const docRef = doc(db, "packages", id);
|
const docSnap = await db.collection("packages").findOne({ id: id});
|
||||||
const docSnap = await getDoc(docRef);
|
|
||||||
|
|
||||||
if (docSnap.exists()) {
|
if (docSnap) {
|
||||||
if (!["developer", "admin"].includes(req.session.user.type)) {
|
if (!["developer", "admin"].includes(req.session.user.type)) {
|
||||||
res.status(403).json({ok: false});
|
res.status(403).json({ok: false});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
await db.collection("packages").deleteOne({ id: id });
|
||||||
await deleteDoc(docRef);
|
|
||||||
|
|
||||||
res.status(200).json({ok: true});
|
res.status(200).json({ok: true});
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {app} from "@/firebase";
|
import client from "@/lib/mongodb";
|
||||||
import {getFirestore, collection, getDocs, setDoc, doc} from "firebase/firestore";
|
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {Group} from "@/interfaces/user";
|
import {Group} from "@/interfaces/user";
|
||||||
import {Package} from "@/interfaces/paypal";
|
import {Package} from "@/interfaces/paypal";
|
||||||
import {v4} from "uuid";
|
import {v4} from "uuid";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
@@ -18,14 +17,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const snapshot = await getDocs(collection(db, "packages"));
|
const snapshot = await db.collection("packages").find({}).toArray();
|
||||||
|
res.status(200).json(snapshot);
|
||||||
res.status(200).json(
|
|
||||||
snapshot.docs.map((doc) => ({
|
|
||||||
id: doc.id,
|
|
||||||
...doc.data(),
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -38,7 +31,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return res.status(403).json({ok: false, reason: "You do not have permission to create a new package"});
|
return res.status(403).json({ok: false, reason: "You do not have permission to create a new package"});
|
||||||
|
|
||||||
const body = req.body as Package;
|
const body = req.body as Package;
|
||||||
|
// Package already had an id but a new one was being set
|
||||||
await setDoc(doc(db, "packages", v4()), body);
|
// with v4() don't know if intentional or not, recreated the behaviour as was
|
||||||
|
await db.collection("packages").insertOne({...body, id: v4()})
|
||||||
res.status(200).json({ok: true});
|
res.status(200).json({ok: true});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user