diff --git a/src/pages/(generation)/LevelGeneration.tsx b/src/pages/(generation)/LevelGeneration.tsx index 480ca61a..168dedf4 100644 --- a/src/pages/(generation)/LevelGeneration.tsx +++ b/src/pages/(generation)/LevelGeneration.tsx @@ -341,8 +341,6 @@ const LevelGeneration = ({id}: Props) => { } if (part.type === "blank_space_text") { - console.log({currentExercise}); - const exercise: WriteBlanksExercise = { id: v4(), prompt: "Complete the text below.", diff --git a/src/pages/api/exam/[module]/index.ts b/src/pages/api/exam/[module]/index.ts index 0c5af8de..02ddf923 100644 --- a/src/pages/api/exam/[module]/index.ts +++ b/src/pages/api/exam/[module]/index.ts @@ -1,89 +1,70 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from "next"; -import { app } from "@/firebase"; -import { - getFirestore, - setDoc, - doc, - runTransaction, - collection, - query, - where, - getDocs, -} from "firebase/firestore"; -import { withIronSessionApiRoute } from "iron-session/next"; -import { sessionOptions } from "@/lib/session"; -import { Exam, InstructorGender, Variant } from "@/interfaces/exam"; -import { getExams } from "@/utils/exams.be"; -import { Module } from "@/interfaces"; +import type {NextApiRequest, NextApiResponse} from "next"; +import {app} from "@/firebase"; +import {getFirestore, setDoc, doc, runTransaction, collection, query, where, getDocs} from "firebase/firestore"; +import {withIronSessionApiRoute} from "iron-session/next"; +import {sessionOptions} from "@/lib/session"; +import {Exam, InstructorGender, Variant} from "@/interfaces/exam"; +import {getExams} from "@/utils/exams.be"; +import {Module} from "@/interfaces"; const db = getFirestore(app); export default withIronSessionApiRoute(handler, sessionOptions); async function handler(req: NextApiRequest, res: NextApiResponse) { - if (req.method === "GET") return await GET(req, res); - if (req.method === "POST") return await POST(req, res); + if (req.method === "GET") return await GET(req, res); + if (req.method === "POST") return await POST(req, res); - res.status(404).json({ ok: false }); + res.status(404).json({ok: false}); } 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 { module, avoidRepeated, variant, instructorGender } = req.query as { - module: Module; - avoidRepeated: string; - variant?: Variant; - instructorGender?: InstructorGender; - }; + const {module, avoidRepeated, variant, instructorGender} = req.query as { + module: Module; + avoidRepeated: string; + variant?: Variant; + instructorGender?: InstructorGender; + }; - const exams: Exam[] = await getExams( - db, - module, - avoidRepeated, - req.session.user.id, - variant, - instructorGender - ); - res.status(200).json(exams); + const exams: Exam[] = await getExams(db, module, avoidRepeated, req.session.user.id, variant, instructorGender); + res.status(200).json(exams); } async function POST(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; + } - if (req.session.user.type !== "developer") { - res.status(403).json({ ok: false }); - return; - } - const { module } = req.query as { module: string }; + const {module} = req.query as {module: string}; - try { - const exam = { - ...req.body, - module: module, - createdBy: req.session.user.id, - createdAt: new Date().toISOString(), - }; - await runTransaction(db, async (transaction) => { - const docRef = doc(db, module, req.body.id); - const docSnap = await transaction.get(docRef); + try { + const exam = { + ...req.body, + module: module, + createdBy: req.session.user.id, + createdAt: new Date().toISOString(), + }; - if (docSnap.exists()) { - throw new Error("Name already exists"); - } + await runTransaction(db, async (transaction) => { + const docRef = doc(db, module, req.body.id); + const docSnap = await transaction.get(docRef); - const newDocRef = doc(db, module, req.body.id); - transaction.set(newDocRef, exam); - }); - res.status(200).json(exam); - } catch (error) { - console.error("Transaction failed: ", error); - res.status(500).json({ ok: false, error: (error as any).message }); - } + if (docSnap.exists()) { + throw new Error("Name already exists"); + } + + const newDocRef = doc(db, module, req.body.id); + transaction.set(newDocRef, exam); + }); + res.status(200).json(exam); + } catch (error) { + console.error("Transaction failed: ", error); + res.status(500).json({ok: false, error: (error as any).message}); + } }