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

71 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-20 13:58:21 +01:00
import {
defaultHandler,
defaultResponder,
getUserFromToken,
} from "@documenso/lib/server";
import prisma from "@documenso/prisma";
import { NextApiRequest, NextApiResponse } from "next";
2023-01-27 20:14:32 +01:00
import { SigningStatus, DocumentStatus } from "@prisma/client";
2023-02-01 18:32:59 +01:00
import { getDocument } from "@documenso/lib/query";
import { Document as PrismaDocument } from "@prisma/client";
2023-01-20 13:58:21 +01:00
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
const existingUser = await getUserFromToken(req, res);
const { token: recipientToken } = req.query;
2023-01-20 13:58:21 +01:00
2023-01-27 18:15:41 +01:00
if (!recipientToken) {
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.");
2023-01-20 13:58:21 +01:00
return;
}
2023-02-01 18:32:59 +01:00
const document: PrismaDocument = await getDocument(recipient.documentId, {
res: res,
req: req,
2023-01-20 13:58:21 +01:00
});
2023-01-27 18:15:41 +01:00
if (!document) res.status(404).end(`No document found.`);
2023-01-20 13:58:21 +01:00
2023-01-27 20:14:32 +01:00
// todo sign stuff
const unsignedRecipients = await prisma.recipient.findMany({
where: {
signingStatus: SigningStatus.NOT_SIGNED,
},
});
if (unsignedRecipients.length === 0) {
await prisma.document.update({
where: {
id: recipient.documentId,
},
data: {
status: DocumentStatus.COMPLETED,
},
});
}
2023-01-27 18:15:41 +01:00
await prisma.recipient.update({
where: {
id: recipient.id,
},
data: {
signingStatus: SigningStatus.SIGNED,
},
});
2023-01-20 13:58:21 +01:00
return res.status(200).end();
2023-01-20 13:58:21 +01:00
}
export default defaultHandler({
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
});