mirror of
https://github.com/documenso/documenso.git
synced 2025-11-17 02:01:33 +10:00
fix: don't double send error reponses when sending fails
This commit is contained in:
@ -1,25 +1,33 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { sendSigningRequest } from "@documenso/lib/mail";
|
import { sendSigningRequest } from "@documenso/lib/mail";
|
||||||
import { getDocument } from "@documenso/lib/query";
|
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 prisma from "@documenso/prisma";
|
||||||
import { Document as PrismaDocument, SendStatus } from "@prisma/client";
|
import { Document as PrismaDocument, SendStatus } from "@prisma/client";
|
||||||
|
|
||||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
try {
|
||||||
const user = await getUserFromToken(req, res);
|
const user = await getUserFromToken(req, res);
|
||||||
const { id: documentId } = req.query;
|
const { id: documentId } = req.query;
|
||||||
const { resendTo: resendTo = [] } = req.body;
|
const { resendTo: resendTo = [] } = req.body;
|
||||||
|
|
||||||
if (!user) return;
|
if (!user) {
|
||||||
|
return res.status(401).send("Unauthorized");
|
||||||
|
}
|
||||||
|
|
||||||
if (!documentId) {
|
if (!documentId) {
|
||||||
res.status(400).send("Missing parameter documentId.");
|
return res.status(400).send("Missing parameter documentId.");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const document: PrismaDocument = await getDocument(+documentId, req, res);
|
const document: PrismaDocument = await getDocument(+documentId, req, res);
|
||||||
|
|
||||||
if (!document) res.status(404).end(`No document with id ${documentId} found.`);
|
if (!document) {
|
||||||
|
res.status(404).end(`No document with id ${documentId} found.`);
|
||||||
|
}
|
||||||
|
|
||||||
let recipientCondition: any = {
|
let recipientCondition: any = {
|
||||||
documentId: +documentId,
|
documentId: +documentId,
|
||||||
@ -39,20 +47,25 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!recipients.length) return res.status(200).send(recipients.length);
|
if (!recipients.length) {
|
||||||
|
return res.status(200).send(recipients.length);
|
||||||
|
}
|
||||||
|
|
||||||
let sentRequests = 0;
|
let sentRequests = 0;
|
||||||
recipients.forEach(async (recipient) => {
|
recipients.forEach(async (recipient) => {
|
||||||
await sendSigningRequest(recipient, document, user).catch((err) => {
|
await sendSigningRequest(recipient, document, user);
|
||||||
console.log(err);
|
|
||||||
return res.status(502).end("Coud not send request for signing.");
|
|
||||||
});
|
|
||||||
|
|
||||||
sentRequests++;
|
sentRequests++;
|
||||||
|
});
|
||||||
|
|
||||||
if (sentRequests === recipients.length) {
|
if (sentRequests === recipients.length) {
|
||||||
return res.status(200).send(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({
|
export default defaultHandler({
|
||||||
|
|||||||
Reference in New Issue
Block a user