diff --git a/src/interfaces/user.ts b/src/interfaces/user.ts index ca30ced0..110650a5 100644 --- a/src/interfaces/user.ts +++ b/src/interfaces/user.ts @@ -1,4 +1,3 @@ -import { ObjectId } from "mongodb"; import {Module} from "."; import {InstructorGender, ShuffleMap} from "./exam"; import {PermissionType} from "./permissions"; @@ -7,7 +6,6 @@ export type User = StudentUser | TeacherUser | CorporateUser | AgentUser | Admin export type UserStatus = "active" | "disabled" | "paymentDue"; export interface BasicUser { - _id: ObjectId; email: string; name: string; profilePicture: string; @@ -154,6 +152,7 @@ export interface Group { } export interface Code { + id: string; code: string; creator: string; expiryDate: Date; diff --git a/src/pages/api/assignments/[id]/[export]/excel.ts b/src/pages/api/assignments/[id]/[export]/excel.ts index 81da0c31..a3e57b1d 100644 --- a/src/pages/api/assignments/[id]/[export]/excel.ts +++ b/src/pages/api/assignments/[id]/[export]/excel.ts @@ -1,7 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { app, storage } from "@/firebase"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; import { ref, uploadBytes, getDownloadURL } from "firebase/storage"; @@ -20,7 +19,7 @@ interface GroupScoreSummaryHelper { } interface AssignmentData { - _id: ObjectId; + id: string; assigner: string; assignees: string[]; results: any; @@ -384,7 +383,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { if (req.session.user) { const { id } = req.query as { id: string }; - const assignment = await db.collection("assignments").findOne({ _id: new ObjectId(id) }); + const assignment = await db.collection("assignments").findOne({ id: id }); if (!assignment) { res.status(400).end(); return; @@ -402,13 +401,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) { // return; // } - const objectIds = assignment.assignees.map(id => new ObjectId(id)); + const objectIds = assignment.assignees.map(id => id); - const users = await db.collection("users").find({ - _id: { $in: objectIds } - }).toArray() as User[] | null; + const users = await db.collection("users").find({ + id: { $in: objectIds } + }).toArray(); - const user = await db.collection("users").findOne({ _id: new ObjectId(assignment.assigner) }); + const user = await db.collection("users").findOne({ id: assignment.assigner }); // we'll need the user in order to get the user data (name, email, focus, etc); if (user && users) { @@ -442,7 +441,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { // update the stats entries with the pdf url to prevent duplication await db.collection("assignments").updateOne( - { _id: assignment._id }, + { id: assignment.id }, { $set: { excel: { diff --git a/src/pages/api/assignments/[id]/[export]/pdf.tsx b/src/pages/api/assignments/[id]/[export]/pdf.tsx index 7182bbcf..afbe08c6 100644 --- a/src/pages/api/assignments/[id]/[export]/pdf.tsx +++ b/src/pages/api/assignments/[id]/[export]/pdf.tsx @@ -1,7 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; -import { app, storage } from "@/firebase"; +import { storage } from "@/firebase"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; import ReactPDF from "@react-pdf/renderer"; @@ -100,8 +99,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) { if (req.session.user) { const { id } = req.query as { id: string }; - const data = await db.collection("assignments").findOne({ _id: new ObjectId(id) }) as { - _id: ObjectId; + const data = await db.collection("assignments").findOne({ id: id }) as { + id: string; assigner: string; assignees: string[]; results: any; @@ -127,7 +126,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { } try { - const user = await db.collection("users").findOne({ _id: new ObjectId(req.session.user.id) }); + const user = await db.collection("users").findOne({ id: req.session.user.id }); // we'll need the user in order to get the user data (name, email, focus, etc); if (user) { @@ -145,7 +144,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { }, []) as Stat[]; const users = await db.collection("users").find({ - _id: { $in: data.assignees.map(id => new ObjectId(id)) } + id: { $in: data.assignees.map(id => id) } }).toArray(); const flattenResultsWithGrade = flattenResults.map((e) => { @@ -299,7 +298,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const getInstitution = async () => { try { // due to database inconsistencies, I'll be overprotective here - const assignerUser = await db.collection("users").findOne({ _id: new ObjectId(data.assigner) }); + const assignerUser = await db.collection("users").findOne({ id: data.assigner }); // we'll need the user in order to get the user data (name, email, focus, etc); if (assignerUser) { @@ -311,7 +310,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { if (groups.length > 0) { const admins = await db.collection("users") - .find({ _id: { $in: groups.map(g => g.admin).map(id => new ObjectId(id))} }) + .find({ id: { $in: groups.map(g => g.admin).map(id => id)} }) .toArray(); const adminData = admins.find((a) => a.corporateInformation?.companyInformation?.name); @@ -372,7 +371,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { // update the stats entries with the pdf url to prevent duplication await db.collection("assignments").updateOne( - { _id: new ObjectId(data._id) }, + { id: data.id }, { $set: { pdf: { @@ -401,7 +400,7 @@ async function get(req: NextApiRequest, res: NextApiResponse) { if (req.session.user) { const { id } = req.query as { id: string }; - const data = await db.collection("assignments").findOne({ _id: new ObjectId(id) }); + const data = await db.collection("assignments").findOne({ id: id }); if (!data) { res.status(400).end(); return; diff --git a/src/pages/api/assignments/[id]/archive.tsx b/src/pages/api/assignments/[id]/archive.tsx index 530e0b54..d6686e34 100644 --- a/src/pages/api/assignments/[id]/archive.tsx +++ b/src/pages/api/assignments/[id]/archive.tsx @@ -1,8 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; -import { app } from "@/firebase"; - import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; @@ -15,7 +12,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { // verify if it's a logged user that is trying to archive if (req.session.user) { const { id } = req.query as { id: string }; - const docSnap = await db.collection("assignments").findOne({ _id: new ObjectId(id) }); + const docSnap = await db.collection("assignments").findOne({ id: id }); if (!docSnap) { res.status(404).json({ ok: false }); @@ -23,7 +20,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { } await db.collection("assignments").updateOne( - { _id: new ObjectId(docSnap._id) }, + { id: docSnap.id }, { $set: { archived: true } } ); res.status(200).json({ ok: true }); diff --git a/src/pages/api/assignments/[id]/index.ts b/src/pages/api/assignments/[id]/index.ts index 9bf2afb1..3a85cd34 100644 --- a/src/pages/api/assignments/[id]/index.ts +++ b/src/pages/api/assignments/[id]/index.ts @@ -1,7 +1,6 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type {NextApiRequest, NextApiResponse} from "next"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import {withIronSessionApiRoute} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; @@ -26,7 +25,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { async function GET(req: NextApiRequest, res: NextApiResponse) { const {id} = req.query; - const snapshot = await db.collection("assignments").findOne({ _id: new ObjectId(id as string) }); + const snapshot = await db.collection("assignments").findOne({ id: id as string }); if (snapshot) { res.status(200).json({...snapshot, id: snapshot._id}); @@ -37,7 +36,7 @@ async function DELETE(req: NextApiRequest, res: NextApiResponse) { const {id} = req.query; await db.collection("assignments").deleteOne( - { _id: new ObjectId(id as string) } + { id: id as string } ); res.status(200).json({ok: true}); @@ -47,7 +46,7 @@ async function PATCH(req: NextApiRequest, res: NextApiResponse) { const {id} = req.query; await db.collection("assignments").updateOne( - { _id: new ObjectId(id as string) }, + { id: id as string }, { $set: {assigner: req.session.user?.id, ...req.body} } ); diff --git a/src/pages/api/assignments/[id]/release.ts b/src/pages/api/assignments/[id]/release.ts index 4d8b2ae7..b9a4d357 100644 --- a/src/pages/api/assignments/[id]/release.ts +++ b/src/pages/api/assignments/[id]/release.ts @@ -12,7 +12,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { // verify if it's a logged user that is trying to archive if (req.session.user) { const { id } = req.query as { id: string }; - const doc = await db.collection("assignments").findOne({ _id: new ObjectId(id) }); + const doc = await db.collection("assignments").findOne({ id: id }); if (!doc) { res.status(404).json({ ok: false }); @@ -20,10 +20,10 @@ async function post(req: NextApiRequest, res: NextApiResponse) { } await db.collection("assignments").updateOne( - { _id: new ObjectId(id) }, + { id: id }, { $set: { released: true } } ); - + res.status(200).json({ ok: true }); return; } diff --git a/src/pages/api/assignments/[id]/start.ts b/src/pages/api/assignments/[id]/start.ts index a85c7b88..bc29cdfd 100644 --- a/src/pages/api/assignments/[id]/start.ts +++ b/src/pages/api/assignments/[id]/start.ts @@ -1,7 +1,6 @@ import type { NextApiRequest, NextApiResponse } from "next"; import moment from "moment"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; @@ -13,7 +12,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { // verify if it's a logged user that is trying to archive if (req.session.user) { const { id } = req.query as { id: string }; - const data = await db.collection("assignments").findOne({ _id: new ObjectId(id) }); + const data = await db.collection("assignments").findOne({ id: id }); if (!data) { res.status(404).json({ ok: false }); @@ -28,7 +27,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { } await db.collection("assignments").updateOne( - { _id: new ObjectId(id) }, + { id: id }, { $set: { start: true } } ); diff --git a/src/pages/api/assignments/[id]/unarchive.tsx b/src/pages/api/assignments/[id]/unarchive.tsx index 10321c05..7a906e58 100644 --- a/src/pages/api/assignments/[id]/unarchive.tsx +++ b/src/pages/api/assignments/[id]/unarchive.tsx @@ -1,6 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; @@ -12,7 +11,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { // verify if it's a logged user that is trying to archive if (req.session.user) { const { id } = req.query as { id: string }; - const docSnap = await db.collection("assignments").findOne({ _id: new ObjectId(id) }); + const docSnap = await db.collection("assignments").findOne({ id: id }); if (!docSnap) { res.status(404).json({ ok: false }); @@ -20,7 +19,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { } await db.collection("assignments").updateOne( - { _id: new ObjectId(id) }, + { id: id }, { $set: { archived: false } } ); diff --git a/src/pages/api/assignments/index.ts b/src/pages/api/assignments/index.ts index 44e11240..35819b58 100644 --- a/src/pages/api/assignments/index.ts +++ b/src/pages/api/assignments/index.ts @@ -1,7 +1,6 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type {NextApiRequest, NextApiResponse} from "next"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import {withIronSessionApiRoute} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; import {uuidv4} from "@firebase/util"; @@ -130,7 +129,7 @@ async function POST(req: NextApiRequest, res: NextApiResponse) { } await db.collection("assignments").insertOne({ - _id: new ObjectId(uuidv4()), + id: uuidv4(), assigner: req.session.user?.id, assignees, results: [], @@ -143,7 +142,7 @@ async function POST(req: NextApiRequest, res: NextApiResponse) { for (const assigneeID of assignees) { - const assignee = await db.collection("users").findOne({ _id: new ObjectId(assigneeID) }); + const assignee = await db.collection("users").findOne({ id: assigneeID }); if (!assignee) continue; const name = body.name; diff --git a/src/pages/api/code/[id].ts b/src/pages/api/code/[id].ts index 19f6c5f9..f9f2cb21 100644 --- a/src/pages/api/code/[id].ts +++ b/src/pages/api/code/[id].ts @@ -1,7 +1,6 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type {NextApiRequest, NextApiResponse} from "next"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import {withIronSessionApiRoute} from "iron-session/next"; import {sessionOptions} from "@/lib/session"; import {uuidv4} from "@firebase/util"; @@ -17,17 +16,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) async function GET(req: NextApiRequest, res: NextApiResponse) { const {id} = req.query; - const code = await db.collection("codes").findOne({ _id: new ObjectId(id as string) }); + const code = await db.collection("codes").findOne({ id: id as string }); res.status(200).json(code); } async function DELETE(req: NextApiRequest, res: NextApiResponse) { const {id} = req.query; - const code = await db.collection("codes").findOne({ _id: new ObjectId(id as string) }); + const code = await db.collection("codes").findOne({ id: id as string }); if (!code) return res.status(404).json; - await db.collection("codes").deleteOne({ _id: new ObjectId(id as string) }); - + await db.collection("codes").deleteOne({ id: id as string }); + res.status(200).json(code); } diff --git a/src/pages/api/code/index.ts b/src/pages/api/code/index.ts index 24c518d7..5030bc34 100644 --- a/src/pages/api/code/index.ts +++ b/src/pages/api/code/index.ts @@ -1,7 +1,6 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from "next"; import client from "@/lib/mongodb"; -import { ObjectId } from 'mongodb'; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; import { Code, Group, Type } from "@/interfaces/user"; @@ -76,7 +75,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { } const codePromises = codes.map(async (code, index) => { - const codeRef = await db.collection("codes").findOne({ _id: new ObjectId(code) }); + const codeRef = await db.collection("codes").findOne({ id: code }); let codeInformation = { type, code, @@ -106,7 +105,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { if (!previousCode && codeRef) { await db.collection("codes").updateOne( - { _id: new ObjectId(codeRef._id) }, + { id: codeRef.id }, { $set: { ...codeInformation, @@ -125,7 +124,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { } else { // upsert: true -> if it doesnt exist insert await db.collection("codes").updateOne( - { _id: new ObjectId(code) }, + { id: code }, { $set: codeInformation }, { upsert: true } ); @@ -146,10 +145,10 @@ async function del(req: NextApiRequest, res: NextApiResponse) { const codes = req.query.code as string[]; for (const code of codes) { - const snapshot = await db.collection("codes").findOne({ _id: new ObjectId(code as string) }); + const snapshot = await db.collection("codes").findOne({ id: code as string }); if (!snapshot) continue; - await db.collection("codes").deleteOne({ _id: snapshot._id }); + await db.collection("codes").deleteOne({ id: snapshot.id }); } res.status(200).json({ codes }); diff --git a/src/pages/api/discounts/[id].ts b/src/pages/api/discounts/[id].ts index 9683f7a6..1bef4c97 100644 --- a/src/pages/api/discounts/[id].ts +++ b/src/pages/api/discounts/[id].ts @@ -1,18 +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, - doc, - getDoc, - deleteDoc, - setDoc, -} from "firebase/firestore"; +import client from "@/lib/mongodb"; import { withIronSessionApiRoute } from "iron-session/next"; import { sessionOptions } from "@/lib/session"; import { PERMISSIONS } from "@/constants/userPermissions"; -const db = getFirestore(app); +const db = client.db(process.env.MONGODB_DB); export default withIronSessionApiRoute(handler, sessionOptions); @@ -29,15 +22,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) { } const { id } = req.query as { id: string }; + const docSnap = await db.collection("discounts").findOne({ id: id }); - const docRef = doc(db, "discounts", id); - const docSnap = await getDoc(docRef); - - if (docSnap.exists()) { - res.status(200).json({ - id: docSnap.id, - ...docSnap.data(), - }); + if (docSnap) { + res.status(200).json(docSnap); } else { res.status(404).json(undefined); } @@ -50,18 +38,19 @@ async function patch(req: NextApiRequest, res: NextApiResponse) { } const { id } = req.query as { id: string }; + const docSnap = await db.collection("discounts").findOne({ id: id }); - const docRef = doc(db, "discounts", id); - const docSnap = await getDoc(docRef); - - if (docSnap.exists()) { + if (docSnap) { if (!["developer", "admin"].includes(req.session.user.type)) { res.status(403).json({ ok: false }); return; } - await setDoc(docRef, req.body, { merge: true }); - + await db.collection("discounts").updateOne( + { id: id }, + { $set: req.body } + ); + res.status(200).json({ ok: true }); } else { res.status(404).json({ ok: false }); @@ -75,17 +64,15 @@ async function del(req: NextApiRequest, res: NextApiResponse) { } const { id } = req.query as { id: string }; + const docSnap = await db.collection("discounts").findOne({ id: id }); - const docRef = doc(db, "discounts", id); - const docSnap = await getDoc(docRef); - - if (docSnap.exists()) { + if (docSnap) { if (!["developer", "admin"].includes(req.session.user.type)) { res.status(403).json({ ok: false }); return; } - await deleteDoc(docRef); + await db.collection("discounts").deleteOne({ id: id }); res.status(200).json({ ok: true }); } else { diff --git a/src/pages/api/discounts/index.ts b/src/pages/api/discounts/index.ts index c5d768d5..018c17f9 100644 --- a/src/pages/api/discounts/index.ts +++ b/src/pages/api/discounts/index.ts @@ -1,22 +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, - setDoc, - doc, - getDoc, - deleteDoc, -} 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 { Discount, Package } from "@/interfaces/paypal"; import { v4 } from "uuid"; -const db = getFirestore(app); +const db = client.db(process.env.MONGODB_DB); export default withIronSessionApiRoute(handler, sessionOptions); @@ -32,14 +23,8 @@ async function get(req: NextApiRequest, res: NextApiResponse) { return; } - const snapshot = await getDocs(collection(db, "discounts")); - - res.status(200).json( - snapshot.docs.map((doc) => ({ - id: doc.id, - ...doc.data(), - })), - ); + const snapshot = await db.collection("discounts").find({}).toArray(); + res.status(200).json(snapshot); } async function post(req: NextApiRequest, res: NextApiResponse) { @@ -56,7 +41,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const body = req.body as Discount; - await setDoc(doc(db, "discounts", v4()), body); + await db.collection("discounts").insertOne({ ...body }); + res.status(200).json({ ok: true }); } @@ -71,10 +57,10 @@ async function del(req: NextApiRequest, res: NextApiResponse) { const discounts = req.query.discount as string[]; for (const discount of discounts) { - const snapshot = await getDoc(doc(db, "discounts", discount as string)); - if (!snapshot.exists()) continue; + const snapshot = await db.collection("discounts").findOne({ id: discount as string }); + if (!snapshot) continue; - await deleteDoc(snapshot.ref); + await db.collection("discounts").deleteOne({ id: discount as string }); } res.status(200).json({ discounts });