Solved the same bug but for the e-mail
This commit is contained in:
@@ -1,14 +1,23 @@
|
|||||||
// 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 {getFirestore, collection, getDocs, query, where, setDoc, doc, getDoc} from "firebase/firestore";
|
import {
|
||||||
|
getFirestore,
|
||||||
|
collection,
|
||||||
|
getDocs,
|
||||||
|
query,
|
||||||
|
where,
|
||||||
|
setDoc,
|
||||||
|
doc,
|
||||||
|
getDoc,
|
||||||
|
} from "firebase/firestore";
|
||||||
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 { Module } from "@/interfaces";
|
||||||
import { getExams } from "@/utils/exams.be";
|
import { getExams } from "@/utils/exams.be";
|
||||||
import { Exam, Variant } from "@/interfaces/exam";
|
import { Exam, Variant } from "@/interfaces/exam";
|
||||||
import {capitalize, flatten} from "lodash";
|
import { capitalize, flatten, uniqBy } from "lodash";
|
||||||
import { User } from "@/interfaces/user";
|
import { User } from "@/interfaces/user";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import { sendEmail } from "@/email";
|
import { sendEmail } from "@/email";
|
||||||
@@ -60,10 +69,17 @@ const generateExams = async (
|
|||||||
): Promise<ExamWithUser[]> => {
|
): Promise<ExamWithUser[]> => {
|
||||||
if (generateMultiple) {
|
if (generateMultiple) {
|
||||||
// for optimization purposes, it would be better to create a new endpoint that returned the answers for all users at once
|
// 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 allExams = assignees.map(async (assignee) => {
|
||||||
const selectedModulePromises = await selectedModules.map(async (module: Module) => {
|
const selectedModulePromises = selectedModules.map(
|
||||||
|
async (module: Module) => {
|
||||||
try {
|
try {
|
||||||
const exams: Exam[] = await getExams(db, module, "true", assignee, variant);
|
const exams: Exam[] = await getExams(
|
||||||
|
db,
|
||||||
|
module,
|
||||||
|
"true",
|
||||||
|
assignee,
|
||||||
|
variant,
|
||||||
|
);
|
||||||
|
|
||||||
const exam = exams[getRandomIndex(exams)];
|
const exam = exams[getRandomIndex(exams)];
|
||||||
if (exam) {
|
if (exam) {
|
||||||
@@ -74,17 +90,21 @@ const generateExams = async (
|
|||||||
console.error(e);
|
console.error(e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}, []);
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
const newModules = await Promise.all(selectedModulePromises);
|
const newModules = await Promise.all(selectedModulePromises);
|
||||||
|
|
||||||
return newModules;
|
return newModules;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const exams = flatten(await Promise.all(allExams)).filter((x) => x !== null) as ExamWithUser[];
|
const exams = flatten(await Promise.all(allExams)).filter(
|
||||||
|
(x) => x !== null,
|
||||||
|
) as ExamWithUser[];
|
||||||
return exams;
|
return exams;
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedModulePromises = await selectedModules.map(async (module: Module) => {
|
const selectedModulePromises = selectedModules.map(async (module: Module) => {
|
||||||
const exams: Exam[] = await getExams(db, module, "false", undefined);
|
const exams: Exam[] = await getExams(db, module, "false", undefined);
|
||||||
const exam = exams[getRandomIndex(exams)];
|
const exam = exams[getRandomIndex(exams)];
|
||||||
|
|
||||||
@@ -96,7 +116,11 @@ const generateExams = async (
|
|||||||
|
|
||||||
const exams = await Promise.all(selectedModulePromises);
|
const exams = await Promise.all(selectedModulePromises);
|
||||||
const examesFiltered = exams.filter((x) => x !== null) as ExamWithUser[];
|
const examesFiltered = exams.filter((x) => x !== null) as ExamWithUser[];
|
||||||
return flatten(assignees.map((assignee) => examesFiltered.map((exam) => ({...exam, assignee}))));
|
return flatten(
|
||||||
|
assignees.map((assignee) =>
|
||||||
|
examesFiltered.map((exam) => ({ ...exam, assignee })),
|
||||||
|
),
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||||
@@ -118,10 +142,17 @@ async function POST(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
variant?: Variant;
|
variant?: Variant;
|
||||||
};
|
};
|
||||||
|
|
||||||
const exams: ExamWithUser[] = await generateExams(generateMultiple, selectedModules, assignees, variant);
|
const exams: ExamWithUser[] = await generateExams(
|
||||||
|
generateMultiple,
|
||||||
|
selectedModules,
|
||||||
|
assignees,
|
||||||
|
variant,
|
||||||
|
);
|
||||||
|
|
||||||
if (exams.length === 0) {
|
if (exams.length === 0) {
|
||||||
res.status(400).json({ok: false, error: "No exams found for the selected modules"});
|
res
|
||||||
|
.status(400)
|
||||||
|
.json({ ok: false, error: "No exams found for the selected modules" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,13 +173,24 @@ async function POST(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const assignee = { id: assigneeID, ...assigneeSnapshot.data() } as User;
|
const assignee = { id: assigneeID, ...assigneeSnapshot.data() } as User;
|
||||||
const name = body.name;
|
const name = body.name;
|
||||||
const teacher = req.session.user!;
|
const teacher = req.session.user!;
|
||||||
const examModulesLabel = exams.map((x) => capitalize(x.module)).join(", ");
|
const examModulesLabel = uniqBy(exams, (x) => x.module)
|
||||||
|
.map((x) => capitalize(x.module))
|
||||||
|
.join(", ");
|
||||||
const startDate = moment(body.startDate).format("DD/MM/YYYY - HH:mm");
|
const startDate = moment(body.startDate).format("DD/MM/YYYY - HH:mm");
|
||||||
const endDate = moment(body.endDate).format("DD/MM/YYYY - HH:mm");
|
const endDate = moment(body.endDate).format("DD/MM/YYYY - HH:mm");
|
||||||
|
|
||||||
await sendEmail(
|
await sendEmail(
|
||||||
"assignment",
|
"assignment",
|
||||||
{user: {name: assignee.name}, assignment: {name, startDate, endDate, modules: examModulesLabel, assigner: teacher.name}},
|
{
|
||||||
|
user: { name: assignee.name },
|
||||||
|
assignment: {
|
||||||
|
name,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
modules: examModulesLabel,
|
||||||
|
assigner: teacher.name,
|
||||||
|
},
|
||||||
|
},
|
||||||
[assignee.email],
|
[assignee.email],
|
||||||
"EnCoach - New Assignment!",
|
"EnCoach - New Assignment!",
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user