fix: improve sealing process
Improves the sealing process by being strict on how long certificate generation can take, opting to fail generation and continue sealing. Also changes the ordering of sealing so an error in the process won't also cause a document to be "COMPLETED" since it hasn't been cryptographically sealed yet. The downside to this change is that documents that fail during sealing will require manual intervention as a signer or owner won't be able to *complete* the document.
This commit is contained in:
@@ -137,7 +137,7 @@ export const completeDocumentWithToken = async ({
|
|||||||
await sendPendingEmail({ documentId, recipientId: recipient.id });
|
await sendPendingEmail({ documentId, recipientId: recipient.id });
|
||||||
}
|
}
|
||||||
|
|
||||||
const documents = await prisma.document.updateMany({
|
const haveAllRecipientsSigned = await prisma.document.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: document.id,
|
id: document.id,
|
||||||
Recipient: {
|
Recipient: {
|
||||||
@@ -146,16 +146,21 @@ export const completeDocumentWithToken = async ({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (haveAllRecipientsSigned) {
|
||||||
|
await sealDocument({ documentId: document.id, requestMetadata });
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.document.update({
|
||||||
|
where: {
|
||||||
|
id: document.id,
|
||||||
|
},
|
||||||
data: {
|
data: {
|
||||||
status: DocumentStatus.COMPLETED,
|
|
||||||
completedAt: new Date(),
|
completedAt: new Date(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (documents.count > 0) {
|
|
||||||
await sealDocument({ documentId: document.id, requestMetadata });
|
|
||||||
}
|
|
||||||
|
|
||||||
const updatedDocument = await getDocument({ token, documentId });
|
const updatedDocument = await getDocument({ token, documentId });
|
||||||
|
|
||||||
await triggerWebhook({
|
await triggerWebhook({
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ export const sealDocument = async ({
|
|||||||
const document = await prisma.document.findFirstOrThrow({
|
const document = await prisma.document.findFirstOrThrow({
|
||||||
where: {
|
where: {
|
||||||
id: documentId,
|
id: documentId,
|
||||||
|
Recipient: {
|
||||||
|
every: {
|
||||||
|
signingStatus: SigningStatus.SIGNED,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
documentData: true,
|
documentData: true,
|
||||||
@@ -53,10 +58,6 @@ export const sealDocument = async ({
|
|||||||
throw new Error(`Document ${document.id} has no document data`);
|
throw new Error(`Document ${document.id} has no document data`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (document.status !== DocumentStatus.COMPLETED) {
|
|
||||||
throw new Error(`Document ${document.id} has not been completed`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const recipients = await prisma.recipient.findMany({
|
const recipients = await prisma.recipient.findMany({
|
||||||
where: {
|
where: {
|
||||||
documentId: document.id,
|
documentId: document.id,
|
||||||
@@ -92,9 +93,9 @@ export const sealDocument = async ({
|
|||||||
// !: Need to write the fields onto the document as a hard copy
|
// !: Need to write the fields onto the document as a hard copy
|
||||||
const pdfData = await getFile(documentData);
|
const pdfData = await getFile(documentData);
|
||||||
|
|
||||||
const certificate = await getCertificatePdf({ documentId }).then(async (doc) =>
|
const certificate = await getCertificatePdf({ documentId })
|
||||||
PDFDocument.load(doc),
|
.then(async (doc) => PDFDocument.load(doc))
|
||||||
);
|
.catch(() => null);
|
||||||
|
|
||||||
const doc = await PDFDocument.load(pdfData);
|
const doc = await PDFDocument.load(pdfData);
|
||||||
|
|
||||||
@@ -103,11 +104,13 @@ export const sealDocument = async ({
|
|||||||
doc.getForm().flatten();
|
doc.getForm().flatten();
|
||||||
flattenAnnotations(doc);
|
flattenAnnotations(doc);
|
||||||
|
|
||||||
|
if (certificate) {
|
||||||
const certificatePages = await doc.copyPages(certificate, certificate.getPageIndices());
|
const certificatePages = await doc.copyPages(certificate, certificate.getPageIndices());
|
||||||
|
|
||||||
certificatePages.forEach((page) => {
|
certificatePages.forEach((page) => {
|
||||||
doc.addPage(page);
|
doc.addPage(page);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for (const field of fields) {
|
for (const field of fields) {
|
||||||
await insertFieldInPDF(doc, field);
|
await insertFieldInPDF(doc, field);
|
||||||
@@ -138,6 +141,15 @@ export const sealDocument = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
|
await tx.document.update({
|
||||||
|
where: {
|
||||||
|
id: document.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
status: DocumentStatus.COMPLETED,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
await tx.documentData.update({
|
await tx.documentData.update({
|
||||||
where: {
|
where: {
|
||||||
id: documentData.id,
|
id: documentData.id,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ export const getCertificatePdf = async ({ documentId }: GetCertificatePdfOptions
|
|||||||
|
|
||||||
await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, {
|
await page.goto(`${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encryptedId}`, {
|
||||||
waitUntil: 'networkidle',
|
waitUntil: 'networkidle',
|
||||||
|
timeout: 10_000,
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await page.pdf({
|
const result = await page.pdf({
|
||||||
|
|||||||
Reference in New Issue
Block a user