Fixed institution print
This commit is contained in:
@@ -16,7 +16,7 @@ import { sessionOptions } from "@/lib/session";
|
|||||||
import ReactPDF from "@react-pdf/renderer";
|
import ReactPDF from "@react-pdf/renderer";
|
||||||
import GroupTestReport from "@/exams/pdf/group.test.report";
|
import GroupTestReport from "@/exams/pdf/group.test.report";
|
||||||
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
|
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 { User, DemographicInformation } from "@/interfaces/user";
|
||||||
import { Module } from "@/interfaces";
|
import { Module } from "@/interfaces";
|
||||||
import { ModuleScore, StudentData } from "@/interfaces/module.scores";
|
import { ModuleScore, StudentData } from "@/interfaces/module.scores";
|
||||||
@@ -28,6 +28,7 @@ import {
|
|||||||
getRadialProgressPNG,
|
getRadialProgressPNG,
|
||||||
streamToBuffer,
|
streamToBuffer,
|
||||||
} from "@/utils/pdf";
|
} from "@/utils/pdf";
|
||||||
|
import { Group } from "@/interfaces/user";
|
||||||
|
|
||||||
interface GroupScoreSummaryHelper {
|
interface GroupScoreSummaryHelper {
|
||||||
score: [number, number];
|
score: [number, number];
|
||||||
@@ -345,8 +346,73 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
return result;
|
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 groupScoreSummary = getGroupScoreSummary();
|
||||||
const demographicInformation = user.demographicInformation as DemographicInformation;
|
const demographicInformation =
|
||||||
|
user.demographicInformation as DemographicInformation;
|
||||||
const pdfStream = await ReactPDF.renderToStream(
|
const pdfStream = await ReactPDF.renderToStream(
|
||||||
<GroupTestReport
|
<GroupTestReport
|
||||||
title={title}
|
title={title}
|
||||||
@@ -360,13 +426,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
logo={"public/logo_title.png"}
|
logo={"public/logo_title.png"}
|
||||||
qrcode={qrcode}
|
qrcode={qrcode}
|
||||||
numberOfStudents={numberOfStudents}
|
numberOfStudents={numberOfStudents}
|
||||||
institution="TODO: PLACEHOLDER"
|
institution={institution}
|
||||||
studentsData={studentsData}
|
studentsData={studentsData}
|
||||||
showLevel={showLevel}
|
showLevel={showLevel}
|
||||||
summaryPNG={overallPNG}
|
summaryPNG={overallPNG}
|
||||||
summaryScore={`${(overallResult * 100).toFixed(0)}%`}
|
summaryScore={`${(overallResult * 100).toFixed(0)}%`}
|
||||||
groupScoreSummary={groupScoreSummary}
|
groupScoreSummary={groupScoreSummary}
|
||||||
passportId={demographicInformation?.passport_id || ''}
|
passportId={demographicInformation?.passport_id || ""}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user