Added level export to excel

This commit is contained in:
Joao Ramos
2024-09-05 23:30:03 +01:00
parent a61ad2cc7e
commit e433a150a9
4 changed files with 104 additions and 43 deletions

View File

@@ -11,7 +11,11 @@ import { getSpecificUsers } from "@/utils/users.be";
import { checkAccess } from "@/utils/permissions";
import { getAssignmentsForCorporates } from "@/utils/assignments.be";
import { search } from "@/utils/search";
import { getGradingSystem } from "@/utils/grading.be";
import { Exam } from "@/interfaces/exam";
import { User } from "@/interfaces/user";
import { calculateBandScore, getGradingLabel } from "@/utils/score";
import { Module } from "@/interfaces";
const db = getFirestore(app);
export default withIronSessionApiRoute(handler, sessionOptions);
@@ -25,6 +29,7 @@ interface TableData {
date: moment.Moment;
assignment: string;
corporateId: string;
level: string;
}
async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -59,7 +64,42 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
const assignmentUsers = [
...new Set(assignments.flatMap((a) => a.assignees)),
];
const assigners = [...new Set(assignments.map((a) => a.assigner))];
const users = await getSpecificUsers(assignmentUsers);
const assignerUsers = await getSpecificUsers(assigners);
const assignerUsersGradingSystems = await Promise.all(
assignerUsers.map(async (user: User) => {
const data = await getGradingSystem(user);
// in this context I need to override as I'll have to match to the assigner
return { ...data, user: user.id };
})
);
const getGradingSystemHelper = (
exams: {id: string; module: Module; assignee: string}[],
assigner: string,
user: User,
correct: number,
total: number
) => {
if (exams.some((e) => e.module === "level")) {
const gradingSystem = assignerUsersGradingSystems.find(
(gs) => gs.user === assigner
);
if (gradingSystem) {
const bandScore = calculateBandScore(
correct,
total,
"level",
user.focus
);
return getGradingLabel(bandScore, gradingSystem?.steps || []);
}
}
return "N/A";
};
const tableResults = assignments.reduce(
(accmA: TableData[], a: AssignmentWithCorporateId) => {
@@ -67,14 +107,25 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
const userStats =
a.results.find((r) => r.user === assignee)?.stats || [];
const userData = users.find((u) => u.id === assignee);
const corporate = users.find((u) => u.id === a.assigner)?.name || "";
const corporateUser = users.find((u) => u.id === a.assigner);
const correct = userStats.reduce((n, e) => n + e.score.correct, 0);
const total = userStats.reduce((n, e) => n + e.score.total, 0);
const level = getGradingSystemHelper(
a.exams,
a.assigner,
userData!,
correct,
total
);
console.log("Level", level);
const commonData = {
user: userData?.name || "",
email: userData?.email || "",
userId: assignee,
corporateId: a.corporateId,
corporate,
corporate: corporateUser?.name || "",
assignment: a.name,
level,
};
if (userStats.length === 0) {
return {
@@ -87,7 +138,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
return {
...commonData,
correct: userStats.reduce((n, e) => n + e.score.correct, 0),
correct,
submitted: true,
date: moment.max(userStats.map((e) => moment(e.date))),
};
@@ -129,11 +180,17 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
},
{
label: "Date",
value: (entry: TableData) => entry.date?.format("YYYY/MM/DD") || '',
value: (entry: TableData) => entry.date?.format("YYYY/MM/DD") || "",
},
{
label: "Level",
value: (entry: TableData) => entry.level,
}
];
const filteredSearch = searchText ? search(searchText, searchFilters, tableResults) : tableResults;
const filteredSearch = searchText
? search(searchText, searchFilters, tableResults)
: tableResults;
worksheet.addRow(headers.map((h) => h.label));
(filteredSearch as TableData[]).forEach((entry) => {