49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { app } from "@/firebase";
|
|
import { withIronSessionApiRoute } from "iron-session/next";
|
|
import { sessionOptions } from "@/lib/session";
|
|
import { CorporateUser, Group } from "@/interfaces/user";
|
|
import { Discount, Package } from "@/interfaces/paypal";
|
|
import { v4 } from "uuid";
|
|
import { checkAccess } from "@/utils/permissions";
|
|
import { CEFR_STEPS } from "@/resources/grading";
|
|
import { getCorporateUser } from "@/resources/user";
|
|
import { getUserCorporate } from "@/utils/groups.be";
|
|
import { Grading, Step } from "@/interfaces";
|
|
import { getGroupsForUser } from "@/utils/groups.be";
|
|
import { uniq } from "lodash";
|
|
import { getSpecificUsers, getUser } from "@/utils/users.be";
|
|
import client from "@/lib/mongodb";
|
|
import { getGradingSystemByEntity } from "@/utils/grading.be";
|
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method === "POST") await post(req, res);
|
|
}
|
|
|
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
if (!req.session.user) {
|
|
res.status(401).json({ ok: false });
|
|
return;
|
|
}
|
|
|
|
if (!checkAccess(req.session.user, ["admin", "developer", "mastercorporate", "corporate"]))
|
|
return res.status(403).json({
|
|
ok: false,
|
|
reason: "You do not have permission to create a new grading system",
|
|
});
|
|
|
|
const body = req.body as {
|
|
entities: string[]
|
|
steps: Step[];
|
|
};
|
|
|
|
await db.collection("grading").updateMany({ entity: { $in: body.entities } }, { $set: { steps: body.steps } }, { upsert: true });
|
|
|
|
res.status(200).json({ ok: true });
|
|
}
|