Skip to content

Instantly share code, notes, and snippets.

@EmmanuelDemey
Created May 6, 2024 09:25
Show Gist options
  • Save EmmanuelDemey/8d30b895214e0f8adbea642f52b83ab6 to your computer and use it in GitHub Desktop.
Save EmmanuelDemey/8d30b895214e0f8adbea642f52b83ab6 to your computer and use it in GitHub Desktop.
import d, { Document, EnvelopeDefinition, Recipients, SignHere, Signer, Tabs } from "docusign-esign";
import fs from "fs";
import path from "path";
import { getContractInformationFromRequest } from "../contract/getContractInformation";
import { generateContractWithoutEOF } from "../contract/generateContractPdf";
import { API_ACCOUNT_ID, PROD_API, INTEGRATION_KEY, USER_ID } from "./config";
import { convertContractToBast64 } from "../utils/pdf";
import { generateContractDocumentFileName } from "../contract/generateContractDocumentFileName";
import { generateCarbonCopies } from "./generateCarbonCopies";
const SIGNER_EMAIL = "[email protected]";
const docusign: any = d;
const getToken = async () => {
let dsApiClient = new docusign.ApiClient();
dsApiClient.setBasePath(PROD_API);
const results = await dsApiClient.requestJWTUserToken(
INTEGRATION_KEY,
USER_ID,
["signature"],
fs.readFileSync(path.join(__dirname, "../../private.key")),
3600
);
const accessToken = results.body.access_token;
return accessToken;
};
const getEnvelopesApi = (accessToken: string) => {
const dsApiClient = new docusign.ApiClient();
dsApiClient.setBasePath(PROD_API);
dsApiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
return new docusign.EnvelopesApi(dsApiClient);
};
const makeEnvelope = async ({ creche, family, child, contrat }: any) => {
const env = new docusign.EnvelopeDefinition();
env.emailSubject = `Contrat à signer - ${generateContractDocumentFileName(contrat, child)}`;
const docusignDocument = new docusign.Document();
const doc = generateContractWithoutEOF(creche, family, child, contrat);
docusignDocument.documentBase64 = await convertContractToBast64(doc);
docusignDocument.name = generateContractDocumentFileName(contrat, child);
docusignDocument.fileExtension = "pdf";
docusignDocument.documentId = "1";
env.documents = [docusignDocument];
const signers = [];
const createSigner = (parent: any, recipientId: string) => {
const signer = docusign.Signer.constructFromObject({
email: SIGNER_EMAIL,
name: `${parent?.lastName?.toUpperCase()} ${parent?.firstName}`,
roleName: "Famille",
recipientId,
});
let signHere1 = docusign.SignHere.constructFromObject({
anchorString: "**signature_1**",
anchorYOffset: "10",
anchorUnits: "pixels",
anchorXOffset: "20",
});
let signer1Tabs = docusign.Tabs.constructFromObject({
signHereTabs: [signHere1],
});
signer.tabs = signer1Tabs;
return signer;
};
if (contrat.to === "Parent 1") {
signers.push(createSigner(family?.parent1, "1"));
} else if (contrat.to === "Parent 2") {
signers.push(createSigner(family?.parent2, "2"));
} else {
signers.push(createSigner(family?.parent1, "1"));
signers.push(createSigner(family?.parent2, "2"));
}
let recipients = docusign.Recipients.constructFromObject({
signers,
carbonCopies: generateCarbonCopies(creche),
});
env.recipients = recipients;
env.status = "sent";
return env;
};
export const sendContractToSignViaDocusign = async (request: any, firestore: any) => {
return new Promise(async (resolve) => {
try {
const accessToken = await getToken();
console.log(accessToken);
const api = getEnvelopesApi(accessToken);
console.log("api");
const informations = await getContractInformationFromRequest(firestore, request);
const envelope = await makeEnvelope(informations);
console.log("envelope");
const results = await api.createEnvelope(API_ACCOUNT_ID, { envelopeDefinition: envelope });
let envelopeId = results.envelopeId;
console.log(`Envelope was created. EnvelopeId ${envelopeId}`);
resolve("ok");
} catch (e) {
console.log("Error", e);
resolve("ko");
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment