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