mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
🚧 insert dates for datefields on sign
This commit is contained in:
@ -7,7 +7,7 @@ import prisma from "@documenso/prisma";
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { SigningStatus, DocumentStatus } from "@prisma/client";
|
import { SigningStatus, DocumentStatus } from "@prisma/client";
|
||||||
import { getDocument } from "@documenso/lib/query";
|
import { getDocument } from "@documenso/lib/query";
|
||||||
import { Document as PrismaDocument } from "@prisma/client";
|
import { Document as PrismaDocument, FieldType } from "@prisma/client";
|
||||||
import { insertImageInPDF, insertTextInPDF } from "@documenso/pdf";
|
import { insertImageInPDF, insertTextInPDF } from "@documenso/pdf";
|
||||||
import { sendSigningDoneMail } from "@documenso/lib/mail";
|
import { sendSigningDoneMail } from "@documenso/lib/mail";
|
||||||
|
|
||||||
@ -38,10 +38,10 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
if (!document) res.status(404).end(`No document found.`);
|
if (!document) res.status(404).end(`No document found.`);
|
||||||
|
|
||||||
// todo rename .document to documentImageAsBase64 or sth. like that
|
// todo rename .document to documentImageAsBase64 or sth. like that
|
||||||
let documentWithSignatureImages = document.document;
|
let documentWithInserts = document.document;
|
||||||
for (const signature of signaturesFromBody) {
|
for (const signature of signaturesFromBody) {
|
||||||
if (!signature.signatureImage && !signature.typedSignature) {
|
if (!signature.signatureImage && !signature.typedSignature) {
|
||||||
documentWithSignatureImages = document.document;
|
documentWithInserts = document.document;
|
||||||
throw new Error("Cant't save invalid signature.");
|
throw new Error("Cant't save invalid signature.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,12 +71,29 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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({
|
await prisma.document.update({
|
||||||
where: {
|
where: {
|
||||||
id: recipient.documentId,
|
id: recipient.documentId,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
document: documentWithSignatureImages,
|
document: documentWithInserts,
|
||||||
status:
|
status:
|
||||||
unsignedRecipients.length > 0
|
unsignedRecipients.length > 0
|
||||||
? DocumentStatus.PENDING
|
? DocumentStatus.PENDING
|
||||||
@ -90,7 +107,7 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
select: { email: true, name: true },
|
select: { email: true, name: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
document.document = documentWithSignatureImages;
|
document.document = documentWithInserts;
|
||||||
if (documentOwner)
|
if (documentOwner)
|
||||||
await sendSigningDoneMail(recipient, document, documentOwner);
|
await sendSigningDoneMail(recipient, document, documentOwner);
|
||||||
}
|
}
|
||||||
@ -99,8 +116,8 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
async function insertSignatureInDocument(signedField: any) {
|
async function insertSignatureInDocument(signedField: any) {
|
||||||
if (signedField?.Signature?.signatureImageAsBase64) {
|
if (signedField?.Signature?.signatureImageAsBase64) {
|
||||||
documentWithSignatureImages = await insertImageInPDF(
|
documentWithInserts = await insertImageInPDF(
|
||||||
documentWithSignatureImages,
|
documentWithInserts,
|
||||||
signedField.Signature
|
signedField.Signature
|
||||||
? signedField.Signature?.signatureImageAsBase64
|
? signedField.Signature?.signatureImageAsBase64
|
||||||
: "",
|
: "",
|
||||||
@ -109,15 +126,15 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
signedField.page
|
signedField.page
|
||||||
);
|
);
|
||||||
} else if (signedField?.Signature?.typedSignature) {
|
} else if (signedField?.Signature?.typedSignature) {
|
||||||
documentWithSignatureImages = await insertTextInPDF(
|
documentWithInserts = await insertTextInPDF(
|
||||||
documentWithSignatureImages,
|
documentWithInserts,
|
||||||
signedField.Signature.typedSignature,
|
signedField.Signature.typedSignature,
|
||||||
signedField.positionX,
|
signedField.positionX,
|
||||||
signedField.positionY,
|
signedField.positionY,
|
||||||
signedField.page
|
signedField.page
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
documentWithSignatureImages = document.document;
|
documentWithInserts = document.document;
|
||||||
throw new Error("Invalid signature could not be inserted.");
|
throw new Error("Invalid signature could not be inserted.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user