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;