From 6bcc303b740e685c7c20414bd9f4a49e2ffa3cd2 Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Tue, 16 Jan 2024 22:24:08 +0000 Subject: [PATCH] Fixed institution print --- src/pages/api/assignments/[id]/export.tsx | 74 +++++++++++++++++++++-- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/src/pages/api/assignments/[id]/export.tsx b/src/pages/api/assignments/[id]/export.tsx index 4a3a3e52..cfbbcc58 100644 --- a/src/pages/api/assignments/[id]/export.tsx +++ b/src/pages/api/assignments/[id]/export.tsx @@ -16,7 +16,7 @@ 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 } from "@/interfaces/user"; +import { Stat, CorporateUser } from "@/interfaces/user"; import { User, DemographicInformation } from "@/interfaces/user"; import { Module } from "@/interfaces"; import { ModuleScore, StudentData } from "@/interfaces/module.scores"; @@ -28,6 +28,7 @@ import { getRadialProgressPNG, streamToBuffer, } from "@/utils/pdf"; +import { Group } from "@/interfaces/user"; interface GroupScoreSummaryHelper { score: [number, number]; @@ -345,8 +346,73 @@ async function post(req: NextApiRequest, res: NextApiResponse) { return result; }; + const getInstitution = async () => { + try { + // due to database inconsistencies, I'll be overprotective here + const assignerUserSnap = await getDoc( + doc(db, "users", data.assigner) + ); + if (assignerUserSnap.exists()) { + // we'll need the user in order to get the user data (name, email, focus, etc); + const assignerUser = assignerUserSnap.data() as User; + + if (assignerUser.type === "teacher") { + // also search for groups where this user belongs + const queryGroups = query( + collection(db, "groups"), + where("participants", "array-contains", assignerUser.id) + ); + const groupSnapshot = await getDocs(queryGroups); + + const groups = groupSnapshot.docs.map((doc) => ({ + id: doc.id, + ...doc.data(), + })) as Group[]; + + if (groups.length > 0) { + const adminQuery = query( + collection(db, "users"), + where( + documentId(), + "in", + groups.map((g) => g.admin) + ) + ); + const adminUsersSnap = await getDocs(adminQuery); + + const admins = adminUsersSnap.docs.map((doc) => ({ + id: doc.id, + ...doc.data(), + })) as CorporateUser[]; + + const adminData = admins.find( + (a) => a.corporateInformation?.companyInformation?.name + ); + if (adminData) { + return adminData.corporateInformation.companyInformation + .name; + } + } + } + + if ( + assignerUser.type === "corporate" && + assignerUser.corporateInformation?.companyInformation?.name + ) { + return assignerUser.corporateInformation.companyInformation + .name; + } + } + } catch (err) { + console.error(err); + } + return ""; + }; + + const institution = await getInstitution(); const groupScoreSummary = getGroupScoreSummary(); - const demographicInformation = user.demographicInformation as DemographicInformation; + const demographicInformation = + user.demographicInformation as DemographicInformation; const pdfStream = await ReactPDF.renderToStream( );