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

165 lines
4.5 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, FieldType } from "@prisma/client";
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) {
const existingUser = await getUserFromToken(req, res);
const { token: recipientToken } = req.query;
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
}
// 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.`);
// todo rename .document to documentImageAsBase64 or sth. like that
let documentWithInserts = document.document;
for (const signature of signaturesFromBody) {
if (!signature.signatureImage && !signature.typedSignature) {
documentWithInserts = document.document;
throw new Error("Cant't save invalid signature.");
}
await saveSignature(signature);
const signedField = await prisma.field.findFirstOrThrow({
where: { id: signature.fieldId },
include: { Signature: true },
});
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,
},
});
const dateFields = await prisma.field.findMany({
where: {
documentId: document.id,
type: FieldType.DATE,
},
});
for (const dateField of dateFields) {
documentWithInserts = await insertTextInPDF(
documentWithInserts,
new Date().toDateString(),
dateField.positionX,
dateField.positionY,
dateField.page
);
}
await prisma.document.update({
where: {
id: recipient.documentId,
},
data: {
document: documentWithInserts,
status:
unsignedRecipients.length > 0
? DocumentStatus.PENDING
: DocumentStatus.COMPLETED,
},
});
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 },
});
document.document = documentWithInserts;
2023-02-28 13:27:17 +01:00
if (documentOwner)
await sendSigningDoneMail(recipient, document, documentOwner);
2023-02-21 16:15:36 +01:00
}
return res.status(200).end();
async function insertSignatureInDocument(signedField: any) {
if (signedField?.Signature?.signatureImageAsBase64) {
documentWithInserts = await insertImageInPDF(
documentWithInserts,
signedField.Signature
? signedField.Signature?.signatureImageAsBase64
: "",
signedField.positionX,
signedField.positionY,
signedField.page
);
} else if (signedField?.Signature?.typedSignature) {
documentWithInserts = await insertTextInPDF(
documentWithInserts,
signedField.Signature.typedSignature,
signedField.positionX,
signedField.positionY,
signedField.page
);
} else {
documentWithInserts = 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) }),
});