Skip to content

Commit 3428bd0

Browse files
committed
refactoring
1 parent be39713 commit 3428bd0

8 files changed

+29
-91
lines changed

lib/DSAuthCodeGrant.js

+7-69
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
'use strict';
1515

1616
const moment = require('moment')
17-
, docusign = require('docusign-esign')
1817
, dsConfig = require('../ds_configuration.js').config
1918
, passport = require('passport')
2019
, baseUriSuffix = '/restapi'
@@ -35,22 +34,8 @@ let DSAuthCodeGrant = function _DSAuthCodeGrant(req) {
3534
// For production use, you'd want to store the refresh token in non-volatile storage since it is
3635
// good for 30 days. You'd probably want to encrypt it too.
3736
this._tokenExpiration = req.user && req.user.tokenExpirationTimestamp; // when does the token expire?
38-
this._accountId = req.session && req.session.accountId; // current account
39-
this._accountName = req.session && req.session.accountName; // current account's name
40-
this._basePath = req.session && req.session.basePath; // current base path. eg https://na2.docusign.net/restapi
41-
this._dsApiClient = null; // the docusign sdk instance
42-
this._dsConfig = null;
4337
this._debug = true; // ### DEBUG ### setting
4438

45-
// INITIALIZE
46-
this._dsApiClient = new docusign.ApiClient();
47-
if (this._basePath) {
48-
this._dsApiClient.setBasePath(this._basePath);
49-
}
50-
if (this._accessToken) {
51-
this._dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + this._accessToken);
52-
}
53-
5439
} // end of DSAuthCodeGrant constructor function
5540

5641
// Public constants
@@ -82,12 +67,11 @@ DSAuthCodeGrant.prototype.oauth_callback1 = (req, res, next) => {
8267
passport.authenticate('docusign', { failureRedirect: '/ds/login' })(req, res, next)
8368
}
8469
DSAuthCodeGrant.prototype.oauth_callback2 = function _oauth_callback2(req, res, next) {
85-
//console.log(`Received access_token: ${req.user.accessToken.substring(0,15)}...`);
70+
this._accessToken = req.user.accessToken;
8671
console.log(`Received access_token: |${req.user.accessToken}|`);
8772
console.log(`Expires at ${req.user.tokenExpirationTimestamp.format("dddd, MMMM Do YYYY, h:mm:ss a")}`);
8873
req.flash('info', 'You have authenticated with DocuSign.');
89-
this._dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + req.user.accessToken);
90-
74+
9175
// The DocuSign Passport strategy looks up the user's account information via OAuth::userInfo.
9276
// See https://developers.docusign.com/esign-rest-api/guides/authentication/user-info-endpoints
9377
// The data includes the user's information and information on the accounts the user has access too.
@@ -171,12 +155,7 @@ DSAuthCodeGrant.prototype.logoutCallback = function _logout (req, res) {
171155
* @function
172156
*/
173157
DSAuthCodeGrant.prototype.internalLogout = function _internalLogout(req, res) {
174-
this._accessToken = null;
175-
this._refreshToken = null;
176158
this._tokenExpiration = null;
177-
this._accountId = null;
178-
this._accountName = null;
179-
this._basePath = null;
180159
req.session.accountId = null;
181160
req.session.accountName = null;
182161
req.session.basePath = null;
@@ -202,20 +181,14 @@ DSAuthCodeGrant.prototype.getDefaultAccountInfo = function _getDefaultAccountInf
202181
throw new Error(this.Error_account_not_found)
203182
}
204183
} else {
205-
account = accounts.find(a => a.is_default);
184+
account = accounts.find(a => a.is_default);
206185
}
207186

208187
// Save the account information
209-
this._accountId = account.account_id;
210-
this._accountName = account.account_name;
211-
this._basePath = account.base_uri + baseUriSuffix;
212-
213-
req.session.accountId = this._accountId;
214-
req.session.accountName = this._accountName;
215-
req.session.basePath = this._basePath;
216-
217-
this._dsApiClient.setBasePath(this._basePath);
218-
console.log(`Using account ${this._accountId}: ${this._accountName}`);
188+
req.session.accountId = account.account_id;
189+
req.session.accountName = account.account_name;
190+
req.session.basePath = account.base_uri + baseUriSuffix;
191+
console.log(`Using account ${account.account_id}: ${account.account_name}`);
219192
}
220193

221194
/**
@@ -255,41 +228,6 @@ DSAuthCodeGrant.prototype.setEg = function _setEg(req, eg) {
255228
req.session.eg = eg
256229
}
257230

258-
/**
259-
* Getter for the object's <tt>dsApiClient</tt>
260-
* @function
261-
* @returns {DSApiClient} dsApiClient
262-
*/
263-
DSAuthCodeGrant.prototype.getDSApi = function(){return this._dsApiClient};
264-
265-
/**
266-
* Getter for the object's <tt>accessToken</tt>
267-
* First check its validity via checkToken
268-
* @function
269-
* @returns {BearerToken} accessToken
270-
*/
271-
DSAuthCodeGrant.prototype.getAccessToken = function(){return this._accessToken};
272-
273-
/**
274-
* Getter for the <tt>accountId</tt>
275-
* @function
276-
* @returns {string} accountId
277-
*/
278-
DSAuthCodeGrant.prototype.getAccountId = function(){return this._accountId};
279-
280-
/**
281-
* Getter for the <tt>accountName</tt>
282-
* @function
283-
* @returns {string} accountName
284-
*/
285-
DSAuthCodeGrant.prototype.getAccountName = function(){return this._accountName};
286-
287-
/**
288-
* Getter for the <tt>baseUri</tt>
289-
* @function
290-
* @returns {string} baseUri
291-
*/
292-
DSAuthCodeGrant.prototype.getBasePath = function(){return this._basePath};
293231

