Eisgnature Migrations, webhook handling, and clean up.
This commit is contained in:
@@ -10,7 +10,7 @@ const documenso = new Documenso({
|
||||
});
|
||||
const JSR_SERVER = "https://reports.test.imex.online";
|
||||
const jsreport = require("@jsreport/nodejs-client");
|
||||
const { QUERY_JOB_FOR_SIGNATURE, INSERT_ESIG_AUDIT_TRAIL } = require("../graphql-client/queries");
|
||||
const { QUERY_JOB_FOR_SIGNATURE, INSERT_ESIGNATURE_DOCUMENT, DISTRIBUTE_ESIGNATURE_DOCUMENT, QUERY_ESIGNATURE_BY_EXTERNAL_ID, UPDATE_ESIGNATURE_DOCUMENT } = require("../graphql-client/queries");
|
||||
|
||||
|
||||
async function distributeDocument(req, res) {
|
||||
@@ -22,8 +22,12 @@ async function distributeDocument(req, res) {
|
||||
documentId,
|
||||
});
|
||||
|
||||
const auditEntry = await client.request(INSERT_ESIG_AUDIT_TRAIL, {
|
||||
obj: {
|
||||
const auditEntry = await client.request(DISTRIBUTE_ESIGNATURE_DOCUMENT, {
|
||||
external_document_id: documentId.toString(),
|
||||
esig_update: {
|
||||
status: "SENT"
|
||||
},
|
||||
audit: {
|
||||
jobid: req.body.jobid,
|
||||
bodyshopid: req.body.bodyshopid,
|
||||
operation: `Esignature document with title ${distributeResult.title} (ID: ${documentId}) distributed to recipients.`,
|
||||
@@ -39,17 +43,31 @@ async function distributeDocument(req, res) {
|
||||
message: error.message, stack: error.stack,
|
||||
body: req.body
|
||||
});
|
||||
res.status(500).json({ error: "An error occurred while distributing the document." });
|
||||
res.status(500).json({ error: "An error occurred while distributing the document.", message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteDocument(req, res) {
|
||||
try {
|
||||
//TODO: Add in logic to check if doc exists, is deletable etc.
|
||||
const client = req.userGraphQLClient;
|
||||
|
||||
const { documentId } = req.body;
|
||||
//TODO: This needs to be hardened to prevent deleting other people's documents, completed ones, etc.
|
||||
const { esignature_documents } = await client.request(QUERY_ESIGNATURE_BY_EXTERNAL_ID, { external_document_id: documentId.toString() });
|
||||
|
||||
if (!esignature_documents || esignature_documents.length === 0) {
|
||||
//return res.status(404).json({ error: "Document not found" });
|
||||
}
|
||||
const deleteResult = await documenso.documents.delete({
|
||||
documentId
|
||||
documentId: (documentId)
|
||||
});
|
||||
|
||||
await client.request(UPDATE_ESIGNATURE_DOCUMENT, {
|
||||
external_document_id: documentId.toString(),
|
||||
esig_update: {
|
||||
status: "DELETED"
|
||||
}
|
||||
})
|
||||
res.json({ success: true, deleteResult });
|
||||
} catch (error) {
|
||||
console.error("Error deleting document:", error?.data);
|
||||
@@ -61,6 +79,23 @@ async function deleteDocument(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
async function viewDocument(req, res) {
|
||||
try {
|
||||
const { documentId } = req.body;
|
||||
const document = await documenso.document.documentDownload({
|
||||
documentId: parseInt(documentId)
|
||||
});
|
||||
res.json({ success: true, document });
|
||||
} catch (error) {
|
||||
console.error("Error viewing document:", error?.data);
|
||||
logger.log(`esig-view-error`, "ERROR", "esig", "api", {
|
||||
message: error.message, stack: error.stack,
|
||||
body: req.body
|
||||
});
|
||||
res.status(500).json({ error: "An error occurred while retrieving the document.", message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async function newEsignDocument(req, res) {
|
||||
try {
|
||||
const client = req.userGraphQLClient;
|
||||
@@ -68,21 +103,18 @@ async function newEsignDocument(req, res) {
|
||||
const { pdf: fileBuffer, esigData } = await RenderTemplate({ client, req })
|
||||
const fileBlob = new Blob([fileBuffer], { type: "application/pdf" });
|
||||
|
||||
|
||||
//Get the Job data.
|
||||
const { jobs_by_pk: jobData } = await client.request(QUERY_JOB_FOR_SIGNATURE, { jobid: req.body.jobid });
|
||||
|
||||
const recipients = [{
|
||||
email: "patrick@imexsystems.ca",//jobData.ownr_ea,
|
||||
name: `${jobData.ownr_fn} ${jobData.ownr_ln}`,
|
||||
role: "SIGNER",
|
||||
}]
|
||||
const createDocumentResponse = await documenso.documents.create({
|
||||
payload: {
|
||||
title: esigData?.title || `Esign request from ${bodyshop.shopname}`,
|
||||
externalId: `${req.body.jobid}|${req.user?.email}`, //Have to pass the uploaded by later on. Limited to 255 chars.
|
||||
recipients: [
|
||||
{
|
||||
email: "allan@imexsystems.ca",//jobData.ownr_ea,
|
||||
name: `${jobData.ownr_fn} ${jobData.ownr_ln}`,
|
||||
role: "SIGNER",
|
||||
}
|
||||
],
|
||||
recipients,
|
||||
meta: {
|
||||
timezone: bodyshop.timezone,
|
||||
dateFormat: "MM/dd/yyyy hh:mm a",
|
||||
@@ -99,7 +131,6 @@ async function newEsignDocument(req, res) {
|
||||
documentId: createDocumentResponse.id,
|
||||
});
|
||||
|
||||
|
||||
if (esigData?.fields && esigData.fields.length > 0) {
|
||||
try {
|
||||
await documenso.envelopes.fields.createMany({
|
||||
@@ -117,16 +148,23 @@ async function newEsignDocument(req, res) {
|
||||
|
||||
const presignToken = await documenso.embedding.embeddingPresignCreateEmbeddingPresignToken({})
|
||||
|
||||
//add to job audit trail.
|
||||
|
||||
const auditEntry = await client.request(INSERT_ESIG_AUDIT_TRAIL, {
|
||||
obj: {
|
||||
const auditEntry = await client.request(INSERT_ESIGNATURE_DOCUMENT, {
|
||||
audit: {
|
||||
jobid: req.body.jobid,
|
||||
bodyshopid: bodyshop.id,
|
||||
operation: `Esignature document created. Subject: ${esigData?.subject || "No subject"}, Message: ${esigData?.message || "No message"}. Document ID: ${createDocumentResponse.id} Envlope ID: ${createDocumentResponse.envelopeId}`,
|
||||
useremail: req.user?.email,
|
||||
type: 'esig-create'
|
||||
|
||||
},
|
||||
esig: {
|
||||
jobid: req.body.jobid,
|
||||
external_document_id: createDocumentResponse.id.toString(),
|
||||
//envelope_id: createDocumentResponse.envelopeId,
|
||||
subject: esigData?.subject || "No subject",
|
||||
message: esigData?.message || "No message",
|
||||
title: esigData?.title || "No title",
|
||||
status: "DRAFT",
|
||||
recipients: recipients,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -283,7 +321,8 @@ const fetchContextData = async ({ templateObject, jsrAuth, req, }) => {
|
||||
module.exports = {
|
||||
newEsignDocument,
|
||||
distributeDocument,
|
||||
deleteDocument
|
||||
deleteDocument,
|
||||
viewDocument
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user