Added initial group report pdf

This commit is contained in:
Joao Ramos
2024-01-09 02:22:54 +00:00
parent 2540398ab0
commit bdf65a7215
8 changed files with 687 additions and 130 deletions

View File

@@ -25,6 +25,11 @@ import { LevelExamDetails } from "@/exams/pdf/details/level.exam";
import { calculateBandScore } from "@/utils/score";
import axios from "axios";
import { moduleLabels } from "@/utils/moduleUtils";
import {
generateQRCode,
getRadialProgressPNG,
streamToBuffer,
} from "@/utils/pdf";
const db = getFirestore(app);
@@ -35,21 +40,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") return post(req, res);
}
export const streamToBuffer = async (
stream: NodeJS.ReadableStream
): Promise<Buffer> => {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
stream.on("data", (data) => {
chunks.push(data);
});
stream.on("end", () => {
resolve(Buffer.concat(chunks));
});
stream.on("error", reject);
});
};
const getExamSummary = (score: number) => {
if (score > 0.8) {
return "Scoring between 81% and 100% on the English exam demonstrates an outstanding level of proficiency in writing, speaking, listening, and reading. Mastery of key concepts is evident across all language domains, showcasing not only a high level of skill but also a dedication to excellence. Continuing to challenge oneself with advanced material in writing, speaking, listening, and reading will further refine the already impressive command of the English language.";
@@ -137,33 +127,6 @@ const handleSkillsFeedbackRequest = async (
}
};
const generateQRCode = async (link: string) => {
try {
const qrCodeDataURL = await qrcode.toDataURL(link);
return qrCodeDataURL;
} catch (error) {
console.error("Error generating QR code:", error);
return null;
}
};
// Radial Progress PNGs were generated with only two colors
// and they use some baseline score (10%, 20%, 30%..)
type RADIAL_PROGRESS_COLOR = "laranja" | "azul";
const getRadialProgressPNG = (
color: RADIAL_PROGRESS_COLOR,
score: number,
total: number
) => {
// calculate the percentage of the score
// and round it to the closest available image
const percent = (score / total) * 100;
const remainder = percent % 10;
const roundedPercent = percent - remainder;
return `public/radial_progress/${color}_${roundedPercent}.png`;
};
async function post(req: NextApiRequest, res: NextApiResponse) {
// verify if it's a logged user that is trying to export
if (req.session.user) {
@@ -178,7 +141,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
);
if (docsSnap.empty) {
res.status(404).end();
res.status(400).end();
return;
}
@@ -288,10 +251,6 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
return result;
});
// generate the file ref for storage
const fileName = `${Date.now().toString()}.pdf`;
const fileRef = ref(storage, `exam_report/${fileName}`);
// calculate the overall score out of all the aggregated results
const overallScore = results.reduce(
(accm, { score }) => accm + score,
@@ -352,6 +311,10 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
/>
);
// generate the file ref for storage
const fileName = `${Date.now().toString()}.pdf`;
const fileRef = ref(storage, `exam_report/${fileName}`);
// upload the pdf to storage
const pdfBuffer = await streamToBuffer(pdfStream);
const snapshot = await uploadBytes(fileRef, pdfBuffer, {
@@ -376,7 +339,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
}
}
res.status(500).json({ ok: false });
res.status(401).json({ ok: false });
return;
}