recipient token model and sending endpoint

This commit is contained in:
Timur Ercan
2023-01-27 16:54:08 +01:00
parent bf54c5ef58
commit c61fc30420
3 changed files with 58 additions and 3 deletions

View File

@ -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) }),
});

View File

@ -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({