From 614a7a2a294b4c733c8630766cbbb91339c6bcdb Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Wed, 28 Aug 2024 07:38:38 +0100 Subject: [PATCH 1/2] Improved asset download criteria --- src/dashboards/AssignmentCard.tsx | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/dashboards/AssignmentCard.tsx b/src/dashboards/AssignmentCard.tsx index 6466a335..d3243656 100644 --- a/src/dashboards/AssignmentCard.tsx +++ b/src/dashboards/AssignmentCard.tsx @@ -62,6 +62,30 @@ export default function AssignmentCard({ return resultModuleBandScores.length === 0 ? -1 : resultModuleBandScores.reduce((acc, curr) => acc + curr, 0) / results.length; }; + const uniqModules = uniqBy(exams, (x) => x.module); + + const shouldRenderPDF = () => { + if(released && allowDownload) { + // in order to be downloadable, the assignment has to be released + // the component should have the allowDownload prop + // and the assignment should not have the level module + return uniqModules.every(({ module }) => module !== 'level'); + } + + return false; + } + + const shouldRenderExcel = () => { + if(released && allowExcelDownload) { + // in order to be downloadable, the assignment has to be released + // the component should have the allowExcelDownload prop + // and the assignment should have the level module + return uniqModules.some(({ module }) => module === 'level'); + } + + return false; + } + return (

{name}

- {allowDownload && released && renderPdfIcon(id, "text-mti-gray-dim", "text-mti-gray-dim")} - {allowExcelDownload && released && renderExcelIcon(id, "text-mti-gray-dim", "text-mti-gray-dim")} + {shouldRenderPDF() && renderPdfIcon(id, "text-mti-gray-dim", "text-mti-gray-dim")} + {shouldRenderExcel() && renderExcelIcon(id, "text-mti-gray-dim", "text-mti-gray-dim")} {allowArchive && !archived && renderArchiveIcon("text-mti-gray-dim", "text-mti-gray-dim")} {allowUnarchive && archived && renderUnarchiveIcon("text-mti-gray-dim", "text-mti-gray-dim")} {!released && renderReleaseIcon("text-mti-gray-dim", "text-mti-gray-dim")} @@ -94,7 +118,7 @@ export default function AssignmentCard({ Assigner: {getUserName(users.find((x) => x.id === assigner))}
- {uniqBy(exams, (x) => x.module).map(({module}) => ( + {uniqModules.map(({module}) => (
Date: Wed, 28 Aug 2024 07:39:51 +0100 Subject: [PATCH 2/2] Added default value in case on the user not being found in the DB --- .../api/assignments/[id]/[export]/excel.ts | 52 ++++++++++++++----- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/pages/api/assignments/[id]/[export]/excel.ts b/src/pages/api/assignments/[id]/[export]/excel.ts index d4fa7f05..2502f03c 100644 --- a/src/pages/api/assignments/[id]/[export]/excel.ts +++ b/src/pages/api/assignments/[id]/[export]/excel.ts @@ -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"); }