Added pdf download to record page
Reenabled reuse of PDF
This commit is contained in:
@@ -14,12 +14,10 @@ import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import ReactPDF from "@react-pdf/renderer";
|
||||
import TestReport from "@/exams/pdf/test.report";
|
||||
import { ref, uploadBytes } from "firebase/storage";
|
||||
import { Stat } from "@/interfaces/user";
|
||||
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
|
||||
import { User } from "@/interfaces/user";
|
||||
import { Module } from "@/interfaces";
|
||||
import { ModuleScore } from "@/interfaces/module.scores";
|
||||
import qrcode from "qrcode";
|
||||
import { SkillExamDetails } from "@/exams/pdf/details/skill.exam";
|
||||
import { LevelExamDetails } from "@/exams/pdf/details/level.exam";
|
||||
import { calculateBandScore } from "@/utils/score";
|
||||
@@ -146,14 +144,17 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
const stats = docsSnap.docs.map((d) => d.data());
|
||||
// TODO: verify if the stats already have a pdf generated
|
||||
// const hasPDF = stats.find((s) => s.pdf);
|
||||
// verify if the stats already have a pdf generated
|
||||
const hasPDF = stats.find((s) => s.pdf);
|
||||
|
||||
// if (hasPDF) {
|
||||
// // if it does, return the pdf url
|
||||
// res.status(200).end(hasPDF.pdf);
|
||||
// return;
|
||||
// }
|
||||
if (hasPDF) {
|
||||
// if it does, return the pdf url
|
||||
const fileRef = ref(storage, hasPDF.pdf);
|
||||
const url = await getDownloadURL(fileRef);
|
||||
|
||||
res.status(200).end(url);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// generate the pdf report
|
||||
@@ -318,7 +319,8 @@ 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}`);
|
||||
const refName = `exam_report/${fileName}`;
|
||||
const fileRef = ref(storage, refName);
|
||||
|
||||
// upload the pdf to storage
|
||||
const pdfBuffer = await streamToBuffer(pdfStream);
|
||||
@@ -329,10 +331,11 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
// update the stats entries with the pdf url to prevent duplication
|
||||
docsSnap.docs.forEach(async (doc) => {
|
||||
await updateDoc(doc.ref, {
|
||||
pdf: snapshot.ref.fullPath,
|
||||
pdf: refName,
|
||||
});
|
||||
});
|
||||
res.status(200).end(snapshot.ref.fullPath);
|
||||
const url = await getDownloadURL(fileRef);
|
||||
res.status(200).end(url);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -361,10 +364,12 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const stats = docsSnap.docs.map((d) => d.data());
|
||||
|
||||
const pdfUrl = stats.find((s) => s.pdf);
|
||||
const hasPDF = stats.find((s) => s.pdf);
|
||||
|
||||
if (pdfUrl) {
|
||||
return res.end(pdfUrl);
|
||||
if (hasPDF) {
|
||||
const fileRef = ref(storage, hasPDF.pdf);
|
||||
const url = await getDownloadURL(fileRef);
|
||||
return res.redirect(url);
|
||||
}
|
||||
|
||||
res.status(500).end();
|
||||
|
||||
Reference in New Issue
Block a user