1+ /**
2+ * @file
3+ * Example 018: Get an envelope's custom field data
4+ * @author DocuSign
5+ */
6+
7+ const path = require ( 'path' )
8+ , docusign = require ( 'docusign-esign' )
9+ , dsConfig = require ( '../../ds_configuration.js' ) . config
10+ ;
11+
12+ const eg018EnvelopeCustomFieldData = exports
13+ , eg = 'eg018' // This example reference.
14+ , mustAuthenticate = '/ds/mustAuthenticate'
15+ , minimumBufferMin = 3
16+ ;
17+
18+
19+ /**
20+ * Get the envelope
21+ * @param {object } req Request obj
22+ * @param {object } res Response obj
23+ */
24+ eg018EnvelopeCustomFieldData . createController = async ( req , res ) => {
25+ // Step 1. Check the token
26+ // At this point we should have a good token. But we
27+ // double-check here to enable a better UX to the user.
28+ let tokenOK = req . dsAuthCodeGrant . checkToken ( minimumBufferMin ) ;
29+ if ( ! tokenOK ) {
30+ req . flash ( 'info' , 'Sorry, you need to re-authenticate.' ) ;
31+ // We could store the parameters of the requested operation
32+ // so it could be restarted automatically.
33+ // But since it should be rare to have a token issue here,
34+ // we'll make the user re-enter the form data after
35+ // authentication.
36+ req . dsAuthCodeGrant . setEg ( req , eg ) ;
37+ res . redirect ( mustAuthenticate ) ;
38+ }
39+ if ( ! req . session . envelopeId ) {
40+ res . render ( 'pages/examples/eg018EnvelopeCustomFieldData' , {
41+ csrfToken : req . csrfToken ( ) ,
42+ title : "Envelope custom field data" ,
43+ envelopeOk : req . session . envelopeId ,
44+ sourceFile : path . basename ( __filename ) ,
45+ sourceUrl : dsConfig . githubExampleUrl + path . basename ( __filename ) ,
46+ documentation : dsConfig . documentation + eg ,
47+ showDoc : dsConfig . documentation
48+ } ) ;
49+ }
50+
51+ // Step 2. Call the worker method
52+ let args = {
53+ accessToken : req . user . accessToken ,
54+ basePath : req . session . basePath ,
55+ accountId : req . session . accountId ,
56+ envelopeId : req . session . envelopeId
57+ }
58+ , results = null
59+ ;
60+
61+ try {
62+ results = await eg018EnvelopeCustomFieldData . worker ( args )
63+ }
64+ catch ( error ) {
65+ let errorBody = error && error . response && error . response . body
66+ // we can pull the DocuSign error code and message from the response body
67+ , errorCode = errorBody && errorBody . errorCode
68+ , errorMessage = errorBody && errorBody . message
69+ ;
70+ // In production, may want to provide customized error messages and
71+ // remediation advice to the user.
72+ res . render ( 'pages/error' , { err : error , errorCode : errorCode , errorMessage : errorMessage } ) ;
73+ }
74+ if ( results ) {
75+ res . render ( 'pages/example_done' , {
76+ title : "Envelope custom field data" ,
77+ h1 : "Envelope custom field data" ,
78+ message : `Results from the EnvelopeCustomFields::list method:` ,
79+ json : JSON . stringify ( results )
80+ } ) ;
81+ }
82+ }
83+
84+ /**
85+ * This function does the work of getting the envelope information
86+ * @param {object } args
87+ */
88+ // ***DS.worker.start ***DS.snippet.1.start
89+ eg018EnvelopeCustomFieldData . worker = async ( args ) => {
90+ let dsApiClient = new docusign . ApiClient ( ) ;
91+ dsApiClient . setBasePath ( args . basePath ) ;
92+ dsApiClient . addDefaultHeader ( 'Authorization' , 'Bearer ' + args . accessToken ) ;
93+ let envelopesApi = new docusign . EnvelopesApi ( dsApiClient )
94+ , results = null ;
95+
96+ // Step 1. Call EnvelopeCustomFields::get
97+ // Exceptions will be caught by the calling function
98+ results = await envelopesApi . listCustomFields ( args . accountId , args . envelopeId , null ) ;
99+ return results ;
100+ }
101+ // ***DS.worker.end ***DS.snippet.1.end
102+
103+
104+ /**
105+ * Form page for this application
106+ */
107+ eg018EnvelopeCustomFieldData . getController = ( req , res ) => {
108+ // Check that the authentication token is ok with a long buffer time.
109+ // If needed, now is the best time to ask the user to authenticate
110+ // since they have not yet entered any information into the form.
111+ let tokenOK = req . dsAuthCodeGrant . checkToken ( ) ;
112+ if ( tokenOK ) {
113+ res . render ( 'pages/examples/eg018EnvelopeCustomFieldData' , {
114+ csrfToken : req . csrfToken ( ) ,
115+ title : "Envelope custom field data" ,
116+ envelopeOk : req . session . envelopeId ,
117+ sourceFile : path . basename ( __filename ) ,
118+ sourceUrl : dsConfig . githubExampleUrl + path . basename ( __filename ) ,
119+ documentation : dsConfig . documentation + eg ,
120+ showDoc : dsConfig . documentation
121+ } ) ;
122+ } else {
123+ // Save the current operation so it will be resumed after authentication
124+ req . dsAuthCodeGrant . setEg ( req , eg ) ;
125+ res . redirect ( mustAuthenticate ) ;
126+ }
127+ }
0 commit comments