From c61fc30420bf22563230f62e2a916d487fb2cb13 Mon Sep 17 00:00:00 2001 From: Timur Ercan Date: Fri, 27 Jan 2023 16:54:08 +0100 Subject: [PATCH] recipient token model and sending endpoint --- apps/web/pages/api/documents/[id]/send.ts | 50 +++++++++++++++++++++++ apps/web/pages/api/documents/[id]/sign.ts | 10 +++-- packages/prisma/schema.prisma | 1 + 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 apps/web/pages/api/documents/[id]/send.ts diff --git a/apps/web/pages/api/documents/[id]/send.ts b/apps/web/pages/api/documents/[id]/send.ts new file mode 100644 index 000000000..c39e1ae4a --- /dev/null +++ b/apps/web/pages/api/documents/[id]/send.ts @@ -0,0 +1,50 @@ +import { + defaultHandler, + defaultResponder, + getUserFromToken, +} from "@documenso/lib/server"; +import prisma from "@documenso/prisma"; +import { NextApiRequest, NextApiResponse } from "next"; +import { sendSigningRequestMail } from "@documenso/lib/mail"; +import { SendStatus } from "@prisma/client"; + +async function postHandler(req: NextApiRequest, res: NextApiResponse) { + const user = await getUserFromToken(req, res); + const { id: documentId } = req.query; + + if (!user) return; + + if (!documentId) { + res.status(400).send("Missing parameter documentId."); + return; + } + + let document = await prisma.document.findFirst({ + where: { + id: +documentId, + }, + }); + + if (!document) + res.status(404).end(`No document with id ${documentId} found.`); + + // todo handle sending to single recipient even though more exist + + const recipients = prisma.recipient.findMany({ + where: { documentId: +documentId }, + }); + + (await recipients).forEach(async (recipient) => { + sendSigningRequestMail(recipient, document); + await prisma.recipient.update({ + where: { id: recipient.id }, + data: { sendStatus: SendStatus.SENT }, + }); + }); + + res.status(200); +} + +export default defaultHandler({ + POST: Promise.resolve({ default: defaultResponder(postHandler) }), +}); diff --git a/apps/web/pages/api/documents/[id]/sign.ts b/apps/web/pages/api/documents/[id]/sign.ts index a48672f89..bae6ecd71 100644 --- a/apps/web/pages/api/documents/[id]/sign.ts +++ b/apps/web/pages/api/documents/[id]/sign.ts @@ -7,9 +7,9 @@ import prisma from "@documenso/prisma"; import { NextApiRequest, NextApiResponse } from "next"; async function postHandler(req: NextApiRequest, res: NextApiResponse) { - const user = await getUserFromToken(req, res); + const existingUser = await getUserFromToken(req, res); const { id: documentId } = req.query; - if (!user) return; + const { token: recipientToken } = req.query; if (!documentId) { res.status(400).send("Missing parameter documentId."); @@ -25,9 +25,13 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { if (!document) res.status(404).end(`No document with id ${documentId} found.`); + const recipients = prisma.recipient.findMany({ + where: { documentId: +documentId }, + }); + // todo sign stuff - res.status(200); + return res.status(200).end(); } export default defaultHandler({ diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma index aaf48c240..deae773de 100644 --- a/packages/prisma/schema.prisma +++ b/packages/prisma/schema.prisma @@ -20,6 +20,7 @@ model Recipient { id Int @id @default(autoincrement()) documentId Int email String @db.VarChar(255) + token String readStatus ReadStatus signingStatus SigningStatus sendStatus SendStatus