294232
/**
295233
* If in debug mode, prints message to the console

lib/examples/eg003ListEnvelopes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ eg003ListEnvelopes.createController = async (req, res) => {
6262
if (results) {
6363
res.render('pages/example_done', {
6464
title: "List envelopes results",
65-
h1: "List envelopes results",
65+
h1: "Envelopes updated",
6666
message: `Results from the Envelopes::listStatusChanges method:`,
6767
json: JSON.stringify(results)
6868
});

lib/examples/eg005EnvelopeRecipients.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ eg005EnvelopeRecipients.createController = async (req, res) => {
4949
}
5050

5151
// Step 2. Call the worker method
52-
let accountId = req.dsAuthCodeGrant.getAccountId()
53-
, dsAPIclient = req.dsAuthCodeGrant.getDSApi()
54-
, args = {
55-
dsAPIclient: dsAPIclient,
56-
accountId: accountId,
57-
envelopeId: req.session.envelopeId
52+
let args = {
53+
accessToken: req.user.accessToken,
54+
basePath: req.session.basePath,
55+
accountId: req.session.accountId,
56+
envelopeId: req.session.envelopeId
5857
}
5958
, results = null
6059
;

lib/examples/eg006EnvelopeDocs.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,14 @@ eg006EnvelopeDocs.createController = async (req, res) => {
107107
*/
108108
// ***DS.worker.start ***DS.snippet.1.start
109109
eg006EnvelopeDocs.worker = async (args) => {
110-
let envelopesApi = new docusign.EnvelopesApi(args.dsAPIclient)
111-
, results = null
112-
;
110+
let dsApiClient = new docusign.ApiClient();
111+
dsApiClient.setBasePath(args.basePath);
112+
dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + args.accessToken);
113+
let envelopesApi = new docusign.EnvelopesApi(dsApiClient);
113114

114115
// Step 1. EnvelopeDocuments::list.
115116
// Exceptions will be caught by the calling function
116-
results = await envelopesApi.listDocuments(args.accountId, args.envelopeId, null);
117+
let results = await envelopesApi.listDocuments(args.accountId, args.envelopeId, null);
117118
return results;
118119
}
119120
// ***DS.worker.end ***DS.snippet.1.end

lib/examples/eg010SendBinaryDocs.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ eg010SendBinaryDocs.createController = async (req, res) => {
5454
signerName: signerName,
5555
ccEmail: ccEmail,
5656
ccName: ccName }
57-
, accountId = req.dsAuthCodeGrant.getAccountId()
5857
, args = {
59-
basePath: basePath = req.dsAuthCodeGrant.getBasePath(),
60-
accessToken: req.dsAuthCodeGrant.getAccessToken(),
61-
accountId: accountId,
58+
accessToken: req.user.accessToken,
59+
basePath: req.session.basePath,
60+
accountId: req.session.accountId,
6261
envelopeArgs: envelopeArgs,
6362
demoDocsPath: demoDocsPath,
6463
doc2File: doc2File,

lib/examples/eg012EmbeddedConsole.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ eg012EmbeddedConsole.worker = async (args) => {
9090
let dsApiClient = new docusign.ApiClient();
9191
dsApiClient.setBasePath(args.basePath);
9292
dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + args.accessToken);
93-
let envelopesApi = new docusign.EnvelopesApi(args.dsAPIclient);
93+
let envelopesApi = new docusign.EnvelopesApi(dsApiClient);
9494

9595
// Step 1. create the NDSE view
9696
let viewRequest = makeConsoleViewRequest(args);

lib/examples/eg014CollectPayment.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,17 @@ eg014CollectPayment.createController = async (req, res) => {
103103
*/
104104
// ***DS.worker.start ***DS.snippet.1.start
105105
eg014CollectPayment.worker = async (args) => {
106-
let envelopesApi = new docusign.EnvelopesApi(args.dsAPIclient)
107-
, results = null
108-
;
106+
let dsApiClient = new docusign.ApiClient();
107+
dsApiClient.setBasePath(args.basePath);
108+
dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + args.accessToken);
109+
let envelopesApi = new docusign.EnvelopesApi(dsApiClient);
109110

110111
// Step 1. Make the envelope request body
111112
let envelope = makeEnvelope(args.envelopeArgs)
112113

113114
// Step 2. call Envelopes::create API method
114115
// Exceptions will be caught by the calling function
115-
results = await envelopesApi.createEnvelope(
116+
let results = await envelopesApi.createEnvelope(
116117
args.accountId, {envelopeDefinition: envelope});
117118
let envelopeId = results.envelopeId;
118119
return ({envelopeId: envelopeId})

views/pages/examples/eg003ListEnvelopes.ejs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<% include ../../partials/head %>
22

3-
<h4>3. List envelopes in the user's account</h4>
4-
<p>List the envelopes created in the last 30 days.</p>
3+
<h4>3. List updated envelopes in the user's account</h4>
4+
<p>List the envelopes whose status has changed in the last 30 days.</p>
55
<p>This example demonstrates how to query DocuSign about envelopes sent by the current user.</p>
66

77
<% if (showDoc) { %>

0 commit comments

Comments
 (0)