From 156b7a69e7296d26ed5035f4f2418efc77052057 Mon Sep 17 00:00:00 2001 From: Timur Ercan Date: Sun, 19 Mar 2023 14:05:20 +0100 Subject: [PATCH] bugfix doc-130 document does not render because there is no user and the request is still valid --- apps/web/pages/api/documents/[id].ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/web/pages/api/documents/[id].ts b/apps/web/pages/api/documents/[id].ts index 517e11757..c9fbd4644 100644 --- a/apps/web/pages/api/documents/[id].ts +++ b/apps/web/pages/api/documents/[id].ts @@ -18,10 +18,10 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { } let user = null; - + let recipient = null; if (recipientToken) { // Request from signing page without login - const recipient = await prisma.recipient.findFirst({ + recipient = await prisma.recipient.findFirst({ where: { token: recipientToken?.toString(), }, @@ -37,7 +37,14 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { if (!user) return res.status(401).end(); - const document: PrismaDocument = await getDocument(+documentId, req, res); + let document: PrismaDocument | null = null; + if (recipientToken) { + document = await prisma.document.findFirst({ + where: { id: recipient?.Document?.id }, + }); + } else { + document = await getDocument(+documentId, req, res); + } if (!document) res.status(404).end(`No document with id ${documentId} found.`); @@ -45,7 +52,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { const signaturesCount = await prisma.signature.count({ where: { Field: { - documentId: document.id, + documentId: document?.id, }, }, });