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