|
| 1 | +import docusign from "../../docusign.app.mjs"; |
| 2 | +import fs from "fs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "docusign-download-documents", |
| 6 | + name: "Download Documents", |
| 7 | + 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/)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + docusign, |
| 12 | + account: { |
| 13 | + propDefinition: [ |
| 14 | + docusign, |
| 15 | + "account", |
| 16 | + ], |
| 17 | + }, |
| 18 | + envelopeId: { |
| 19 | + type: "string", |
| 20 | + label: "Envelope ID", |
| 21 | + description: "Identifier of the envelope to download documents from", |
| 22 | + async options({ prevContext }) { |
| 23 | + const baseUri = await this.docusign.getBaseUri({ |
| 24 | + accountId: this.account, |
| 25 | + }); |
| 26 | + const { startPosition } = prevContext; |
| 27 | + const { |
| 28 | + envelopes = [], nextUri, endPosition, |
| 29 | + } = await this.docusign.listEnvelopes(baseUri, { |
| 30 | + start_position: startPosition, |
| 31 | + from_date: "2000-01-01", |
| 32 | + }); |
| 33 | + return { |
| 34 | + options: envelopes.map(({ |
| 35 | + envelopeId: value, emailSubject: label, |
| 36 | + }) => ({ |
| 37 | + label, |
| 38 | + value, |
| 39 | + })), |
| 40 | + context: { |
| 41 | + startPosition: nextUri |
| 42 | + ? endPosition + 1 |
| 43 | + : undefined, |
| 44 | + }, |
| 45 | + }; |
| 46 | + }, |
| 47 | + }, |
| 48 | + downloadType: { |
| 49 | + type: "string", |
| 50 | + label: "Download Type", |
| 51 | + description: "Download envelope documents to the `/tmp` directory", |
| 52 | + options: [ |
| 53 | + { |
| 54 | + label: "All Documents (PDF)", |
| 55 | + value: "combined", |
| 56 | + }, |
| 57 | + { |
| 58 | + label: "All Documents (Zip)", |
| 59 | + value: "archive", |
| 60 | + }, |
| 61 | + { |
| 62 | + label: "Certificate (PDF)", |
| 63 | + value: "certificate", |
| 64 | + }, |
| 65 | + { |
| 66 | + label: "Portfolio (PDF)", |
| 67 | + value: "portfolio", |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + filename: { |
| 72 | + type: "string", |
| 73 | + label: "Filename", |
| 74 | + description: "The filename to save the file as in the `/tmp` directory including the file extension (.pdf or .zip)", |
| 75 | + }, |
| 76 | + }, |
| 77 | + methods: { |
| 78 | + getEnvelope($, baseUri, envelopeId) { |
| 79 | + return this.docusign._makeRequest({ |
| 80 | + $, |
| 81 | + config: { |
| 82 | + url: `${baseUri}envelopes/${envelopeId}`, |
| 83 | + }, |
| 84 | + }); |
| 85 | + }, |
| 86 | + async downloadToTmp(baseUri, documentsUri, filePath) { |
| 87 | + const content = await this.docusign._makeRequest({ |
| 88 | + config: { |
| 89 | + url: `${baseUri}${documentsUri.slice(1)}/${this.downloadType}`, |
| 90 | + responseType: "arraybuffer", |
| 91 | + }, |
| 92 | + }); |
| 93 | + const rawcontent = content.toString("base64"); |
| 94 | + const buffer = Buffer.from(rawcontent, "base64"); |
| 95 | + fs.writeFileSync(filePath, buffer); |
| 96 | + }, |
| 97 | + }, |
| 98 | + async run({ $ }) { |
| 99 | + const baseUri = await this.docusign.getBaseUri({ |
| 100 | + accountId: this.account, |
| 101 | + }); |
| 102 | + const envelope = await this.getEnvelope($, baseUri, this.envelopeId); |
| 103 | + const filePath = `/tmp/${this.filename}`; |
| 104 | + await this.downloadToTmp(baseUri, envelope.documentsUri, filePath); |
| 105 | + |
| 106 | + $.export("$summary", `Successfully downloaded files to ${filePath}`); |
| 107 | + |
| 108 | + return filePath; |
| 109 | + }, |
| 110 | +}; |
0 commit comments