-
Notifications
You must be signed in to change notification settings - Fork 0
/
eg028CreateBrand.js
115 lines (105 loc) · 4.01 KB
/
eg028CreateBrand.js
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
/**
* @file
* Example 028: Create new brand
* @author DocuSign
*/
const path = require('path')
, docusign = require('docusign-esign')
, validator = require('validator')
, dsConfig = require('../../config/index.js').config
;
const eg028CreateBrand = exports
, eg = 'eg028' // This example reference
, mustAuthenticate = '/ds/mustAuthenticate'
, minimumBufferMin = 3
;
/**
* Create the envelope
* @param {object} req Request obj
* @param {object} res Response obj
*/
eg028CreateBrand.createController = async (req, res) => {
// 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.');
// Save the current operation so it will be resumed after authentication
req.dsAuth.setEg(req, eg);
res.redirect(mustAuthenticate);
}
let body = req.body
// Additional data validation might also be appropriate
, brandName = validator.escape(body.brandName)
, defaultBrandLanguage = validator.escape(body.defaultBrandLanguage)
// Step 1. Obtain your OAuth token
args = {
accessToken: req.user.accessToken, // Represents your {ACCESS_TOKEN}
basePath: req.session.basePath,
accountId: req.session.accountId, // Represents your {ACCOUNT_ID}
brandName: brandName,
defaultBrandLanguage: defaultBrandLanguage
}
, results = null
;
let dsApiClient = new docusign.ApiClient();
dsApiClient.setBasePath(args.basePath);
// Step 2. Construct your API headers
dsApiClient.addDefaultHeader('Authorization', 'Bearer ' + args.accessToken);
let accountsApi = new docusign.AccountsApi(dsApiClient)
// Step 3. Construct the request body
let callback = {
brand: {
brandName: args.brandName,
defaultBrandLanguage: args.defaultBrandLanguage
}
}
try {
// Step 4. Call the eSignature REST API
results = await accountsApi.createBrand(args.accountId, callback);
}
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, you 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) {
req.session.brandId = results.brands[0].brandId;
// Save for use by other examples that need an brandId
res.render('pages/example_done', {
title: "New brand sent",
h1: "New brand sent",
message: `The brand has been created!<br />Brand ID: ${results.brands[0].brandId}.`
});
}
}
// ***DS.snippet.0.end
/**
* Form page for this application
*/
eg028CreateBrand.getController = (req, res) => {
// Check that the authentication token is okay 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/eg028CreateBrand', {
eg: eg, csrfToken: req.csrfToken(),
title: "Signing request by email",
sourceFile: path.basename(__filename),
sourceUrl: dsConfig.githubExampleUrl + 'eSignature/' + 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);
}
}