- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.5k
Docusign - Download Documents #14831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 3 commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            110 changes: 110 additions & 0 deletions
          
          110 
        
  components/docusign/actions/download-documents/download-documents.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| 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; | ||
| }, | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.