Skip to content

Commit 80ba8b5

Browse files
scheduled sending and delayed routing (#59)
* scheduled sending and delayed routing
1 parent 5416dd8 commit 80ba8b5

15 files changed

+853
-272
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const eg001 = require('./lib/eSignature/controllers/eg001EmbeddedSigning');
2525
const {
2626
eg002, eg003, eg004, eg005, eg006, eg007, eg008,
2727
eg009, eg010, eg011, eg012, eg013, eg014, eg015,
28-
eg016, eg017, eg018, eg019, eg020, eg022,
29-
eg023, eg024, eg025, eg026, eg027, eg028, eg029,
30-
eg030, eg031, eg032, eg033, eg034, eg035,
28+
eg016, eg017, eg018, eg019, eg020, eg022, eg023,
29+
eg024, eg025, eg026, eg027, eg028, eg029, eg030,
30+
eg031, eg032, eg033, eg034, eg035, eg036, eg037
3131
} = require("./lib/eSignature/controllers");
3232

3333
const {
@@ -219,6 +219,10 @@ if (examplesApi.examplesApi.isRoomsApi) {
219219
.post('/eg034', eg034.createController)
220220
.get('/eg035', eg035.getController)
221221
.post('/eg035', eg035.createController)
222+
.get('/eg036', eg036.getController)
223+
.post('/eg036', eg036.createController)
224+
.get('/eg037', eg037.getController)
225+
.post('/eg037', eg037.createController)
222226
}
223227

224228
function dsLoginCB1(req, res, next) { req.dsAuthCodeGrant.oauth_callback1(req, res, next) }
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @file
3+
* Example 035: Creates an envelope that would include two documents and add a signer and cc recipients to be notified via email
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const validator = require('validator');
9+
const dsConfig = require('../../../config/index.js').config;
10+
const { scheduleEnvelope } = require('../examples/scheduledSending');
11+
12+
const eg035ScheduledSending = exports;
13+
const eg = 'eg035'; // This example reference.
14+
const mustAuthenticate = '/ds/mustAuthenticate';
15+
const minimumBufferMin = 3;
16+
const demoDocsPath = path.resolve(__dirname, '../../../demo_documents');
17+
const docPdf = 'World_Wide_Corp_lorem.pdf';
18+
19+
/**
20+
* Create the envelope
21+
* @param {object} req Request obj
22+
* @param {object} res Response obj
23+
*/
24+
eg035ScheduledSending.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+
const tokenOK = req.dsAuth.checkToken(minimumBufferMin);
29+
if (! tokenOK) {
30+
req.flash('info', 'Sorry, you need to re-authenticate.');
31+
// Save the current operation so it will be resumed after authentication
32+
req.dsAuth.setEg(req, eg);
33+
res.redirect(mustAuthenticate);
34+
}
35+
36+
// Step 2. Call the worker method
37+
const { body } = req;
38+
const envelopeArgs = {
39+
signerEmail: validator.escape(body.signerEmail),
40+
signerName: validator.escape(body.signerName),
41+
resumeDate: validator.escape(body.resumeDate),
42+
docPdf: path.resolve(demoDocsPath, docPdf)
43+
};
44+
const args = {
45+
accessToken: req.user.accessToken,
46+
basePath: req.session.basePath,
47+
accountId: req.session.accountId,
48+
envelopeArgs: envelopeArgs
49+
};
50+
let results = null;
51+
52+
try {
53+
results = await scheduleEnvelope(args);
54+
}
55+
catch (error) {
56+
const errorBody = error && error.response && error.response.body;
57+
// we can pull the DocuSign error code and message from the response body
58+
const errorCode = errorBody && errorBody.errorCode;
59+
const errorMessage = errorBody && errorBody.message;
60+
// In production, may want to provide customized error messages and
61+
// remediation advice to the user.
62+
res.render('pages/error', {err: error, errorCode, errorMessage});
63+
}
64+
if (results) {
65+
req.session.envelopeId = results.envelopeId; // Save for use by other examples
66+
// which need an envelopeId
67+
res.render('pages/example_done', {
68+
title: "Envelope scheduled",
69+
h1: "Envelope scheduled",
70+
message: `The envelope has been created and scheduled!<br/>Envelope ID ${results.envelopeId}.`
71+
});
72+
}
73+
}
74+
75+
/**
76+
* Form page for this application
77+
*/
78+
eg035ScheduledSending.getController = (req, res) => {
79+
// Check that the authentication token is ok with a long buffer time.
80+
// If needed, now is the best time to ask the user to authenticate
81+
// since they have not yet entered any information into the form.
82+
const tokenOK = req.dsAuth.checkToken();
83+
if (tokenOK) {
84+
sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
85+
res.render('pages/examples/eg035ScheduledSending', {
86+
eg: eg, csrfToken: req.csrfToken(),
87+
title: "Scheduled Sending",
88+
sourceFile: sourceFile,
89+
sourceUrl: dsConfig.githubExampleUrl + 'eSignature/examples/' + sourceFile,
90+
documentation: dsConfig.documentation + eg,
91+
showDoc: dsConfig.documentation
92+
});
93+
} else {
94+
// Save the current operation so it will be resumed after authentication
95+
req.dsAuth.setEg(req, eg);
96+
res.redirect(mustAuthenticate);
97+
}
98+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @file
3+
* Example 036: Demonstrates how to delay an envelope’s delivery between recipients using the delayed routing feature.
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const validator = require('validator');
9+
const dsConfig = require('../../../config/index.js').config;
10+
const { SendEnvelopeWithDelayedRouting } = require('../examples/delayedRouting');
11+
12+
const eg036DelayedRouting = exports;
13+
const eg = 'eg036'; // This example reference.
14+
const mustAuthenticate = '/ds/mustAuthenticate';
15+
const minimumBufferMin = 3;
16+
const demoDocsPath = path.resolve(__dirname, '../../../demo_documents');
17+
const docPdf = 'World_Wide_Corp_lorem.pdf';
18+
19+
/**
20+
* Create the envelope
21+
* @param {object} req Request obj
22+
* @param {object} res Response obj
23+
*/
24+
eg036DelayedRouting.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+
const tokenOK = req.dsAuth.checkToken(minimumBufferMin);
29+
if (! tokenOK) {
30+
req.flash('info', 'Sorry, you need to re-authenticate.');
31+
// Save the current operation so it will be resumed after authentication
32+
req.dsAuth.setEg(req, eg);
33+
res.redirect(mustAuthenticate);
34+
}
35+
36+
// Step 2. Call the worker method
37+
const { body } = req;
38+
const envelopeArgs = {
39+
signer1Email: validator.escape(body.signer1Email),
40+
signer1Name: validator.escape(body.signer1Name),
41+
signer2Email: validator.escape(body.signer2Email),
42+
signer2Name: validator.escape(body.signer2Name),
43+
delay: validator.escape(body.delay),
44+
docPdf: path.resolve(demoDocsPath, docPdf)
45+
};
46+
const args = {
47+
accessToken: req.user.accessToken,
48+
basePath: req.session.basePath,
49+
accountId: req.session.accountId,
50+
envelopeArgs: envelopeArgs
51+
};
52+
let results = null;
53+
54+
try {
55+
results = await SendEnvelopeWithDelayedRouting(args);
56+
}
57+
catch (error) {
58+
const errorBody = error && error.response && error.response.body;
59+
// we can pull the DocuSign error code and message from the response body
60+
const errorCode = errorBody && errorBody.errorCode;
61+
const errorMessage = errorBody && errorBody.message;
62+
// In production, may want to provide customized error messages and
63+
// remediation advice to the user.
64+
res.render('pages/error', {err: error, errorCode, errorMessage});
65+
}
66+
if (results) {
67+
req.session.envelopeId = results.envelopeId; // Save for use by other examples
68+
// which need an envelopeId
69+
res.render('pages/example_done', {
70+
title: "Envelope sent",
71+
h1: "Envelope sent",
72+
message: `The envelope has been created and sent!<br/>Envelope ID ${results.envelopeId}.`
73+
});
74+
}
75+
}
76+
77+
/**
78+
* Form page for this application
79+
*/
80+
eg036DelayedRouting.getController = (req, res) => {
81+
// Check that the authentication token is ok with a long buffer time.
82+
// If needed, now is the best time to ask the user to authenticate
83+
// since they have not yet entered any information into the form.
84+
const tokenOK = req.dsAuth.checkToken();
85+
if (tokenOK) {
86+
sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
87+
res.render('pages/examples/eg036DelayedRouting', {
88+
eg: eg, csrfToken: req.csrfToken(),
89+
title: "Delayed Routing",
90+
sourceFile: sourceFile,
91+
sourceUrl: dsConfig.githubExampleUrl + 'eSignature/examples/' + sourceFile,
92+
documentation: dsConfig.documentation + eg,
93+
showDoc: dsConfig.documentation
94+
});
95+
} else {
96+
// Save the current operation so it will be resumed after authentication
97+
req.dsAuth.setEg(req, eg);
98+
res.redirect(mustAuthenticate);
99+
}
100+
}

lib/eSignature/controllers/eg035SmsDelivery.js renamed to lib/eSignature/controllers/eg037SmsDelivery.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* Example 035: Remote signer, cc, envelope has three documents
3+
* Example 037: Remote signer, cc, envelope has three documents
44
* @author DocuSign
55
*/
66

@@ -9,8 +9,8 @@ const { smsDelivery } = require('../examples/smsDelivery');
99
const validator = require('validator');
1010
const dsConfig = require('../../../config/index.js').config;
1111

12-
const eg035SmsDelivery = exports;
13-
const eg = 'eg035'; // This example reference.
12+
const eg037SmsDelivery = exports;
13+
const eg = 'eg037'; // This example reference.
1414
const mustAuthenticate = '/ds/mustAuthenticate';
1515
const minimumBufferMin = 3;
1616
const demoDocsPath = path.resolve(__dirname, '../../../demo_documents');
@@ -23,7 +23,7 @@ const doc3File = 'World_Wide_Corp_lorem.pdf';
2323
* @param {object} req Request obj
2424
* @param {object} res Response obj
2525
*/
26-
eg035SmsDelivery.createController = async (req, res) => {
26+
eg037SmsDelivery.createController = async (req, res) => {
2727
// Step 1. Check the token
2828
// At this point we should have a good token. But we
2929
// double-check here to enable a better UX to the user.
@@ -84,14 +84,14 @@ eg035SmsDelivery.createController = async (req, res) => {
8484
/**
8585
* Form page for this application
8686
*/
87-
eg035SmsDelivery.getController = (req, res) => {
87+
eg037SmsDelivery.getController = (req, res) => {
8888
// Check that the authentication token is ok with a long buffer time.
8989
// If needed, now is the best time to ask the user to authenticate
9090
// since they have not yet entered any information into the form.
9191
const tokenOK = req.dsAuth.checkToken();
9292
if (tokenOK) {
9393
sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
94-
res.render('pages/examples/eg035SmsDelivery', {
94+
res.render('pages/examples/eg037SmsDelivery', {
9595
eg: eg, csrfToken: req.csrfToken(),
9696
title: "Signing request by email",
9797
sourceFile: sourceFile,

lib/eSignature/controllers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ module.exports.eg031 = require('./eg031BulkSendEnvelope');
3131
module.exports.eg032 = require('./eg032PauseSignatureWorkflow');
3232
module.exports.eg033 = require('./eg033UnpauseSignatureWorkflow');
3333
module.exports.eg034 = require('./eg034UseConditionalRecipients');
34-
module.exports.eg035 = require('./eg035SmsDelivery');
34+
module.exports.eg035 = require('./eg035ScheduledSending');
35+
module.exports.eg036 = require('./eg036DelayedRouting');
36+
module.exports.eg037 = require('./eg037SmsDelivery');

0 commit comments

Comments
 (0)