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