forked from docusign/code-examples-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheg003ListEnvelopes.js
More file actions
128 lines (115 loc) · 4.36 KB
/
Copy patheg003ListEnvelopes.js
File metadata and controls
128 lines (115 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @file
* Example 003: List envelopes in the user's account
* @author DocuSign
*/
const docusign = require('docusign-esign')
, dsConfig = require('../../config/index.js').config
, moment = require('moment')
, path = require('path')
;
const eg003ListEnvelopes = exports
, eg = 'eg003' // This example reference.
, mustAuthenticate = '/ds/mustAuthenticate'
, minimumBufferMin = 3
;
/**
* List envelopes in the user's account
* @param {object} req Request obj
* @param {object} res Response obj
*/
eg003ListEnvelopes.createController = async (req, res) => {
// Step 1. Check the token
// At this point we should have a good token. But we
// double-check here to enable a better UX to the user.
let tokenOK = req.dsAuth.checkToken(minimumBufferMin);
if (! tokenOK) {
req.flash('info', 'Sorry, you need to re-authenticate.');
// We could store the parameters of the requested operation
// so it could be restarted automatically.
// But since it should be rare to have a token issue here,
// we'll make the user re-enter the form data after
// authentication.
req.dsAuth.setEg(req, eg);
res.redirect(mustAuthenticate);
}
// Step 2. Call the worker method
let args = {
accessToken: req.user.accessToken,
basePath: req.session.basePath,
accountId: req.session.accountId,
}
, results = null
;
try {
results = await eg003ListEnvelopes.worker (args)
}
catch (error) {
let errorBody = error && error.response && error.response.body
// we can pull the DocuSign error code and message from the response body
, errorCode = errorBody && errorBody.errorCode
, errorMessage = errorBody && errorBody.message
;
// In production, may want to provide customized error messages and
// remediation advice to the user.
res.render('pages/error', {err: error, errorCode: errorCode, errorMessage: errorMessage});
}
if (results) {
res.render('pages/example_done', {
title: "List envelopes results",
h1: "Envelopes updated",
message: `Results from the Envelopes::listStatusChanges method:`,
json: JSON.stringify(results)
});
}
}
/**
* This function does the work of listing the envelopes
*/
// ***DS.snippet.0.start
eg003ListEnvelopes.worker = async (args) => {
// Data for this method
// args.basePath
// args.accessToken
// args.accountId
let dsApiClient = new docusign.ApiClient();
dsApiClient.setBasePath(args.basePath);
dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + args.accessToken);
let envelopesApi = new docusign.EnvelopesApi(dsApiClient)
, results = null;
// Step 1. List the envelopes
// The Envelopes::listStatusChanges method has many options
// See https://developers.docusign.com/esign-rest-api/reference/Envelopes/Envelopes/listStatusChanges
// The list status changes call requires at least a from_date OR
// a set of envelopeIds. Here we filter using a from_date.
// Here we set the from_date to filter envelopes for the last month
// Use ISO 8601 date format
let options = {fromDate: moment().subtract(30, 'days').format()};
// Exceptions will be caught by the calling function
results = await envelopesApi.listStatusChanges(args.accountId, options);
return results;
}
// ***DS.snippet.0.end
/**
* Form page for this application
*/
eg003ListEnvelopes.getController = (req, res) => {
// Check that the authentication token is ok with a long buffer time.
// If needed, now is the best time to ask the user to authenticate
// since they have not yet entered any information into the form.
let tokenOK = req.dsAuth.checkToken();
if (tokenOK) {
res.render('pages/examples/eg003ListEnvelopes', {
eg: eg, csrfToken: req.csrfToken(),
title: "List envelopes",
sourceFile: path.basename(__filename),
sourceUrl: dsConfig.githubExampleUrl + path.basename(__filename),
documentation: dsConfig.documentation + eg,
showDoc: dsConfig.documentation
});
} else {
// Save the current operation so it will be resumed after authentication
req.dsAuth.setEg(req, eg);
res.redirect(mustAuthenticate);
}
}