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-02-18 14:37:26 +01:00
|
|
|
import { insertImageInPDF, insertTextInPDF } from "@documenso/pdf";
|
2023-02-21 16:15:36 +01:00
|
|
|
import { sendSigningDoneMail } from "@documenso/lib/mail";
|
2023-01-20 13:58:21 +01:00
|
|
|
|
|
|
|
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
2023-01-27 16:54:08 +01:00
|
|
|
const existingUser = await getUserFromToken(req, res);
|
|
|
|
|
const { token: recipientToken } = req.query;
|
2023-02-21 13:06:30 +01:00
|
|
|
const { signatures: signaturesFromBody }: { signatures: any[] } = req.body;
|
2023-01-20 13:58:21 +01:00
|
|
|
|
2023-01-27 18:15:41 +01:00
|
|
|
if (!recipientToken) {
|
2023-02-17 13:37:28 +01:00
|
|
|
return res.status(401).send("Missing recipient token.");
|
2023-01-27 18:15:41 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-21 13:06:30 +01:00
|
|
|
// The recipient who received the signing request
|
2023-01-27 18:15:41 +01:00
|
|
|
const recipient = await prisma.recipient.findFirstOrThrow({
|
|
|
|
|
where: { token: recipientToken?.toString() },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!recipient) {
|
2023-02-17 13:37:28 +01:00
|
|
|
return res.status(401).send("Recipient not found.");
|
2023-01-20 13:58:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-01 19:15:43 +01:00
|
|
|
const document: PrismaDocument = await getDocument(
|
|
|
|
|
recipient.documentId,
|
2023-02-17 13:37:28 +01:00
|
|
|
req,
|
|
|
|
|
res
|
2023-02-01 19:15:43 +01:00
|
|
|
);
|
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-02-21 13:06:30 +01:00
|
|
|
|
|
|
|
|
// todo rename .document to documentImageAsBase64 or sth. like that
|
|
|
|
|
let documentWithSignatureImages = document.document;
|
|
|
|
|
for (const signature of signaturesFromBody) {
|
|
|
|
|
if (!signature.signatureImage && !signature.typedSignature) {
|
|
|
|
|
documentWithSignatureImages = document.document;
|
2023-02-20 18:14:52 +01:00
|
|
|
throw new Error("Cant't save invalid signature.");
|
2023-02-21 13:06:30 +01:00
|
|
|
}
|
2023-02-20 18:14:52 +01:00
|
|
|
|
2023-02-21 13:06:30 +01:00
|
|
|
await saveSignature(signature);
|
|
|
|
|
|
|
|
|
|
const signedField = await prisma.field.findFirstOrThrow({
|
|
|
|
|
where: { id: signature.fieldId },
|
|
|
|
|
include: { Signature: true },
|
2023-02-20 14:49:17 +01:00
|
|
|
});
|
2023-02-21 13:06:30 +01:00
|
|
|
|
|
|
|
|
await insertSignatureInDocument(signedField);
|
|
|
|
|
}
|
2023-02-17 13:37:28 +01:00
|
|
|
|
|
|
|
|
await prisma.recipient.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: recipient.id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
signingStatus: SigningStatus.SIGNED,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-01-27 20:14:32 +01:00
|
|
|
|
|
|
|
|
const unsignedRecipients = await prisma.recipient.findMany({
|
|
|
|
|
where: {
|
2023-02-17 13:37:28 +01:00
|
|
|
documentId: recipient.documentId,
|
2023-01-27 20:14:32 +01:00
|
|
|
signingStatus: SigningStatus.NOT_SIGNED,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-21 13:06:30 +01:00
|
|
|
await prisma.document.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: recipient.documentId,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
document: documentWithSignatureImages,
|
|
|
|
|
status:
|
|
|
|
|
unsignedRecipients.length > 0
|
|
|
|
|
? DocumentStatus.PENDING
|
|
|
|
|
: DocumentStatus.COMPLETED,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-01-27 16:54:08 +01:00
|
|
|
|
2023-02-21 16:15:36 +01:00
|
|
|
if (unsignedRecipients.length === 0) {
|
|
|
|
|
const documentOwner = await prisma.user.findFirstOrThrow({
|
|
|
|
|
where: { id: document.userId },
|
|
|
|
|
select: { email: true, name: true },
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-28 13:27:17 +01:00
|
|
|
document.document = documentWithSignatureImages;
|
|
|
|
|
if (documentOwner)
|
|
|
|
|
await sendSigningDoneMail(recipient, document, documentOwner);
|
2023-02-21 16:15:36 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-27 16:54:08 +01:00
|
|
|
return res.status(200).end();
|
2023-02-21 13:06:30 +01:00
|
|
|
|
|
|
|
|
async function insertSignatureInDocument(signedField: any) {
|
|
|
|
|
if (signedField?.Signature?.signatureImageAsBase64) {
|
|
|
|
|
documentWithSignatureImages = await insertImageInPDF(
|
|
|
|
|
documentWithSignatureImages,
|
|
|
|
|
signedField.Signature
|
|
|
|
|
? signedField.Signature?.signatureImageAsBase64
|
|
|
|
|
: "",
|
|
|
|
|
signedField.positionX,
|
|
|
|
|
signedField.positionY,
|
|
|
|
|
signedField.page
|
|
|
|
|
);
|
|
|
|
|
} else if (signedField?.Signature?.typedSignature) {
|
|
|
|
|
documentWithSignatureImages = await insertTextInPDF(
|
|
|
|
|
documentWithSignatureImages,
|
|
|
|
|
signedField.Signature.typedSignature,
|
|
|
|
|
signedField.positionX,
|
|
|
|
|
signedField.positionY,
|
|
|
|
|
signedField.page
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
documentWithSignatureImages = document.document;
|
|
|
|
|
throw new Error("Invalid signature could not be inserted.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function saveSignature(signature: any) {
|
|
|
|
|
await prisma.signature.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
fieldId: signature.fieldId,
|
|
|
|
|
},
|
|
|
|
|
update: {},
|
|
|
|
|
create: {
|
|
|
|
|
recipientId: recipient.id,
|
|
|
|
|
fieldId: signature.fieldId,
|
|
|
|
|
signatureImageAsBase64: signature.signatureImage
|
|
|
|
|
? signature.signatureImage
|
|
|
|
|
: null,
|
|
|
|
|
typedSignature: signature.typedSignature
|
|
|
|
|
? signature.typedSignature
|
|
|
|
|
: null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-20 13:58:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
|
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
|
|
|
|
|
});
|