diff --git a/components/docusign/actions/download-documents/download-documents.mjs b/components/docusign/actions/download-documents/download-documents.mjs new file mode 100644 index 0000000000000..0ca9b852c8ab7 --- /dev/null +++ b/components/docusign/actions/download-documents/download-documents.mjs @@ -0,0 +1,111 @@ +import docusign from "../../docusign.app.mjs"; +import fs from "fs"; + +export default { + key: "docusign-download-documents", + name: "Download Documents", + description: "Download the documents of an envelope to the /tmp directory. [See the documentation here](https://developers.docusign.com/docs/esign-rest-api/how-to/download-envelope-documents/)", + version: "0.0.1", + type: "action", + props: { + docusign, + account: { + propDefinition: [ + docusign, + "account", + ], + }, + envelopeId: { + type: "string", + label: "Envelope ID", + description: "Identifier of the envelope to download documents from", + async options({ prevContext }) { + const baseUri = await this.docusign.getBaseUri({ + accountId: this.account, + }); + const { startPosition } = prevContext; + const { + envelopes = [], nextUri, endPosition, + } = await this.docusign.listEnvelopes(baseUri, { + start_position: startPosition, + from_date: "2000-01-01", + }); + return { + options: envelopes.map(({ + envelopeId: value, emailSubject: label, + }) => ({ + label, + value, + })), + context: { + startPosition: nextUri + ? endPosition + 1 + : undefined, + }, + }; + }, + }, + downloadType: { + type: "string", + label: "Download Type", + description: "Download envelope documents to the `/tmp` directory", + options: [ + { + label: "All Documents (PDF)", + value: "combined", + }, + { + label: "All Documents (Zip)", + value: "archive", + }, + { + label: "Certificate (PDF)", + value: "certificate", + }, + { + label: "Portfolio (PDF)", + value: "portfolio", + }, + ], + }, + filename: { + type: "string", + label: "Filename", + description: "The filename to save the file as in the `/tmp` directory including the file extension (.pdf or .zip)", + }, + }, + methods: { + getEnvelope($, baseUri, envelopeId) { + return this.docusign._makeRequest({ + $, + config: { + url: `${baseUri}envelopes/${envelopeId}`, + }, + }); + }, + async downloadToTmp($, baseUri, documentsUri, filePath) { + const content = await this.docusign._makeRequest({ + $, + config: { + url: `${baseUri}${documentsUri.slice(1)}/${this.downloadType}`, + responseType: "arraybuffer", + }, + }); + const rawcontent = content.toString("base64"); + const buffer = Buffer.from(rawcontent, "base64"); + fs.writeFileSync(filePath, buffer); + }, + }, + async run({ $ }) { + const baseUri = await this.docusign.getBaseUri({ + accountId: this.account, + }); + const envelope = await this.getEnvelope($, baseUri, this.envelopeId); + const filePath = `/tmp/${this.filename}`; + await this.downloadToTmp($, baseUri, envelope.documentsUri, filePath); + + $.export("$summary", `Successfully downloaded files to ${filePath}`); + + return filePath; + }, +}; diff --git a/components/docusign/package.json b/components/docusign/package.json index ad3e845510dfb..12f1f7b65675b 100644 --- a/components/docusign/package.json +++ b/components/docusign/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/docusign", - "version": "0.2.0", + "version": "0.2.1", "description": "Pipedream Docusign Components", "main": "docusign.app.mjs", "keywords": [