send sign request, sign status

This commit is contained in:
Timur Ercan
2023-01-27 18:15:41 +01:00
parent 2ebb68ca7a
commit 59f5297dd7
7 changed files with 68 additions and 24 deletions

View File

@ -19,10 +19,18 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
return; return;
} }
let document = await prisma.document.findFirst({ const document = await prisma.document.findFirstOrThrow({
where: { where: {
id: +documentId, id: +documentId,
}, },
include: {
User: {
select: {
name: true,
},
},
Recipient: true,
},
}); });
if (!document) if (!document)
@ -35,14 +43,17 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
}); });
(await recipients).forEach(async (recipient) => { (await recipients).forEach(async (recipient) => {
sendSigningRequestMail(recipient, document); await sendSigningRequestMail(recipient, document);
await prisma.recipient.update({ await prisma.recipient.updateMany({
where: { id: recipient.id }, where: {
id: recipient.id,
sendStatus: SendStatus.NOT_SENT,
},
data: { sendStatus: SendStatus.SENT }, data: { sendStatus: SendStatus.SENT },
}); });
}); });
res.status(200); return res.status(200).end();
} }
export default defaultHandler({ export default defaultHandler({

View File

@ -5,32 +5,45 @@ import {
} from "@documenso/lib/server"; } from "@documenso/lib/server";
import prisma from "@documenso/prisma"; import prisma from "@documenso/prisma";
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
import { SigningStatus } from "@prisma/client";
async function postHandler(req: NextApiRequest, res: NextApiResponse) { async function postHandler(req: NextApiRequest, res: NextApiResponse) {
const existingUser = await getUserFromToken(req, res); const existingUser = await getUserFromToken(req, res);
const { id: documentId } = req.query;
const { token: recipientToken } = req.query; const { token: recipientToken } = req.query;
if (!documentId) { if (!recipientToken) {
res.status(400).send("Missing parameter documentId."); res.status(401).send("Missing recipient token.");
return;
}
const recipient = await prisma.recipient.findFirstOrThrow({
where: { token: recipientToken?.toString() },
});
if (!recipient) {
res.status(401).send("Recipient not found.");
return; return;
} }
let document = await prisma.document.findFirst({ let document = await prisma.document.findFirst({
where: { where: {
id: +documentId, id: recipient.documentId,
}, },
}); });
if (!document) if (!document) res.status(404).end(`No document found.`);
res.status(404).end(`No document with id ${documentId} found.`);
const recipients = prisma.recipient.findMany({ // todo sign ui
where: { documentId: +documentId },
await prisma.recipient.update({
where: {
id: recipient.id,
},
data: {
signingStatus: SigningStatus.SIGNED,
},
}); });
// todo sign stuff
return res.status(200).end(); return res.status(200).end();
} }

View File

@ -1,12 +1,12 @@
import { ReactElement, useEffect } from "react"; import { ReactElement, useEffect } from "react";
import Layout from "../../components/layout"; import Layout from "../../../components/layout";
import { NextPageWithLayout } from "../_app"; import { NextPageWithLayout } from "../../_app";
import { Document, Page, pdfjs } from "react-pdf"; import { Document, Page, pdfjs } from "react-pdf";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib"; import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib";
const PDFViewer = dynamic(() => import("../../components/pdf-viewer"), { const PDFViewer = dynamic(() => import("../../../components/pdf-viewer"), {
ssr: false, ssr: false,
}); });

View File

@ -0,0 +1,22 @@
import Head from "next/head";
import { ReactElement } from "react";
import Layout from "../../../components/layout";
import Logo from "../../../components/logo";
import { NextPageWithLayout } from "../../_app";
const SignPage: NextPageWithLayout = () => {
return (
<>
<Head>
<title>Sign | Documenso</title>
</Head>
Hello, please sign at the dotted line.
</>
);
};
SignPage.getLayout = function getLayout(page: ReactElement) {
return <Layout>{page}</Layout>;
};
export default SignPage;

View File

@ -16,7 +16,7 @@ export const sendMail = async (
); );
await transport.sendMail({ await transport.sendMail({
from: process.env.SENDGRID_API_KEY, from: process.env.MAIL_FROM,
to: to, to: to,
subject: subject, subject: subject,
html: htmlFormattedMessage, html: htmlFormattedMessage,

View File

@ -3,8 +3,7 @@ import { NEXT_PUBLIC_WEBAPP_URL } from "@documenso/lib/constants";
export const sendSignedMail = async (document: any, recipient: any) => { export const sendSignedMail = async (document: any, recipient: any) => {
// todo check if recipient has an account // todo check if recipient has an account
await sendMail(
sendMail(
document.user.email, document.user.email,
`${recipient.email} signed ${document.title}`, `${recipient.email} signed ${document.title}`,
`Hi ${document.user.name}, ${recipient.email} has signed your document ${document.title}. Click <a href="${NEXT_PUBLIC_WEBAPP_URL}/document/${document.id}"> VIEW DOCUMENT</a> to view it now.` `Hi ${document.user.name}, ${recipient.email} has signed your document ${document.title}. Click <a href="${NEXT_PUBLIC_WEBAPP_URL}/document/${document.id}"> VIEW DOCUMENT</a> to view it now.`

View File

@ -1,9 +1,8 @@
import { sendMail } from "./sendMail"; import { sendMail } from "./sendMail";
export const sendSigningRequestMail = async (recipient: any, document: any) => { export const sendSigningRequestMail = async (recipient: any, document: any) => {
sendMail( await sendMail(
recipient.email, recipient.email,
`Please sign ${document.title}`, `Please sign ${document.title}`,
`${document.user.name} has sent you a document to sign. Click <b><a href="https">SIGN DOCUMENT</a></b> to sign it now.` `${document.User.name} has sent you a document to sign. Click <b><a href="${process.env.NEXT_PUBLIC_WEBAPP_URL}/documents/${document.id}/sign">SIGN DOCUMENT</a></b> to sign it now.`
); );
}; };