Skip to content

Commit 015b797

Browse files
committed
eg 15, 18
1 parent 34bae9e commit 015b797

File tree

7 files changed

+279
-22
lines changed

7 files changed

+279
-22
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const express = require('express')
3030
, eg012 = require('./lib/examples/eg012EmbeddedConsole')
3131
, eg013 = require('./lib/examples/eg013AddDocToTemplate')
3232
, eg014 = require('./lib/examples/eg014CollectPayment')
33+
, eg015 = require('./lib/examples/eg015EnvelopeTabData')
34+
, eg018 = require('./lib/examples/eg018EnvelopeCustomFieldData')
3335
;
3436

3537
const PORT = process.env.PORT || 5000
@@ -131,6 +133,10 @@ let app = express()
131133
.post('/eg013', eg013.createController)
132134
.get('/eg014', eg014.getController)
133135
.post('/eg014', eg014.createController)
136+
.get('/eg015', eg015.getController)
137+
.post('/eg015', eg015.createController)
138+
.get('/eg018', eg018.getController)
139+
.post('/eg018', eg018.createController)
134140
;
135141

136142
function dsLoginCB1 (req, res, next) {req.dsAuthCodeGrant.oauth_callback1(req, res, next)}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @file
3+
* Example 015: Get an envelope's tab (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 eg015EnvelopeTabData = exports
13+
, eg = 'eg015' // 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+
eg015EnvelopeTabData.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/eg015EnvelopeTabData', {
41+
csrfToken: req.csrfToken(),
42+
title: "Get envelope tab 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 eg015EnvelopeTabData.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: "Get envelope tab data",
77+
h1: "Get envelope tab data",
78+
message: `Results from the EnvelopeFormData::get 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+
eg015EnvelopeTabData.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 EnvelopeFormData::get
97+
// Exceptions will be caught by the calling function
98+
results = await envelopesApi.getFormData(args.accountId, args.envelopeId);
99+
return results;
100+
}
101+
// ***DS.worker.end ***DS.snippet.1.end
102+
103+
104+
/**
105+
* Form page for this application
106+
*/
107+
eg015EnvelopeTabData.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/eg015EnvelopeTabData', {
114+
csrfToken: req.csrfToken(),
115+
title: "Get envelope tab data information",
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+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}

views/pages/examples/eg015EnvelopeTabData.ejs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
still in progress.
99
</p>
1010

11-
<% if (show_doc) { %>
11+
<% if (showDoc) { %>
1212
<p><a target='_blank' href='<%= documentation %>'>Documentation</a> about this example.</p>
1313
<% } %>
1414

@@ -25,16 +25,15 @@
2525
Recommendation: use example 9, then this example, since example 9 includes many tabs of different types.</p>
2626
2727
<form class="eg" action="" method="post" data-busy="form">
28-
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
29-
<button type="submit" class="btn btn-primary">Continue</button>
28+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
29+
<button type="submit" class="btn btn-primary">Continue</button>
3030
</form>
3131
3232
<% } else { %>
33-
<p>Problem: please first create an envelope using <a href="{{ app_url ~ 'index.php?page=' }}eg009">example 9.</a> <br/>
33+
<p>Problem: please first create an envelope using <a href="eg009">example 9.</a> <br/>
3434
Thank you.</p>
3535
36-
<form class="eg" action="{{ app_url ~ 'index.php' }}" method="get">
37-
<input type="hidden" name="page" value="eg009"/>
36+
<form class="eg" action="eg009" method="get">
3837
<button type="submit" class="btn btn-primary">Continue</button>
3938
</form>
4039
<% } %>

views/pages/examples/eg016SetTabValues.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
<label for="signer_email">Signer Email</label>
2222
<input type="email" class="form-control" id="signer_email" name="signer_email"
2323
aria-describedby="emailHelp" placeholder="pat@example.com" required
24-
value="{{ signer_email }}">
24+
value="<%= signer_email %>">
2525
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
2626
</div>
2727
<div class="form-group">
2828
<label for="signer_name">Signer Name</label>
2929
<input type="text" class="form-control" id="signer_name" placeholder="Pat Johnson" name="signer_name"
30-
value="{{ signer_name }}" required>
30+
value="<%= signer_name %>" required>
3131
</div>
32-
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
32+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
3333
<button type="submit" class="btn btn-primary">Submit</button>
3434
</form>
3535

views/pages/examples/eg017SetTemplateTabValues.ejs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ radio button and checkbox tabs.</p>
2222
<label for="signer_email">Signer Email</label>
2323
<input type="email" class="form-control" id="signer_email" name="signer_email"
2424
aria-describedby="emailHelp" placeholder="pat@example.com" required
25-
value="{{ signer_email }}">
25+
value="<%= signer_email %>">
2626
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
2727
</div>
2828
<div class="form-group">
2929
<label for="signer_name">Signer Name</label>
3030
<input type="text" class="form-control" id="signer_name" placeholder="Pat Johnson" name="signer_name"
31-
value="{{ signer_name }}" required>
31+
value="<%= signer_name %>" required>
3232
</div>
3333
<div class="form-group">
3434
<label for="cc_email">CC Email</label>
@@ -41,16 +41,15 @@ radio button and checkbox tabs.</p>
4141
<input type="text" class="form-control" id="cc_name" placeholder="Pat Johnson" name="cc_name"
4242
required>
4343
</div>
44-
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
44+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
4545
<button type="submit" class="btn btn-primary">Submit</button>
4646
</form>
4747
4848
<% } else { %>
49-
<p>Problem: please first create the template by using <a href="{{ app_url ~ 'index.php?page=' }}eg008">example 8.</a> <br/>
49+
<p>Problem: please first create the template by using <a href="eg008">example 8.</a> <br/>
5050
Thank you.</p>
5151
52-
<form class="eg" action="{{ app_url ~ 'index.php' }}" method="get">
53-
<input type="hidden" name="page" value="eg008"/>
52+
<form class="eg" action="eg008" method="get">
5453
<button type="submit" class="btn btn-primary">Continue</button>
5554
</form>
5655
<% } %>

views/pages/examples/eg018EnvelopeCustomFieldData.ejs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ envelope's certificate of completion.</p>
1010
an envelope.
1111
</p>
1212

13-
{% if show_doc %}
13+
<% if (showDoc) { %>
1414
<p><a target='_blank' href='<%= documentation %>'>Documentation</a> about this example.</p>
15-
{% endif %}
15+
<% } %>
1616

1717
<p>API method used:
1818
<a target ='_blank' href="https://developers.docusign.com/esign-rest-api/reference/Envelopes/EnvelopeCustomFields/list">EnvelopeCustomFields::list</a>.
@@ -27,16 +27,15 @@ envelope's certificate of completion.</p>
2727
Recommendation: use example 16 or 17, then this example, since those examples set a custom data field for the envelope.</p>
2828
2929
<form class="eg" action="" method="post" data-busy="form">
30-
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
31-
<button type="submit" class="btn btn-primary">Continue</button>
30+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
31+
<button type="submit" class="btn btn-primary">Continue</button>
3232
</form>
3333
3434
<% } else { %>
35-
<p>Problem: please first create an envelope using <a href="{{ app_url ~ 'index.php?page=' }}eg016">example 16.</a> <br/>
35+
<p>Problem: please first create an envelope using <a href="eg016">example 16.</a> <br/>
3636
Thank you.</p>
3737
38-
<form class="eg" action="{{ app_url ~ 'index.php' }}" method="get">
39-
<input type="hidden" name="page" value="eg016"/>
38+
<form class="eg" action="eg016" method="get">
4039
<button type="submit" class="btn btn-primary">Continue</button>
4140
</form>
4241
<% } %>

0 commit comments

Comments
 (0)