Assignments now generate unique list of exams for each user
This commit is contained in:
@@ -48,20 +48,22 @@ export default function AssignmentCreator({isCreating, assignment, assigner, gro
|
|||||||
const createAssignment = () => {
|
const createAssignment = () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
const examPromises = selectedModules.map(async (module) => getExam(module, false));
|
(assignment ? axios.patch : axios.post)(
|
||||||
Promise.all(examPromises)
|
`/api/assignments${assignment ? `/${assignment.id}` : ""}`,
|
||||||
.then((exams) => {
|
{
|
||||||
(assignment ? axios.patch : axios.post)(`/api/assignments${assignment ? `/${assignment.id}` : ""}`, {
|
|
||||||
assigner,
|
|
||||||
assignees,
|
assignees,
|
||||||
name,
|
name,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
results: [],
|
selectedModules
|
||||||
exams: exams.map((e) => ({module: e?.module, id: e?.id})),
|
}
|
||||||
})
|
)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success(`The assignment "${name}" has been ${assignment ? "updated" : "created"} successfully!`);
|
toast.success(
|
||||||
|
`The assignment "${name}" has been ${
|
||||||
|
assignment ? "updated" : "created"
|
||||||
|
} successfully!`
|
||||||
|
);
|
||||||
cancelCreation();
|
cancelCreation();
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
@@ -69,12 +71,6 @@ export default function AssignmentCreator({isCreating, assignment, assigner, gro
|
|||||||
toast.error("Something went wrong, please try again later!");
|
toast.error("Something went wrong, please try again later!");
|
||||||
})
|
})
|
||||||
.finally(() => setIsLoading(false));
|
.finally(() => setIsLoading(false));
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
console.log(e);
|
|
||||||
toast.error("Something went wrong, please try again later!");
|
|
||||||
setIsLoading(false);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteAssignment = () => {
|
const deleteAssignment = () => {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export interface Assignment {
|
|||||||
type: "academic" | "general";
|
type: "academic" | "general";
|
||||||
stats: Stat[];
|
stats: Stat[];
|
||||||
}[];
|
}[];
|
||||||
exams: {id: string; module: Module}[];
|
exams: {id: string; module: Module, assignee: string}[];
|
||||||
startDate: Date;
|
startDate: Date;
|
||||||
endDate: Date;
|
endDate: Date;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ import {getFirestore, collection, getDocs, query, where, setDoc, doc} from "fire
|
|||||||
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";
|
||||||
|
import { Module } from "@/interfaces";
|
||||||
|
import { getExams } from "@/utils/exams.be";
|
||||||
|
import { Exam } from "@/interfaces/exam";
|
||||||
|
import { flatten } from "lodash";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
@@ -34,8 +38,65 @@ async function GET(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
res.status(200).json(docs);
|
res.status(200).json(docs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ExamWithUser {
|
||||||
|
module: Module;
|
||||||
|
id: string;
|
||||||
|
assignee: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRandomIndex(arr: any[]): number {
|
||||||
|
const randomIndex = Math.floor(Math.random() * arr.length);
|
||||||
|
return randomIndex;
|
||||||
|
}
|
||||||
|
|
||||||
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||||
await setDoc(doc(db, "assignments", uuidv4()), {assigner: req.session.user?.id, ...req.body});
|
const { selectedModules, assignees, ...body } = req.body as {
|
||||||
|
selectedModules: Module[];
|
||||||
|
assignees: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
// for optimization purposes, it would be better to create a new endpoint that returned the answers for all users at once
|
||||||
|
const allExams = await assignees.map(async (assignee) => {
|
||||||
|
const selectedModulePromises = await selectedModules.map(
|
||||||
|
async (module: Module) => {
|
||||||
|
try {
|
||||||
|
const exams: Exam[] = await getExams(db, module, "true", assignee);
|
||||||
|
|
||||||
|
const exam = exams[getRandomIndex(exams)];
|
||||||
|
if (exam) {
|
||||||
|
return { module: exam.module, id: exam.id, assignee };
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
const newModules = await Promise.all(selectedModulePromises);
|
||||||
|
|
||||||
|
return newModules;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const exams = flatten(await Promise.all(allExams)).filter(
|
||||||
|
(x) => x !== null
|
||||||
|
) as ExamWithUser[];
|
||||||
|
|
||||||
|
if (exams.length === 0) {
|
||||||
|
res
|
||||||
|
.status(400)
|
||||||
|
.json({ ok: false, error: "No exams found for the selected modules" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await setDoc(doc(db, "assignments", uuidv4()), {
|
||||||
|
assigner: req.session.user?.id,
|
||||||
|
assignees,
|
||||||
|
results: [],
|
||||||
|
exams,
|
||||||
|
...body,
|
||||||
|
});
|
||||||
|
|
||||||
res.status(200).json({ ok: true });
|
res.status(200).json({ ok: true });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user