Updated the Grading System
This commit is contained in:
@@ -1,82 +1,78 @@
|
||||
// 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 { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { PERMISSIONS } from "@/constants/userPermissions";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") return get(req, res);
|
||||
if (req.method === "DELETE") return del(req, res);
|
||||
if (req.method === "PATCH") return patch(req, res);
|
||||
if (req.method === "GET") return get(req, res);
|
||||
if (req.method === "DELETE") return del(req, res);
|
||||
if (req.method === "PATCH") return patch(req, res);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const docSnap = await db.collection("discounts").findOne({ id: id });
|
||||
const {id} = req.query as {id: string};
|
||||
const docSnap = await db.collection("discounts").findOne({id: id});
|
||||
|
||||
if (docSnap) {
|
||||
res.status(200).json(docSnap);
|
||||
} else {
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
if (docSnap) {
|
||||
res.status(200).json(docSnap);
|
||||
} else {
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async function patch(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const docSnap = await db.collection("discounts").findOne({ id: id });
|
||||
const {id} = req.query as {id: string};
|
||||
const docSnap = await db.collection("discounts").findOne({id: id});
|
||||
|
||||
if (docSnap) {
|
||||
if (!["developer", "admin"].includes(req.session.user.type)) {
|
||||
res.status(403).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
if (docSnap) {
|
||||
if (!["developer", "admin"].includes(req.session.user.type)) {
|
||||
res.status(403).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("discounts").updateOne(
|
||||
{ id: id },
|
||||
{ $set: { id: id, ...req.body},
|
||||
{ upsert: true }
|
||||
);
|
||||
await db.collection("discounts").updateOne({id: id}, {$set: {id: id, ...req.body}}, {upsert: true});
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
} else {
|
||||
res.status(404).json({ ok: false });
|
||||
}
|
||||
res.status(200).json({ok: true});
|
||||
} else {
|
||||
res.status(404).json({ok: false});
|
||||
}
|
||||
}
|
||||
|
||||
async function del(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const docSnap = await db.collection("discounts").findOne({ id: id });
|
||||
const {id} = req.query as {id: string};
|
||||
const docSnap = await db.collection("discounts").findOne({id: id});
|
||||
|
||||
if (docSnap) {
|
||||
if (!["developer", "admin"].includes(req.session.user.type)) {
|
||||
res.status(403).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
if (docSnap) {
|
||||
if (!["developer", "admin"].includes(req.session.user.type)) {
|
||||
res.status(403).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
await db.collection("discounts").deleteOne({ id: id });
|
||||
await db.collection("discounts").deleteOne({id: id});
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
} else {
|
||||
res.status(404).json({ ok: false });
|
||||
}
|
||||
res.status(200).json({ok: true});
|
||||
} else {
|
||||
res.status(404).json({ok: false});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
import { app } from "@/firebase";
|
||||
import { getFirestore, doc, getDoc } from "firebase/firestore";
|
||||
import { CEFR_STEPS } from "@/resources/grading";
|
||||
import { getUserCorporate } from "@/utils/groups.be";
|
||||
import { User } from "@/interfaces/user";
|
||||
import { Grading } from "@/interfaces";
|
||||
const db = getFirestore(app);
|
||||
import {app} from "@/firebase";
|
||||
import {getFirestore, doc, getDoc} from "firebase/firestore";
|
||||
import {CEFR_STEPS} from "@/resources/grading";
|
||||
import {getUserCorporate} from "@/utils/groups.be";
|
||||
import {User} from "@/interfaces/user";
|
||||
import {Grading} from "@/interfaces";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export const getGradingSystem = async (user: User): Promise<Grading> => {
|
||||
const snapshot = await getDoc(doc(db, "grading", user.id));
|
||||
if (snapshot.exists()) return snapshot.data() as Grading;
|
||||
const grading = await db.collection("grading").findOne<Grading>({id: user.id});
|
||||
if (!!grading) return grading;
|
||||
|
||||
if (user.type !== "teacher" && user.type !== "student")
|
||||
return { steps: CEFR_STEPS, user: user.id };
|
||||
if (user.type !== "teacher" && user.type !== "student") return {steps: CEFR_STEPS, user: user.id};
|
||||
|
||||
const corporate = await getUserCorporate(user.id);
|
||||
if (!corporate) return { steps: CEFR_STEPS, user: user.id };
|
||||
const corporate = await getUserCorporate(user.id);
|
||||
if (!corporate) return {steps: CEFR_STEPS, user: user.id};
|
||||
|
||||
const corporateSnapshot = await getDoc(doc(db, "grading", corporate.id));
|
||||
if (corporateSnapshot.exists()) return corporateSnapshot.data() as Grading;
|
||||
const corporateSnapshot = await db.collection("grading").findOne<Grading>({id: corporate.id});
|
||||
if (!!corporateSnapshot) return corporateSnapshot;
|
||||
|
||||
return { steps: CEFR_STEPS, user: user.id };
|
||||
return {steps: CEFR_STEPS, user: user.id};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user