Merged in bug-fixing-280824 (pull request #83)

Bug fixing 280824

Approved-by: Tiago Ribeiro
This commit is contained in:
João Ramos
2024-08-28 08:57:52 +00:00
committed by Tiago Ribeiro
2 changed files with 65 additions and 17 deletions

View File

@@ -84,9 +84,16 @@ function commonExcel({
.map((assignee: string) => {
const userStats = allStats.filter((s: any) => s.user === assignee);
const dates = userStats.map((s: any) => moment(s.date));
const user = users.find((u) => u.id === assignee);
return {
userId: assignee,
user: users.find((u) => u.id === assignee),
// added some default values in case the user is not found
// could it be possible to have an assigned user deleted from the database?
user: user || {
name: "Unknown",
email: "Unknown",
demographicInformation: { passportId: "Unknown", gender: "Unknown" },
},
...userStats.reduce(
(acc: any, curr: any) => {
return {
@@ -152,7 +159,7 @@ function commonExcel({
});
// added empty arrays to force row spacings
const customTableAndLine = [[],...customTable, []];
const customTableAndLine = [[], ...customTable, []];
customTableAndLine.forEach((row: string[], index) => {
worksheet.addRow(row);
});
@@ -188,7 +195,8 @@ function commonExcel({
worksheet.addRow(tableColumnHeaders);
// 1 headers rows
const startIndexTable = firstSectionData.length + customTableAndLine.length + 1;
const startIndexTable =
firstSectionData.length + customTableAndLine.length + 1;
// // Merge "Test Sections" over dynamic number of columns
// const tableColumns = staticHeaders.length + numberOfTestSections;
@@ -197,7 +205,7 @@ function commonExcel({
// horizontally group Test Sections
// if there are test section headers to even merge:
if(testSectionHeaders.length > 1) {
if (testSectionHeaders.length > 1) {
worksheet.mergeCells(
startIndexTable,
staticHeaders.length + 1,
@@ -233,7 +241,12 @@ function commonExcel({
// vertically group based on the part, exercise and type
staticHeaders.forEach((header, index) => {
worksheet.mergeCells(startIndexTable, index + 1, startIndexTable + 3, index + 1);
worksheet.mergeCells(
startIndexTable,
index + 1,
startIndexTable + 3,
index + 1
);
});
assigneesData.forEach((data, index) => {
@@ -320,13 +333,17 @@ async function mastercorporateAssignment(
const adminsData = await getSpecificUsers(adminUsers);
const companiesData = adminsData.map((user) => {
const name = getUserName(user);
const users = userGroupsParticipants
.filter((p) => data.assignees.includes(p));
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 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
@@ -346,9 +363,11 @@ async function mastercorporateAssignment(
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}`])
].map((c) => [c.name, `${c.correct}/${c.total}`]);
const customTableHeaders = [{ name: "Corporate", helper: (data: any) => data.user.corporateName}];
const customTableHeaders = [
{ name: "Corporate", helper: (data: any) => data.user.corporateName },
];
return commonExcel({
data,
userName: user.corporateInformation?.companyInformation?.name || "",
@@ -358,12 +377,13 @@ async function mastercorporateAssignment(
return {
...u,
corporateName: getUserName(admin),
}
};
}),
sectionName: "Master Corporate Name :",
customTable: [['Corporate Summary'], ...customTable],
customTable: [["Corporate Summary"], ...customTable],
customTableHeaders: customTableHeaders.map((h) => h.name),
renderCustomTableData: (data) => customTableHeaders.map((h) => h.helper(data)),
renderCustomTableData: (data) =>
customTableHeaders.map((h) => h.helper(data)),
});
}
@@ -415,7 +435,11 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
case "corporate":
return corporateAssignment(user as CorporateUser, data, users);
case "mastercorporate":
return mastercorporateAssignment(user as MasterCorporateUser, data, users);
return mastercorporateAssignment(
user as MasterCorporateUser,
data,
users
);
default:
throw new Error("Invalid user type");
}