Files
sign/apps/web/pages/api/documents/[id]/send.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

import {
defaultHandler,
defaultResponder,
getUserFromToken,
} from "@documenso/lib/server";
import prisma from "@documenso/prisma";
import { NextApiRequest, NextApiResponse } from "next";
2023-02-01 18:32:59 +01:00
import { sendSigningRequest } from "@documenso/lib/mail";
import { getDocument } from "@documenso/lib/query";
2023-02-07 12:54:31 +01:00
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;
if (!user) return;
if (!documentId) {
res.status(400).send("Missing parameter documentId.");
return;
}
2023-02-01 19:15:43 +01:00
const document: PrismaDocument = await getDocument(+documentId, req, res);
if (!document)
res.status(404).end(`No document with id ${documentId} found.`);
// todo handle sending to single recipient even though more exist
2023-02-07 12:54:31 +01:00
const recipients = await prisma.recipient.findMany({
2023-01-27 20:14:32 +01:00
where: {
documentId: +documentId,
2023-02-07 12:54:31 +01:00
sendStatus: SendStatus.NOT_SENT,
2023-01-27 20:14:32 +01:00
},
});
2023-02-07 12:54:31 +01:00
if (!recipients.length) return res.status(200).end("");
2023-02-18 13:19:37 +01:00
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.");
});
});
2023-02-18 13:19:37 +01:00
return res.status(202).end();
// todo check if recipient has an account and show them in their inbox or something
}
export default defaultHandler({
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
});