Refactored discounts and replaced my previous commit id queries to use id not _id

This commit is contained in:
Carlos Mesquita
2024-09-07 16:03:26 +01:00
parent 171231cd21
commit c07e3f86fb
13 changed files with 65 additions and 104 deletions

View File

@@ -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<AssignmentData>({ _id: new ObjectId(id) });
const assignment = await db.collection("assignments").findOne<AssignmentData>({ 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<User>({
id: { $in: objectIds }
}).toArray();
const user = await db.collection("users").findOne<User>({ _id: new ObjectId(assignment.assigner) });
const user = await db.collection("users").findOne<User>({ 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: {

View File

@@ -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<User>({ _id: new ObjectId(req.session.user.id) });
const user = await db.collection("users").findOne<User>({ 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<User>({
_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<User>({ _id: new ObjectId(data.assigner) });
const assignerUser = await db.collection("users").findOne<User>({ 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<CorporateUser>({ _id: { $in: groups.map(g => g.admin).map(id => new ObjectId(id))} })
.find<CorporateUser>({ 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;

View File

@@ -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 });

View File

@@ -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} }
);

View File

@@ -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;
}

View File

@@ -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 } }
);

View File

@@ -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 } }
);

View File

@@ -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<User>({ _id: new ObjectId(assigneeID) });
const assignee = await db.collection("users").findOne<User>({ id: assigneeID });
if (!assignee) continue;
const name = body.name;

View File

@@ -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);
}

View File

@@ -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<Code>({ _id: new ObjectId(code) });
const codeRef = await db.collection("codes").findOne<Code>({ 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<Code>({ _id: new ObjectId(code as string) });
const snapshot = await db.collection("codes").findOne<Code>({ 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 });

View File

@@ -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 {

View File

@@ -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 });