|
|
|
|
@@ -13,30 +13,34 @@ import {
|
|
|
|
|
} from "firebase/firestore";
|
|
|
|
|
import { withIronSessionApiRoute } from "iron-session/next";
|
|
|
|
|
import { sessionOptions } from "@/lib/session";
|
|
|
|
|
import ReactPDF from "@react-pdf/renderer";
|
|
|
|
|
import GroupTestReport from "@/exams/pdf/group.test.report";
|
|
|
|
|
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
|
|
|
|
|
import { Stat, CorporateUser } from "@/interfaces/user";
|
|
|
|
|
import { User, DemographicInformation } from "@/interfaces/user";
|
|
|
|
|
import { CorporateUser, MasterCorporateUser } from "@/interfaces/user";
|
|
|
|
|
import { User } from "@/interfaces/user";
|
|
|
|
|
import { Module } from "@/interfaces";
|
|
|
|
|
import { ModuleScore, StudentData } from "@/interfaces/module.scores";
|
|
|
|
|
import { SkillExamDetails } from "@/exams/pdf/details/skill.exam";
|
|
|
|
|
import { LevelExamDetails } from "@/exams/pdf/details/level.exam";
|
|
|
|
|
import { calculateBandScore, getLevelScore } from "@/utils/score";
|
|
|
|
|
import {
|
|
|
|
|
generateQRCode,
|
|
|
|
|
getRadialProgressPNG,
|
|
|
|
|
streamToBuffer,
|
|
|
|
|
} from "@/utils/pdf";
|
|
|
|
|
import { Group } from "@/interfaces/user";
|
|
|
|
|
import moment from "moment-timezone";
|
|
|
|
|
import ExcelJS from "exceljs";
|
|
|
|
|
|
|
|
|
|
import { getStudentGroupsForUsersWithoutAdmin } from "@/utils/groups.be";
|
|
|
|
|
import { getSpecificUsers, getUser } from "@/utils/users.be";
|
|
|
|
|
import { getUserName } from "@/utils/users";
|
|
|
|
|
interface GroupScoreSummaryHelper {
|
|
|
|
|
score: [number, number];
|
|
|
|
|
label: string;
|
|
|
|
|
sessions: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface AssignmentData {
|
|
|
|
|
assigner: string;
|
|
|
|
|
assignees: string[];
|
|
|
|
|
results: any;
|
|
|
|
|
exams: { module: Module }[];
|
|
|
|
|
startDate: string;
|
|
|
|
|
excel: {
|
|
|
|
|
path: string;
|
|
|
|
|
version: string;
|
|
|
|
|
};
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const db = getFirestore(app);
|
|
|
|
|
|
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
|
@@ -55,63 +59,31 @@ function logWorksheetData(worksheet: any) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
// verify if it's a logged user that is trying to export
|
|
|
|
|
if (req.session.user) {
|
|
|
|
|
const { id } = req.query as { id: string };
|
|
|
|
|
|
|
|
|
|
const docSnap = await getDoc(doc(db, "assignments", id));
|
|
|
|
|
const data = docSnap.data() as {
|
|
|
|
|
assigner: string;
|
|
|
|
|
assignees: string[];
|
|
|
|
|
results: any;
|
|
|
|
|
exams: { module: Module }[];
|
|
|
|
|
startDate: string;
|
|
|
|
|
excel: {
|
|
|
|
|
path: string;
|
|
|
|
|
version: string;
|
|
|
|
|
};
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
if (!data) {
|
|
|
|
|
res.status(400).end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (
|
|
|
|
|
// data.excel &&
|
|
|
|
|
// data.excel.path &&
|
|
|
|
|
// data.excel.version === process.env.EXCEL_VERSION
|
|
|
|
|
// ) {
|
|
|
|
|
// // if it does, return the excel url
|
|
|
|
|
// const fileRef = ref(storage, data.excel.path);
|
|
|
|
|
// const url = await getDownloadURL(fileRef);
|
|
|
|
|
// res.status(200).end(url);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const docsSnap = await getDocs(
|
|
|
|
|
query(collection(db, "users"), where(documentId(), "in", data.assignees))
|
|
|
|
|
);
|
|
|
|
|
const users = docsSnap.docs.map((d) => ({
|
|
|
|
|
...d.data(),
|
|
|
|
|
id: d.id,
|
|
|
|
|
})) as User[];
|
|
|
|
|
|
|
|
|
|
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
|
|
|
|
if (docUser.exists()) {
|
|
|
|
|
// we'll need the user in order to get the user data (name, email, focus, etc);
|
|
|
|
|
const user = docUser.data() as CorporateUser;
|
|
|
|
|
|
|
|
|
|
function commonExcel({
|
|
|
|
|
data,
|
|
|
|
|
userName,
|
|
|
|
|
users,
|
|
|
|
|
sectionName,
|
|
|
|
|
customTable,
|
|
|
|
|
customTableHeaders,
|
|
|
|
|
renderCustomTableData,
|
|
|
|
|
}: {
|
|
|
|
|
data: AssignmentData;
|
|
|
|
|
userName: string;
|
|
|
|
|
users: User[];
|
|
|
|
|
sectionName: string;
|
|
|
|
|
customTable: string[][];
|
|
|
|
|
customTableHeaders: string[];
|
|
|
|
|
renderCustomTableData: (data: any) => string[];
|
|
|
|
|
}) {
|
|
|
|
|
const allStats = data.results.flatMap((r: any) => r.stats);
|
|
|
|
|
|
|
|
|
|
const uniqueExercises = [
|
|
|
|
|
...new Set(allStats.map((s: any) => s.exercise)),
|
|
|
|
|
];
|
|
|
|
|
const uniqueExercises = [...new Set(allStats.map((s: any) => s.exercise))];
|
|
|
|
|
|
|
|
|
|
const assigneesData = data.assignees
|
|
|
|
|
.map((assignee: string) => {
|
|
|
|
|
const userStats = allStats.filter((s: any) => s.user === assignee);
|
|
|
|
|
const dates = userStats.map((s: any) => moment(s.date));
|
|
|
|
|
return {
|
|
|
|
|
userId: assignee,
|
|
|
|
|
user: users.find((u) => u.id === assignee),
|
|
|
|
|
@@ -126,6 +98,8 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
},
|
|
|
|
|
{ correct: 0, missing: 0, total: 0 }
|
|
|
|
|
),
|
|
|
|
|
firstDate: moment.min(...dates),
|
|
|
|
|
lastDate: moment.max(...dates),
|
|
|
|
|
stats: userStats,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
@@ -135,18 +109,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
const highestScore = Math.max(...results);
|
|
|
|
|
const lowestScore = Math.min(...results);
|
|
|
|
|
const averageScore = results.reduce((a, b) => a + b, 0) / results.length;
|
|
|
|
|
|
|
|
|
|
const dates = assigneesData
|
|
|
|
|
.flatMap((r: any) => r.stats)
|
|
|
|
|
.map((r: any) => r.date)
|
|
|
|
|
.map((d: number) => moment(d));
|
|
|
|
|
const firstDate = moment.min(dates);
|
|
|
|
|
const lastDate = moment.max(dates);
|
|
|
|
|
const firstDate = moment.min(assigneesData.map((r: any) => r.firstDate));
|
|
|
|
|
const lastDate = moment.max(assigneesData.map((r: any) => r.lastDate));
|
|
|
|
|
|
|
|
|
|
const firstSectionData = [
|
|
|
|
|
{
|
|
|
|
|
label: "Corporate Name :",
|
|
|
|
|
value: user.corporateInformation?.companyInformation?.name || "",
|
|
|
|
|
label: sectionName,
|
|
|
|
|
value: userName,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Report Download date :",
|
|
|
|
|
@@ -182,6 +151,12 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
worksheet.getCell(`B${index + 1}`).value = value; // Second column (values)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// added empty arrays to force row spacings
|
|
|
|
|
const customTableAndLine = [[],...customTable, []];
|
|
|
|
|
customTableAndLine.forEach((row: string[], index) => {
|
|
|
|
|
worksheet.addRow(row);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Define the static part of the headers (before "Test Sections")
|
|
|
|
|
const staticHeaders = [
|
|
|
|
|
"Sr N",
|
|
|
|
|
@@ -190,10 +165,11 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
"Passport/ID",
|
|
|
|
|
"Email ID",
|
|
|
|
|
"Gender",
|
|
|
|
|
...customTableHeaders,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Define additional headers after "Test Sections"
|
|
|
|
|
const additionalHeaders = ["Time Spent", "Score"];
|
|
|
|
|
const additionalHeaders = ["Time Spent", "Date", "Score"];
|
|
|
|
|
|
|
|
|
|
// Calculate the dynamic columns based on the testSectionsArray
|
|
|
|
|
const testSectionHeaders = uniqueExercises.map(
|
|
|
|
|
@@ -212,30 +188,34 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
worksheet.addRow(tableColumnHeaders);
|
|
|
|
|
|
|
|
|
|
// 1 headers rows
|
|
|
|
|
const startIndexTable = firstSectionData.length + 1;
|
|
|
|
|
const startIndexTable = firstSectionData.length + customTableAndLine.length + 1;
|
|
|
|
|
|
|
|
|
|
// // Merge "Test Sections" over dynamic number of columns
|
|
|
|
|
// const tableColumns = staticHeaders.length + numberOfTestSections;
|
|
|
|
|
|
|
|
|
|
// K10:M12 = 10,11,12,13
|
|
|
|
|
// horizontally group Test Sections
|
|
|
|
|
worksheet.mergeCells(
|
|
|
|
|
1,
|
|
|
|
|
startIndexTable,
|
|
|
|
|
staticHeaders.length + 1,
|
|
|
|
|
1,
|
|
|
|
|
startIndexTable,
|
|
|
|
|
tableColumnHeadersFirstPart.length
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add the dynamic second and third header rows for test sections and sub-columns
|
|
|
|
|
worksheet.addRow([
|
|
|
|
|
...Array(staticHeaders.length).fill(""),
|
|
|
|
|
...testSectionHeaders,
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
]);
|
|
|
|
|
worksheet.addRow([
|
|
|
|
|
...Array(staticHeaders.length).fill(""),
|
|
|
|
|
...uniqueExercises.map(() => "Grammar & Vocabulary"),
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
]);
|
|
|
|
|
worksheet.addRow([
|
|
|
|
|
...Array(staticHeaders.length).fill(""),
|
|
|
|
|
@@ -244,15 +224,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
),
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Merging static headers and "Test Sections" over dynamic columns
|
|
|
|
|
worksheet.mergeCells(`A${startIndexTable}:A${startIndexTable + 3}`); // "Sr N"
|
|
|
|
|
worksheet.mergeCells(`B${startIndexTable}:B${startIndexTable + 3}`); // "Candidate ID"
|
|
|
|
|
worksheet.mergeCells(`C${startIndexTable}:C${startIndexTable + 3}`); // "First and Last Name"
|
|
|
|
|
worksheet.mergeCells(`D${startIndexTable}:D${startIndexTable + 3}`); // "Passport/ID"
|
|
|
|
|
worksheet.mergeCells(`E${startIndexTable}:E${startIndexTable + 3}`); // "Email ID"
|
|
|
|
|
worksheet.mergeCells(`F${startIndexTable}:F${startIndexTable + 3}`); // "Gender"
|
|
|
|
|
// vertically group based on the part, exercise and type
|
|
|
|
|
staticHeaders.forEach((header, index) => {
|
|
|
|
|
worksheet.mergeCells(startIndexTable, index + 1, startIndexTable + 3, index + 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assigneesData.forEach((data, index) => {
|
|
|
|
|
worksheet.addRow([
|
|
|
|
|
@@ -262,18 +240,18 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
data.user.demographicInformation?.passportId,
|
|
|
|
|
data.user.email,
|
|
|
|
|
data.user.demographicInformation?.gender,
|
|
|
|
|
...renderCustomTableData(data),
|
|
|
|
|
...uniqueExercises.map((exercise) => {
|
|
|
|
|
const score = data.stats.find(
|
|
|
|
|
(s: any) => s.exercise === exercise && s.user === data.userId
|
|
|
|
|
).score;
|
|
|
|
|
return `${score.correct}/${score.total}`;
|
|
|
|
|
}),
|
|
|
|
|
`${
|
|
|
|
|
Math.ceil(data.stats.reduce(
|
|
|
|
|
(acc: number, curr: any) => acc + curr.timeSpent,
|
|
|
|
|
0
|
|
|
|
|
) / 60)
|
|
|
|
|
} minutes`,
|
|
|
|
|
`${Math.ceil(
|
|
|
|
|
data.stats.reduce((acc: number, curr: any) => acc + curr.timeSpent, 0) /
|
|
|
|
|
60
|
|
|
|
|
)} minutes`,
|
|
|
|
|
data.lastDate.format("DD/MM/YYYY HH:mm"),
|
|
|
|
|
data.correct,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
@@ -304,12 +282,142 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
worksheet.addRow(["info@encoach.com"]);
|
|
|
|
|
|
|
|
|
|
// Convert workbook to Buffer (Node.js) or Blob (Browser)
|
|
|
|
|
const buffer = await workbook.xlsx.writeBuffer();
|
|
|
|
|
return workbook.xlsx.writeBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function corporateAssignment(
|
|
|
|
|
user: CorporateUser,
|
|
|
|
|
data: AssignmentData,
|
|
|
|
|
users: User[]
|
|
|
|
|
) {
|
|
|
|
|
return commonExcel({
|
|
|
|
|
data,
|
|
|
|
|
userName: user.corporateInformation?.companyInformation?.name || "",
|
|
|
|
|
users,
|
|
|
|
|
sectionName: "Corporate Name :",
|
|
|
|
|
customTable: [],
|
|
|
|
|
customTableHeaders: [],
|
|
|
|
|
renderCustomTableData: () => [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function mastercorporateAssignment(
|
|
|
|
|
user: MasterCorporateUser,
|
|
|
|
|
data: AssignmentData,
|
|
|
|
|
users: User[]
|
|
|
|
|
) {
|
|
|
|
|
const userGroups = await getStudentGroupsForUsersWithoutAdmin(
|
|
|
|
|
user.id,
|
|
|
|
|
data.assignees
|
|
|
|
|
);
|
|
|
|
|
const adminUsers = [...new Set(userGroups.map((g) => g.admin))];
|
|
|
|
|
|
|
|
|
|
const userGroupsParticipants = userGroups.flatMap((g) => g.participants);
|
|
|
|
|
const adminsData = await getSpecificUsers(adminUsers);
|
|
|
|
|
const companiesData = adminsData.map((user) => {
|
|
|
|
|
const name = getUserName(user);
|
|
|
|
|
const users = userGroupsParticipants
|
|
|
|
|
.filter((p) => data.assignees.includes(p));
|
|
|
|
|
|
|
|
|
|
const stats = data.results
|
|
|
|
|
.flatMap((r: any) => r.stats)
|
|
|
|
|
.filter((s: any) => users.includes(s.user));
|
|
|
|
|
const correct = stats.reduce((acc: number, s: any) => acc + s.score.correct, 0);
|
|
|
|
|
const total = stats.reduce(
|
|
|
|
|
(acc: number, curr: any) => acc + curr.score.total,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
name,
|
|
|
|
|
correct,
|
|
|
|
|
total,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const customTable = [
|
|
|
|
|
...companiesData,
|
|
|
|
|
{
|
|
|
|
|
name: "Total",
|
|
|
|
|
correct: companiesData.reduce((acc, curr) => acc + curr.correct, 0),
|
|
|
|
|
total: companiesData.reduce((acc, curr) => acc + curr.total, 0),
|
|
|
|
|
},
|
|
|
|
|
].map((c) => [c.name, `${c.correct}/${c.total}`])
|
|
|
|
|
|
|
|
|
|
const customTableHeaders = [{ name: "Corporate", helper: (data: any) => data.user.corporateName}];
|
|
|
|
|
return commonExcel({
|
|
|
|
|
data,
|
|
|
|
|
userName: user.corporateInformation?.companyInformation?.name || "",
|
|
|
|
|
users: users.map((u) => {
|
|
|
|
|
const userGroup = userGroups.find((g) => g.participants.includes(u.id));
|
|
|
|
|
const admin = adminsData.find((a) => a.id === userGroup?.admin);
|
|
|
|
|
return {
|
|
|
|
|
...u,
|
|
|
|
|
corporateName: getUserName(admin),
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
sectionName: "Master Corporate Name :",
|
|
|
|
|
customTable: [['Corporate Summary'], ...customTable],
|
|
|
|
|
customTableHeaders: customTableHeaders.map((h) => h.name),
|
|
|
|
|
renderCustomTableData: (data) => customTableHeaders.map((h) => h.helper(data)),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
// verify if it's a logged user that is trying to export
|
|
|
|
|
if (req.session.user) {
|
|
|
|
|
const { id } = req.query as { id: string };
|
|
|
|
|
|
|
|
|
|
const docSnap = await getDoc(doc(db, "assignments", id));
|
|
|
|
|
const data = docSnap.data() as AssignmentData;
|
|
|
|
|
if (!data) {
|
|
|
|
|
res.status(400).end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (
|
|
|
|
|
// data.excel &&
|
|
|
|
|
// data.excel.path &&
|
|
|
|
|
// data.excel.version === process.env.EXCEL_VERSION
|
|
|
|
|
// ) {
|
|
|
|
|
// // if it does, return the excel url
|
|
|
|
|
// const fileRef = ref(storage, data.excel.path);
|
|
|
|
|
// const url = await getDownloadURL(fileRef);
|
|
|
|
|
// res.status(200).end(url);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const docsSnap = await getDocs(
|
|
|
|
|
query(collection(db, "users"), where(documentId(), "in", data.assignees))
|
|
|
|
|
);
|
|
|
|
|
const users = docsSnap.docs.map((d) => ({
|
|
|
|
|
...d.data(),
|
|
|
|
|
id: d.id,
|
|
|
|
|
})) as User[];
|
|
|
|
|
|
|
|
|
|
const docUser = await getDoc(doc(db, "users", data.assigner));
|
|
|
|
|
if (docUser.exists()) {
|
|
|
|
|
// we'll need the user in order to get the user data (name, email, focus, etc);
|
|
|
|
|
const user = docUser.data() as User;
|
|
|
|
|
|
|
|
|
|
// generate the file ref for storage
|
|
|
|
|
const fileName = `${Date.now().toString()}.xlsx`;
|
|
|
|
|
const refName = `assignment_report/${fileName}`;
|
|
|
|
|
const fileRef = ref(storage, refName);
|
|
|
|
|
|
|
|
|
|
const getExcelFn = () => {
|
|
|
|
|
switch (user.type) {
|
|
|
|
|
case "teacher":
|
|
|
|
|
case "corporate":
|
|
|
|
|
return corporateAssignment(user as CorporateUser, data, users);
|
|
|
|
|
case "mastercorporate":
|
|
|
|
|
return mastercorporateAssignment(user as MasterCorporateUser, data, users);
|
|
|
|
|
default:
|
|
|
|
|
throw new Error("Invalid user type");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const buffer = await getExcelFn();
|
|
|
|
|
|
|
|
|
|
// upload the pdf to storage
|
|
|
|
|
const snapshot = await uploadBytes(fileRef, buffer, {
|
|
|
|
|
contentType:
|
|
|
|
|
|