Skip to content

The Open Source DocuSign Alternative.

fix: don't double send error reponses when sending fails

Lucas Smith3f14d8007ae4
diff --git a/apps/web/pages/api/documents/[id]/send.ts b/apps/web/pages/api/documents/[id]/send.ts
index 302e75001..aa93cab24 100644
--- a/apps/web/pages/api/documents/[id]/send.ts
+++ b/apps/web/pages/api/documents/[id]/send.ts
@@ -1,58 +1,71 @@
import { NextApiRequest, NextApiResponse } from "next";
import { sendSigningRequest } from "@documenso/lib/mail";
import { getDocument } from "@documenso/lib/query";
-import { defaultHandler, defaultResponder, getUserFromToken } from "@documenso/lib/server";
+import {
+ defaultHandler,
+ defaultResponder,
+ getUserFromToken,
+} from "@documenso/lib/server";
import prisma from "@documenso/prisma";
import { Document as PrismaDocument, SendStatus } from "@prisma/client";
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
- const user = await getUserFromToken(req, res);
- const { id: documentId } = req.query;
- const { resendTo: resendTo = [] } = req.body;
+ try {
+ const user = await getUserFromToken(req, res);
+ const { id: documentId } = req.query;
+ const { resendTo: resendTo = [] } = req.body;
- if (!user) return;
-
- if (!documentId) {
- res.status(400).send("Missing parameter documentId.");
- return;
- }
+ if (!user) {
+ return res.status(401).send("Unauthorized");
+ }
- const document: PrismaDocument = await getDocument(+documentId, req, res);
+ if (!documentId) {
+ return res.status(400).send("Missing parameter documentId.");
+ }
- if (!document) res.status(404).end(`No document with id ${documentId} found.`);
+ const document: PrismaDocument = await getDocument(+documentId, req, res);
- let recipientCondition: any = {
- documentId: +documentId,
- sendStatus: SendStatus.NOT_SENT,
- };
+ if (!document) {
+ res.status(404).end(`No document with id ${documentId} found.`);
+ }
- if (resendTo.length) {
- recipientCondition = {
+ let recipientCondition: any = {
documentId: +documentId,
- id: { in: resendTo },
+ sendStatus: SendStatus.NOT_SENT,
};
- }
- const recipients = await prisma.recipient.findMany({
- where: {
- ...recipientCondition,
- },
- });
+ if (resendTo.length) {
+ recipientCondition = {
+ documentId: +documentId,
+ id: { in: resendTo },
+ };
+ }
+
+ const recipients = await prisma.recipient.findMany({
+ where: {
+ ...recipientCondition,
+ },
+ });
- if (!recipients.length) return res.status(200).send(recipients.length);
+ if (!recipients.length) {
+ return res.status(200).send(recipients.length);
+ }
- let sentRequests = 0;
- recipients.forEach(async (recipient) => {
- await sendSigningRequest(recipient, document, user).catch((err) => {
- console.log(err);
- return res.status(502).end("Coud not send request for signing.");
+ let sentRequests = 0;
+ recipients.forEach(async (recipient) => {
+ await sendSigningRequest(recipient, document, user);
+
+ sentRequests++;
});
- sentRequests++;
if (sentRequests === recipients.length) {
return res.status(200).send(recipients.length);
}
- });
+
+ return res.status(502).end("Coud not send request for signing.");
+ } catch (err) {
+ return res.status(502).end("Coud not send request for signing.");
+ }
}
export default defaultHandler({