Solved a problem with the download of the excel
This commit is contained in:
@@ -343,7 +343,7 @@ const MasterStatistical = (props: Props) => {
|
|||||||
</div>
|
</div>
|
||||||
{renderSearch()}
|
{renderSearch()}
|
||||||
<div className="flex flex-col gap-3 justify-end">
|
<div className="flex flex-col gap-3 justify-end">
|
||||||
<Button className="max-w-[200px] h-[70px]" variant="outline" onClick={triggerDownload}>
|
<Button className="max-w-[200px] h-[70px]" variant="outline" isLoading={downloading} onClick={triggerDownload}>
|
||||||
Download
|
Download
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,254 +1,229 @@
|
|||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import { storage } from "@/firebase";
|
import {storage} from "@/firebase";
|
||||||
import { withIronSessionApiRoute } from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import { sessionOptions } from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
|
import {ref, uploadBytes, getDownloadURL} from "firebase/storage";
|
||||||
import { AssignmentWithCorporateId } from "@/interfaces/results";
|
import {AssignmentWithCorporateId} from "@/interfaces/results";
|
||||||
import moment from "moment-timezone";
|
import moment from "moment-timezone";
|
||||||
import ExcelJS from "exceljs";
|
import ExcelJS from "exceljs";
|
||||||
import { getSpecificUsers } from "@/utils/users.be";
|
import {getSpecificUsers} from "@/utils/users.be";
|
||||||
import { checkAccess } from "@/utils/permissions";
|
import {checkAccess} from "@/utils/permissions";
|
||||||
import { getAssignmentsForCorporates } from "@/utils/assignments.be";
|
import {getAssignmentsForCorporates} from "@/utils/assignments.be";
|
||||||
import { search } from "@/utils/search";
|
import {search} from "@/utils/search";
|
||||||
import { getGradingSystem } from "@/utils/grading.be";
|
import {getGradingSystem} from "@/utils/grading.be";
|
||||||
import { User } from "@/interfaces/user";
|
import {User} from "@/interfaces/user";
|
||||||
import { calculateBandScore, getGradingLabel } from "@/utils/score";
|
import {calculateBandScore, getGradingLabel} from "@/utils/score";
|
||||||
import { Module } from "@/interfaces";
|
import {Module} from "@/interfaces";
|
||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
interface TableData {
|
interface TableData {
|
||||||
user: string;
|
user: string;
|
||||||
email: string;
|
email: string;
|
||||||
correct: number;
|
correct: number;
|
||||||
corporate: string;
|
corporate: string;
|
||||||
submitted: boolean;
|
submitted: boolean;
|
||||||
date: moment.Moment;
|
date: moment.Moment;
|
||||||
assignment: string;
|
assignment: string;
|
||||||
corporateId: string;
|
corporateId: string;
|
||||||
score: number;
|
score: number;
|
||||||
level: string;
|
level: string;
|
||||||
part1?: string;
|
part1?: string;
|
||||||
part2?: string;
|
part2?: string;
|
||||||
part3?: string;
|
part3?: string;
|
||||||
part4?: string;
|
part4?: string;
|
||||||
part5?: string;
|
part5?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
// if (req.method === "GET") return get(req, res);
|
// if (req.method === "GET") return get(req, res);
|
||||||
if (req.method === "POST") return await post(req, res);
|
if (req.method === "POST") return await post(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const searchFilters = [["email"], ["user"], ["userId"]];
|
const searchFilters = [["email"], ["user"], ["userId"]];
|
||||||
|
|
||||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||||
// verify if it's a logged user that is trying to export
|
// verify if it's a logged user that is trying to export
|
||||||
if (req.session.user) {
|
if (req.session.user) {
|
||||||
if (
|
if (!checkAccess(req.session.user, ["mastercorporate", "corporate", "developer", "admin"])) {
|
||||||
!checkAccess(req.session.user, ["mastercorporate", "corporate", "developer", "admin"])
|
return res.status(403).json({error: "Unauthorized"});
|
||||||
) {
|
}
|
||||||
return res.status(403).json({ error: "Unauthorized" });
|
const {
|
||||||
}
|
ids,
|
||||||
const {
|
startDate,
|
||||||
ids,
|
endDate,
|
||||||
startDate,
|
searchText,
|
||||||
endDate,
|
displaySelection = true,
|
||||||
searchText,
|
} = req.body as {
|
||||||
displaySelection = true,
|
ids: string[];
|
||||||
} = req.body as {
|
startDate?: string;
|
||||||
ids: string[];
|
endDate?: string;
|
||||||
startDate?: string;
|
searchText: string;
|
||||||
endDate?: string;
|
displaySelection?: boolean;
|
||||||
searchText: string;
|
};
|
||||||
displaySelection?: boolean;
|
const startDateParsed = startDate ? new Date(startDate) : undefined;
|
||||||
};
|
const endDateParsed = endDate ? new Date(endDate) : undefined;
|
||||||
const startDateParsed = startDate ? new Date(startDate) : undefined;
|
const assignments = await getAssignmentsForCorporates(ids, startDateParsed, endDateParsed);
|
||||||
const endDateParsed = endDate ? new Date(endDate) : undefined;
|
|
||||||
const assignments = await getAssignmentsForCorporates(
|
|
||||||
ids,
|
|
||||||
startDateParsed,
|
|
||||||
endDateParsed
|
|
||||||
);
|
|
||||||
|
|
||||||
const assignmentUsers = [
|
const assignmentUsers = [...new Set(assignments.flatMap((a) => a.assignees))];
|
||||||
...new Set(assignments.flatMap((a) => a.assignees)),
|
const assigners = [...new Set(assignments.map((a) => a.assigner))];
|
||||||
];
|
const users = await getSpecificUsers(assignmentUsers);
|
||||||
const assigners = [...new Set(assignments.map((a) => a.assigner))];
|
const assignerUsers = await getSpecificUsers(assigners);
|
||||||
const users = await getSpecificUsers(assignmentUsers);
|
|
||||||
const assignerUsers = await getSpecificUsers(assigners);
|
|
||||||
|
|
||||||
const assignerUsersGradingSystems = await Promise.all(
|
const assignerUsersGradingSystems = await Promise.all(
|
||||||
assignerUsers.map(async (user: User) => {
|
assignerUsers.map(async (user: User) => {
|
||||||
const data = await getGradingSystem(user);
|
const data = await getGradingSystem(user);
|
||||||
// in this context I need to override as I'll have to match to the assigner
|
// in this context I need to override as I'll have to match to the assigner
|
||||||
return { ...data, user: user.id };
|
return {...data, user: user.id};
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const getGradingSystemHelper = (
|
const getGradingSystemHelper = (
|
||||||
exams: { id: string; module: Module; assignee: string }[],
|
exams: {id: string; module: Module; assignee: string}[],
|
||||||
assigner: string,
|
assigner: string,
|
||||||
user: User,
|
user: User,
|
||||||
correct: number,
|
correct: number,
|
||||||
total: number
|
total: number,
|
||||||
) => {
|
) => {
|
||||||
if (exams.some((e) => e.module === "level")) {
|
if (exams.some((e) => e.module === "level")) {
|
||||||
const gradingSystem = assignerUsersGradingSystems.find(
|
const gradingSystem = assignerUsersGradingSystems.find((gs) => gs.user === assigner);
|
||||||
(gs) => gs.user === assigner
|
if (gradingSystem) {
|
||||||
);
|
const bandScore = calculateBandScore(correct, total, "level", user?.focus || "academic");
|
||||||
if (gradingSystem) {
|
return {
|
||||||
const bandScore = calculateBandScore(
|
label: getGradingLabel(bandScore, gradingSystem?.steps || []),
|
||||||
correct,
|
score: bandScore,
|
||||||
total,
|
};
|
||||||
"level",
|
}
|
||||||
user.focus
|
}
|
||||||
);
|
|
||||||
return {
|
|
||||||
label: getGradingLabel(bandScore, gradingSystem?.steps || []),
|
|
||||||
score: bandScore,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { score: -1, label: "N/A" };
|
return {score: -1, label: "N/A"};
|
||||||
};
|
};
|
||||||
|
|
||||||
const tableResults = assignments
|
const tableResults = assignments
|
||||||
.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
.reduce((accmA: TableData[], a: AssignmentWithCorporateId) => {
|
||||||
const userResults = a.assignees.map((assignee) => {
|
const userResults = a.assignees.map((assignee) => {
|
||||||
const userStats =
|
const userStats = a.results.find((r) => r.user === assignee)?.stats || [];
|
||||||
a.results.find((r) => r.user === assignee)?.stats || [];
|
const userData = users.find((u) => u.id === assignee);
|
||||||
const userData = users.find((u) => u.id === assignee);
|
const corporateUser = users.find((u) => u.id === a.assigner);
|
||||||
const corporateUser = users.find((u) => u.id === a.assigner);
|
const correct = userStats.reduce((n, e) => n + e.score.correct, 0);
|
||||||
const correct = userStats.reduce((n, e) => n + e.score.correct, 0);
|
const total = userStats.reduce((n, e) => n + e.score.total, 0);
|
||||||
const total = userStats.reduce((n, e) => n + e.score.total, 0);
|
const {label: level, score} = getGradingSystemHelper(a.exams, a.assigner, userData!, correct, total);
|
||||||
const { label: level, score } = getGradingSystemHelper(
|
|
||||||
a.exams,
|
|
||||||
a.assigner,
|
|
||||||
userData!,
|
|
||||||
correct,
|
|
||||||
total
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log("Level", level);
|
console.log("Level", level);
|
||||||
const commonData = {
|
const commonData = {
|
||||||
user: userData?.name || "",
|
user: userData?.name || "",
|
||||||
email: userData?.email || "",
|
email: userData?.email || "",
|
||||||
userId: assignee,
|
userId: assignee,
|
||||||
corporateId: a.corporateId,
|
corporateId: a.corporateId,
|
||||||
corporate: corporateUser?.name || "",
|
corporate: corporateUser?.name || "",
|
||||||
assignment: a.name,
|
assignment: a.name,
|
||||||
level,
|
level,
|
||||||
score,
|
score,
|
||||||
};
|
};
|
||||||
if (userStats.length === 0) {
|
if (userStats.length === 0) {
|
||||||
return {
|
return {
|
||||||
...commonData,
|
...commonData,
|
||||||
correct: 0,
|
correct: 0,
|
||||||
submitted: false,
|
submitted: false,
|
||||||
// date: moment(),
|
// date: moment(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const partsData = userStats.every((e) => e.module === "level")
|
const partsData = userStats.every((e) => e.module === "level")
|
||||||
? userStats.reduce((acc, e, index) => {
|
? userStats.reduce((acc, e, index) => {
|
||||||
return {
|
return {
|
||||||
...acc,
|
...acc,
|
||||||
[`part${index}`]: `${e.score.correct}/${e.score.total}`,
|
[`part${index}`]: `${e.score.correct}/${e.score.total}`,
|
||||||
};
|
};
|
||||||
}, {})
|
}, {})
|
||||||
: {};
|
: {};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...commonData,
|
...commonData,
|
||||||
correct,
|
correct,
|
||||||
submitted: true,
|
submitted: true,
|
||||||
date: moment.max(userStats.map((e) => moment(e.date))),
|
date: moment.max(userStats.map((e) => moment(e.date))),
|
||||||
...partsData,
|
...partsData,
|
||||||
};
|
};
|
||||||
}) as TableData[];
|
}) as TableData[];
|
||||||
|
|
||||||
return [...accmA, ...userResults];
|
return [...accmA, ...userResults];
|
||||||
}, [])
|
}, [])
|
||||||
.sort((a, b) => b.score - a.score);
|
.sort((a, b) => b.score - a.score);
|
||||||
|
|
||||||
// Create a new workbook and add a worksheet
|
// Create a new workbook and add a worksheet
|
||||||
const workbook = new ExcelJS.Workbook();
|
const workbook = new ExcelJS.Workbook();
|
||||||
const worksheet = workbook.addWorksheet("Master Statistical");
|
const worksheet = workbook.addWorksheet("Master Statistical");
|
||||||
|
|
||||||
const headers = [
|
const headers = [
|
||||||
{
|
{
|
||||||
label: "User",
|
label: "User",
|
||||||
value: (entry: TableData) => entry.user,
|
value: (entry: TableData) => entry.user,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Email",
|
label: "Email",
|
||||||
value: (entry: TableData) => entry.email,
|
value: (entry: TableData) => entry.email,
|
||||||
},
|
},
|
||||||
...(displaySelection
|
...(displaySelection
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
label: "Corporate",
|
label: "Corporate",
|
||||||
value: (entry: TableData) => entry.corporate,
|
value: (entry: TableData) => entry.corporate,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
: []),
|
: []),
|
||||||
{
|
{
|
||||||
label: "Assignment",
|
label: "Assignment",
|
||||||
value: (entry: TableData) => entry.assignment,
|
value: (entry: TableData) => entry.assignment,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Submitted",
|
label: "Submitted",
|
||||||
value: (entry: TableData) => (entry.submitted ? "Yes" : "No"),
|
value: (entry: TableData) => (entry.submitted ? "Yes" : "No"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Correct",
|
label: "Correct",
|
||||||
value: (entry: TableData) => entry.correct,
|
value: (entry: TableData) => entry.correct,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Date",
|
label: "Date",
|
||||||
value: (entry: TableData) => entry.date?.format("YYYY/MM/DD") || "",
|
value: (entry: TableData) => entry.date?.format("YYYY/MM/DD") || "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Level",
|
label: "Level",
|
||||||
value: (entry: TableData) => entry.level,
|
value: (entry: TableData) => entry.level,
|
||||||
},
|
},
|
||||||
...new Array(5).fill(0).map((_, index) => ({
|
...new Array(5).fill(0).map((_, index) => ({
|
||||||
label: `Part ${index + 1}`,
|
label: `Part ${index + 1}`,
|
||||||
value: (entry: TableData) => {
|
value: (entry: TableData) => {
|
||||||
const key = `part${index}` as keyof TableData;
|
const key = `part${index}` as keyof TableData;
|
||||||
return entry[key] || "";
|
return entry[key] || "";
|
||||||
},
|
},
|
||||||
})),
|
})),
|
||||||
];
|
];
|
||||||
|
|
||||||
const filteredSearch = searchText
|
const filteredSearch = searchText ? search(searchText, searchFilters, tableResults) : tableResults;
|
||||||
? search(searchText, searchFilters, tableResults)
|
|
||||||
: tableResults;
|
|
||||||
|
|
||||||
worksheet.addRow(headers.map((h) => h.label));
|
worksheet.addRow(headers.map((h) => h.label));
|
||||||
(filteredSearch as TableData[]).forEach((entry) => {
|
(filteredSearch as TableData[]).forEach((entry) => {
|
||||||
worksheet.addRow(headers.map((h) => h.value(entry)));
|
worksheet.addRow(headers.map((h) => h.value(entry)));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Convert workbook to Buffer (Node.js) or Blob (Browser)
|
// Convert workbook to Buffer (Node.js) or Blob (Browser)
|
||||||
const buffer = await workbook.xlsx.writeBuffer();
|
const buffer = await workbook.xlsx.writeBuffer();
|
||||||
|
|
||||||
// generate the file ref for storage
|
// generate the file ref for storage
|
||||||
const fileName = `${Date.now().toString()}.xlsx`;
|
const fileName = `${Date.now().toString()}.xlsx`;
|
||||||
const refName = `statistical/${fileName}`;
|
const refName = `statistical/${fileName}`;
|
||||||
const fileRef = ref(storage, refName);
|
const fileRef = ref(storage, refName);
|
||||||
// upload the pdf to storage
|
// upload the pdf to storage
|
||||||
await uploadBytes(fileRef, buffer, {
|
await uploadBytes(fileRef, buffer, {
|
||||||
contentType:
|
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
});
|
||||||
});
|
|
||||||
|
|
||||||
const url = await getDownloadURL(fileRef);
|
const url = await getDownloadURL(fileRef);
|
||||||
res.status(200).end(url);
|
res.status(200).end(url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(401).json({ error: "Unauthorized" });
|
return res.status(401).json({error: "Unauthorized"});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user