Added an export feature for the master statisticl screen
This commit is contained in:
@@ -2,8 +2,7 @@
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {getAllAssignersByCorporate} from "@/utils/groups.be";
|
||||
import {getAssignmentsByAssigners} from "@/utils/assignments.be";
|
||||
import {getAssignmentsByAssigner, getAssignmentsForCorporates} from "@/utils/assignments.be";
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
@@ -30,34 +29,8 @@ async function GET(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const idsList = ids.split(",");
|
||||
|
||||
const assigners = await Promise.all(
|
||||
idsList.map(async (id) => {
|
||||
const assigners = await getAllAssignersByCorporate(id);
|
||||
return {
|
||||
corporateId: id,
|
||||
assigners,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
const assignments = await Promise.all(
|
||||
assigners.map(async (data) => {
|
||||
try {
|
||||
const assigners = [...new Set([...data.assigners, data.corporateId])];
|
||||
const assignments = await getAssignmentsByAssigners(assigners, startDateParsed, endDateParsed);
|
||||
return assignments.map((assignment) => ({
|
||||
...assignment,
|
||||
corporateId: data.corporateId,
|
||||
}));
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return [];
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// const assignments = await getAssignmentsByAssigners(assignmentList, startDateParsed, endDateParsed);
|
||||
res.status(200).json(assignments.flat());
|
||||
const assignments = await getAssignmentsForCorporates(idsList, startDateParsed, endDateParsed);
|
||||
res.status(200).json(assignments);
|
||||
} catch (err: any) {
|
||||
res.status(500).json({error: err.message});
|
||||
}
|
||||
|
||||
162
src/pages/api/assignments/statistical/excel.ts
Normal file
162
src/pages/api/assignments/statistical/excel.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { app, storage } from "@/firebase";
|
||||
import { getFirestore } from "firebase/firestore";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
|
||||
import { AssignmentWithCorporateId } from "@/interfaces/results";
|
||||
import moment from "moment-timezone";
|
||||
import ExcelJS from "exceljs";
|
||||
import { getSpecificUsers } from "@/utils/users.be";
|
||||
import { checkAccess } from "@/utils/permissions";
|
||||
import { getAssignmentsForCorporates } from "@/utils/assignments.be";
|
||||
import { search } from "@/utils/search";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
interface TableData {
|
||||
user: string;
|
||||
email: string;
|
||||
correct: number;
|
||||
corporate: string;
|
||||
submitted: boolean;
|
||||
date: moment.Moment;
|
||||
assignment: string;
|
||||
corporateId: string;
|
||||
}
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
// if (req.method === "GET") return get(req, res);
|
||||
if (req.method === "POST") return await post(req, res);
|
||||
}
|
||||
|
||||
const searchFilters = [["email"], ["user"], ["userId"]];
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
// verify if it's a logged user that is trying to export
|
||||
if (req.session.user) {
|
||||
if (
|
||||
!checkAccess(req.session.user, ["mastercorporate", "developer", "admin"])
|
||||
) {
|
||||
return res.status(401).json({ error: "Unauthorized" });
|
||||
}
|
||||
const { ids, startDate, endDate, searchText } = req.body as {
|
||||
ids: string[];
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
searchText: string;
|
||||
};
|
||||
const startDateParsed = startDate ? new Date(startDate) : undefined;
|
||||
const endDateParsed = endDate ? new Date(endDate) : undefined;
|
||||
const assignments = await getAssignmentsForCorporates(
|
||||
ids,
|
||||
startDateParsed,
|
||||
endDateParsed
|
||||
);
|
||||
|
||||
const assignmentUsers = [
|
||||
...new Set(assignments.flatMap((a) => a.assignees)),
|
||||
];
|
||||
const users = await getSpecificUsers(assignmentUsers);
|
||||
|
||||
const tableResults = assignments.reduce(
|
||||
(accmA: TableData[], a: AssignmentWithCorporateId) => {
|
||||
const userResults = a.assignees.map((assignee) => {
|
||||
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 commonData = {
|
||||
user: userData?.name || "",
|
||||
email: userData?.email || "",
|
||||
userId: assignee,
|
||||
corporateId: a.corporateId,
|
||||
corporate,
|
||||
assignment: a.name,
|
||||
};
|
||||
if (userStats.length === 0) {
|
||||
return {
|
||||
...commonData,
|
||||
correct: 0,
|
||||
submitted: false,
|
||||
// date: moment(),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...commonData,
|
||||
correct: userStats.reduce((n, e) => n + e.score.correct, 0),
|
||||
submitted: true,
|
||||
date: moment.max(userStats.map((e) => moment(e.date))),
|
||||
};
|
||||
}) as TableData[];
|
||||
|
||||
return [...accmA, ...userResults];
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
// Create a new workbook and add a worksheet
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
const worksheet = workbook.addWorksheet("Master Statistical");
|
||||
|
||||
const headers = [
|
||||
{
|
||||
label: "User",
|
||||
value: (entry: TableData) => entry.user,
|
||||
},
|
||||
{
|
||||
label: "Email",
|
||||
value: (entry: TableData) => entry.email,
|
||||
},
|
||||
{
|
||||
label: "Corporate",
|
||||
value: (entry: TableData) => entry.corporate,
|
||||
},
|
||||
{
|
||||
label: "Assignment",
|
||||
value: (entry: TableData) => entry.assignment,
|
||||
},
|
||||
{
|
||||
label: "Submitted",
|
||||
value: (entry: TableData) => (entry.submitted ? "Yes" : "No"),
|
||||
},
|
||||
{
|
||||
label: "Correct",
|
||||
value: (entry: TableData) => entry.correct,
|
||||
},
|
||||
{
|
||||
label: "Date",
|
||||
value: (entry: TableData) => entry.date?.format("YYYY/MM/DD") || '',
|
||||
},
|
||||
];
|
||||
|
||||
const filteredSearch = searchText ? search(searchText, searchFilters, tableResults) : tableResults;
|
||||
|
||||
worksheet.addRow(headers.map((h) => h.label));
|
||||
(filteredSearch as TableData[]).forEach((entry) => {
|
||||
worksheet.addRow(headers.map((h) => h.value(entry)));
|
||||
});
|
||||
|
||||
// Convert workbook to Buffer (Node.js) or Blob (Browser)
|
||||
const buffer = await workbook.xlsx.writeBuffer();
|
||||
|
||||
// generate the file ref for storage
|
||||
const fileName = `${Date.now().toString()}.xlsx`;
|
||||
const refName = `statistical/${fileName}`;
|
||||
const fileRef = ref(storage, refName);
|
||||
// upload the pdf to storage
|
||||
const snapshot = await uploadBytes(fileRef, buffer, {
|
||||
contentType:
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
});
|
||||
|
||||
const url = await getDownloadURL(fileRef);
|
||||
res.status(200).end(url);
|
||||
return;
|
||||
}
|
||||
|
||||
return res.status(401).json({ error: "Unauthorized" });
|
||||
}
|
||||
Reference in New Issue
Block a user