mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
recipient token model and sending endpoint
This commit is contained in:
50
apps/web/pages/api/documents/[id]/send.ts
Normal file
50
apps/web/pages/api/documents/[id]/send.ts
Normal 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) }),
|
||||
});
|
||||
@ -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({
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user