From b881969bd45d7a8fd38070c5f416a81aa7b05c15 Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Sat, 27 Jan 2024 19:10:41 +0000 Subject: [PATCH] Exporting a report from an user now allows access to the user data of the user that did the exam instead of the current session --- src/pages/api/stats/[id]/export.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pages/api/stats/[id]/export.tsx b/src/pages/api/stats/[id]/export.tsx index b3374c4c..824c74d9 100644 --- a/src/pages/api/stats/[id]/export.tsx +++ b/src/pages/api/stats/[id]/export.tsx @@ -126,6 +126,15 @@ async function post(req: NextApiRequest, res: NextApiResponse) { const stats = docsSnap.docs.map((d) => d.data()); // verify if the stats already have a pdf generated const hasPDF = stats.find((s) => s.pdf); + // find the user that generated the stats + const statIndex = stats.findIndex((s) => s.user); + + if(statIndex === -1) { + res.status(401).json({ok: false}); + return; + } + const userId = stats[statIndex].user; + if (hasPDF) { // if it does, return the pdf url @@ -138,7 +147,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) { try { // generate the pdf report - const docUser = await getDoc(doc(db, "users", req.session.user.id)); + const docUser = await getDoc(doc(db, "users", userId)); if (docUser.exists()) { // we'll need the user in order to get the user data (name, email, focus, etc);