Eisgnature Migrations, webhook handling, and clean up.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
|
||||
const { Documenso } = require("@documenso/sdk-typescript");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const logger = require("../utils/logger");
|
||||
const { QUERY_META_FOR_ESIG_COMPLETION, INSERT_ESIGNATURE_DOCUMENT, INSERT_ESIG_AUDIT_TRAIL } = require("../graphql-client/queries");
|
||||
const { QUERY_META_FOR_ESIG_COMPLETION, INSERT_ESIGNATURE_COMPLETED_DOCOUMENT, UPDATE_ESIGNATURE_DOCUMENT, DISTRIBUTE_ESIGNATURE_DOCUMENT } = require("../graphql-client/queries");
|
||||
const { uploadFileBuffer } = require("../media/imgproxy-media");
|
||||
const { log } = require("node-persist");
|
||||
|
||||
const client = require('../graphql-client/graphql-client').client;
|
||||
const documenso = new Documenso({
|
||||
apiKey: "api_asojim0czruv13ud",//Done on a by team basis,
|
||||
@@ -30,38 +30,56 @@ async function esignWebhook(req, res) {
|
||||
body: message
|
||||
});
|
||||
|
||||
//TODO: Implement checks to prevent this from going backwards in status? If a request fails, it retries, which could cause a document marked as completed to be marked as rejected if the rejection event is processed after the completion event.
|
||||
switch (message.event) {
|
||||
case webhookTypeEnums.DOCUMENT_OPENED:
|
||||
await client.request(UPDATE_ESIGNATURE_DOCUMENT, {
|
||||
external_document_id: message.payload?.payload?.id?.toString(),
|
||||
esig_update: {
|
||||
status: "OPENED",
|
||||
opened: true,
|
||||
}
|
||||
})
|
||||
break;
|
||||
case webhookTypeEnums.DOCUMENT_REJECTED:
|
||||
await client.request(UPDATE_ESIGNATURE_DOCUMENT, {
|
||||
external_document_id: message.payload?.payload?.id?.toString(),
|
||||
esig_update: {
|
||||
status: "REJECTED",
|
||||
rejected: true,
|
||||
}
|
||||
})
|
||||
break;
|
||||
case webhookTypeEnums.DOCUMENT_CREATED:
|
||||
//This is largely a throwaway event we know it was created.
|
||||
console.log("Document created event received. Document ID:", message.payload.documentId);
|
||||
console.log("Document created event received. Document ID:", message.payload?.payload?.documentId);
|
||||
// Here you can add any additional processing you want to do when a document is created
|
||||
break;
|
||||
case webhookTypeEnums.DOCUMENT_COMPLETED:
|
||||
console.log("Document completed event received. Document ID:", message.payload.documentId);
|
||||
console.log("Document completed event received. Document ID:", message.payload?.payload?.documentId);
|
||||
await handleDocumentCompleted(message.payload);
|
||||
// Here you can add any additional processing you want to do when a document is completed
|
||||
break;
|
||||
case webhookTypeEnums.DOCUMENT_SIGNED:
|
||||
console.log("Document signed event received. Document ID:", message.payload.documentId);
|
||||
console.log("Document signed event received. Document ID:", message.payload?.payload?.documentId);
|
||||
// Here you can add any additional processing you want to do when a document is signed
|
||||
await client.request(UPDATE_ESIGNATURE_DOCUMENT, {
|
||||
external_document_id: message.payload?.payload?.id?.toString(),
|
||||
esig_update: {
|
||||
status: "SIGNED",
|
||||
}
|
||||
})
|
||||
break;
|
||||
default:
|
||||
console.log(`Unhandled event type: ${message.event}`);
|
||||
res.status(200).json({ message: "Unsupported event type." });
|
||||
logger.log(`esig-webhook-received-unknown`, "ERROR", "redis", "api", {
|
||||
event: message.event,
|
||||
body: message
|
||||
});
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
// const result = await documenso.documents.download({
|
||||
// documentId: req.body.payload.id,
|
||||
// });
|
||||
// result.resultingBuffer = Buffer.from(result.resultingArrayBuffer);
|
||||
// // Save the document to a file for testing purposes
|
||||
// const downloadsDir = path.join(__dirname, '../downloads');
|
||||
// if (!fs.existsSync(downloadsDir)) {
|
||||
// fs.mkdirSync(downloadsDir, { recursive: true });
|
||||
// }
|
||||
// const filePath = path.join(downloadsDir, `document_${req.body.payload.id}.pdf`);
|
||||
// fs.writeFileSync(filePath, result.resultingBuffer);
|
||||
|
||||
// console.log(result)
|
||||
logger.log(`esig-webhook-processed`, "INFO", "redis", "api", { event: message.event, documentId: message.payload?.payload?.id, jobid: message.payload?.payload?.externalId?.split("|")[0] || null });
|
||||
|
||||
res.sendStatus(200)
|
||||
} catch (error) {
|
||||
@@ -69,25 +87,14 @@ async function esignWebhook(req, res) {
|
||||
message: error.message, stack: error.stack,
|
||||
body: req.body
|
||||
});
|
||||
// const downloadsDir = path.join(__dirname, '../downloads');
|
||||
// if (!fs.existsSync(downloadsDir)) {
|
||||
// fs.mkdirSync(downloadsDir, { recursive: true });
|
||||
// }
|
||||
// const filePath = path.join(downloadsDir, `document_${req.body.payload.id}.pdf`);
|
||||
// fs.writeFileSync(filePath, Buffer.from(err.body));
|
||||
// console.error("Error handling esign webhook:", err);
|
||||
res.sendStatus(500)
|
||||
res.status(500).json({ message: "Error processing webhook event.", error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDocumentCompleted(payload = sampleComplete) {
|
||||
|
||||
|
||||
//Check if the bodyshop is on image proxy or not
|
||||
try {
|
||||
//Split the external id to get the uploaded user.
|
||||
const [jobid, uploaded_by] = payload.externalId.split("|");
|
||||
|
||||
if (!jobid || !uploaded_by) {
|
||||
throw new Error(`Invalid externalId format. Expected "jobid|uploaded_by", got "${payload.externalId}"`);
|
||||
}
|
||||
@@ -106,7 +113,7 @@ async function handleDocumentCompleted(payload = sampleComplete) {
|
||||
let key = `${jobs_by_pk.bodyshop.id}/${jobs_by_pk.id}/${replaceAccents(document.filename).replace(/[^A-Z0-9]+/gi, "_")}-${new Date().getTime()}.pdf`;
|
||||
|
||||
if (jobs_by_pk?.bodyshop?.uselocalmediaserver) {
|
||||
//LMS not yet implemented.
|
||||
//TODO:LMS not yet implemented.
|
||||
|
||||
} else {
|
||||
//S3 Upload
|
||||
@@ -125,8 +132,15 @@ async function handleDocumentCompleted(payload = sampleComplete) {
|
||||
s3Key: key,
|
||||
bucket: uploadResult.bucket
|
||||
});
|
||||
const auditEntry = await client.request(INSERT_ESIG_AUDIT_TRAIL, {
|
||||
obj: {
|
||||
|
||||
await client.request(DISTRIBUTE_ESIGNATURE_DOCUMENT, {
|
||||
external_document_id: payload.id.toString(),
|
||||
esig_update: {
|
||||
status: "COMPLETED",
|
||||
completed: true,
|
||||
completed_at: new Date().toISOString()
|
||||
},
|
||||
audit: {
|
||||
jobid: jobs_by_pk.id,
|
||||
bodyshopid: jobs_by_pk.bodyshop.id,
|
||||
operation: `Esignature document with title ${payload.title} (ID: ${payload.documentMeta.id}) has been completed.`,
|
||||
@@ -134,8 +148,10 @@ async function handleDocumentCompleted(payload = sampleComplete) {
|
||||
type: 'esig-complete'
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
//insert the document record with the s3 key and bucket info.
|
||||
await client.request(INSERT_ESIGNATURE_DOCUMENT, {
|
||||
await client.request(INSERT_ESIGNATURE_COMPLETED_DOCOUMENT, {
|
||||
docInput: {
|
||||
jobid: jobs_by_pk.id,
|
||||
uploaded_by: uploaded_by,
|
||||
|
||||
Reference in New Issue
Block a user