Removed unnecessary code
This commit is contained in:
@@ -341,8 +341,6 @@ const LevelGeneration = ({id}: Props) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (part.type === "blank_space_text") {
|
if (part.type === "blank_space_text") {
|
||||||
console.log({currentExercise});
|
|
||||||
|
|
||||||
const exercise: WriteBlanksExercise = {
|
const exercise: WriteBlanksExercise = {
|
||||||
id: v4(),
|
id: v4(),
|
||||||
prompt: "Complete the text below.",
|
prompt: "Complete the text below.",
|
||||||
|
|||||||
@@ -1,89 +1,70 @@
|
|||||||
// 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 {app} from "@/firebase";
|
||||||
import {
|
import {getFirestore, setDoc, doc, runTransaction, collection, query, where, getDocs} from "firebase/firestore";
|
||||||
getFirestore,
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
setDoc,
|
import {sessionOptions} from "@/lib/session";
|
||||||
doc,
|
import {Exam, InstructorGender, Variant} from "@/interfaces/exam";
|
||||||
runTransaction,
|
import {getExams} from "@/utils/exams.be";
|
||||||
collection,
|
import {Module} from "@/interfaces";
|
||||||
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);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (req.method === "GET") return await GET(req, res);
|
if (req.method === "GET") return await GET(req, res);
|
||||||
if (req.method === "POST") return await POST(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) {
|
async function GET(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ ok: false });
|
res.status(401).json({ok: false});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { module, avoidRepeated, variant, instructorGender } = req.query as {
|
const {module, avoidRepeated, variant, instructorGender} = req.query as {
|
||||||
module: Module;
|
module: Module;
|
||||||
avoidRepeated: string;
|
avoidRepeated: string;
|
||||||
variant?: Variant;
|
variant?: Variant;
|
||||||
instructorGender?: InstructorGender;
|
instructorGender?: InstructorGender;
|
||||||
};
|
};
|
||||||
|
|
||||||
const exams: Exam[] = await getExams(
|
const exams: Exam[] = await getExams(db, module, avoidRepeated, req.session.user.id, variant, instructorGender);
|
||||||
db,
|
res.status(200).json(exams);
|
||||||
module,
|
|
||||||
avoidRepeated,
|
|
||||||
req.session.user.id,
|
|
||||||
variant,
|
|
||||||
instructorGender
|
|
||||||
);
|
|
||||||
res.status(200).json(exams);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ ok: false });
|
res.status(401).json({ok: false});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.session.user.type !== "developer") {
|
const {module} = req.query as {module: string};
|
||||||
res.status(403).json({ ok: false });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { module } = req.query as { module: string };
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const exam = {
|
const exam = {
|
||||||
...req.body,
|
...req.body,
|
||||||
module: module,
|
module: module,
|
||||||
createdBy: req.session.user.id,
|
createdBy: req.session.user.id,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
await runTransaction(db, async (transaction) => {
|
|
||||||
const docRef = doc(db, module, req.body.id);
|
|
||||||
const docSnap = await transaction.get(docRef);
|
|
||||||
|
|
||||||
if (docSnap.exists()) {
|
await runTransaction(db, async (transaction) => {
|
||||||
throw new Error("Name already exists");
|
const docRef = doc(db, module, req.body.id);
|
||||||
}
|
const docSnap = await transaction.get(docRef);
|
||||||
|
|
||||||
const newDocRef = doc(db, module, req.body.id);
|
if (docSnap.exists()) {
|
||||||
transaction.set(newDocRef, exam);
|
throw new Error("Name already exists");
|
||||||
});
|
}
|
||||||
res.status(200).json(exam);
|
|
||||||
} catch (error) {
|
const newDocRef = doc(db, module, req.body.id);
|
||||||
console.error("Transaction failed: ", error);
|
transaction.set(newDocRef, exam);
|
||||||
res.status(500).json({ ok: false, error: (error as any).message });
|
});
|
||||||
}
|
res.status(200).json(exam);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Transaction failed: ", error);
|
||||||
|
res.status(500).json({ok: false, error: (error as any).message});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user