Skip to content

Commit b5e7f58

Browse files
added eg006 and eg007 examples
1 parent 47d6269 commit b5e7f58

File tree

11 files changed

+365
-9
lines changed

11 files changed

+365
-9
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ const {
4545
const { eg001monitor } = require("./lib/monitor/controllers/index");
4646

4747
const {
48-
eg001admin, eg002admin, eg003admin, eg004admin, eg005admin
48+
eg001admin, eg002admin, eg003admin,
49+
eg004admin, eg005admin, eg006admin,
50+
eg007admin
4951
} = require("./lib/admin/controllers");
5052

5153
const PORT = process.env.PORT || 3000;
@@ -151,6 +153,10 @@ if (examplesApi.examplesApi.isRoomsApi) {
151153
.get('/eg004status', eg004admin.checkStatus)
152154
.get('/eg005', eg005admin.getController)
153155
.post('/eg005', eg005admin.createController)
156+
.get('/eg006', eg006admin.getController)
157+
.post('/eg006', eg006admin.createController)
158+
.get('/eg007', eg007admin.getController)
159+
.post('/eg007', eg007admin.createController)
154160
} else {
155161
app.get('/eg001', eg001.getController)
156162
.post('/eg001', eg001.createController)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @file
3+
* Example 006: Get user profile data by email
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const { getUserProfileByEmail } = require('../examples/getUserProfileByEmail');
9+
const dsConfig = require('../../../config/index.js').config;
10+
const validator = require('validator');
11+
const { getOrganizationId } = require("../getOrganizationId.js");
12+
13+
const eg006GetUserProfileByEmail = exports;
14+
const eg = 'eg006' // This example reference.;
15+
const mustAuthenticate = '/ds/mustAuthenticate';
16+
const minimumBufferMin = 3;
17+
18+
/**
19+
* Getting user profile by email
20+
* @param {object} req Request obj
21+
* @param {object} res Response obj
22+
*/
23+
eg006GetUserProfileByEmail.createController = async(req, res) => {
24+
// Step 1. Check the token
25+
// At this point we should have a good token. But we
26+
// double-check here to enable a better UX to the user.
27+
const tokenOK = req.dsAuth.checkToken(minimumBufferMin);
28+
if (!tokenOK) {
29+
req.flash('info', 'Sorry, you need to re-authenticate.');
30+
// Save the current operation so it will be resumed after authentication
31+
req.dsAuth.setEg(req, eg);
32+
res.redirect(mustAuthenticate);
33+
}
34+
35+
const { body } = req;
36+
const args = {
37+
accessToken: req.user.accessToken,
38+
email: validator.escape(body.email),
39+
accountId: req.session.accountId,
40+
organizationId: req.session.organizationId,
41+
basePath: dsConfig.adminAPIUrl
42+
};
43+
44+
let results = null;
45+
46+
try {
47+
results = await getUserProfileByEmail(args)
48+
} catch (error) {
49+
// we can pull the DocuSign error code and message from the response body
50+
const errorBody = error && error.response && error.response.body;
51+
const errorCode = errorBody && errorBody.errorCode;
52+
const errorMessage = errorBody && errorBody.message;
53+
54+
// In production, may want to provide customized error messages and
55+
// remediation advice to the user.
56+
res.render('pages/error', { err: error, errorCode, errorMessage });
57+
}
58+
if (results) {
59+
res.render('pages/example_done', {
60+
title: "Get user profile by email",
61+
h1: "Get user profile by email",
62+
message: "Results from UsersApi:getUserDSProfilesByEmail method:",
63+
json: JSON.stringify(results)
64+
});
65+
}
66+
}
67+
68+
/**
69+
* Form page for this application
70+
*/
71+
eg006GetUserProfileByEmail.getController = async (req, res) => {
72+
// Check that the authentication token is ok with a long buffer time.
73+
// If needed, now is the best time to ask the user to authenticate
74+
// since they have not yet entered any information into the form.
75+
76+
let tokenOK = req.dsAuth.checkToken();
77+
if (tokenOK) {
78+
try {
79+
await getOrganizationId(req);
80+
81+
const args = {
82+
accessToken: req.user.accessToken,
83+
accountId: req.session.accountId,
84+
basePath: req.session.basePath,
85+
organizationId: req.session.organizationId
86+
};
87+
88+
sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
89+
res.render('pages/admin-examples/eg006GetUserProfileByEmail', {
90+
eg: eg,
91+
csrfToken: req.csrfToken(),
92+
title: "Get user profile by email",
93+
sourceFile: sourceFile,
94+
sourceUrl: dsConfig.githubExampleUrl + "admin/examples/" + sourceFile,
95+
documentation: dsConfig.documentation + eg,
96+
showDoc: dsConfig.documentation
97+
});
98+
}
99+
catch (error) {
100+
const errorBody = error && error.response && error.response.body;
101+
const errorCode = errorBody && errorBody.errorCode;
102+
const errorMessage = errorBody && errorBody.message;
103+
104+
// In production, may want to provide customized error messages and
105+
// remediation advice to the user.
106+
res.render('pages/error', { err: error, errorCode, errorMessage });
107+
}
108+
109+
} else {
110+
// Save the current operation so it will be resumed after authentication
111+
req.dsAuth.setEg(req, eg);
112+
res.redirect(mustAuthenticate);
113+
}
114+
}
115+
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* @file
3+
* Example 007: Get user profile by user ID
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const { getUserProfileByUserId } = require('../examples/getUserProfileByUserId');
9+
const dsConfig = require('../../../config/index.js').config;
10+
const validator = require('validator');
11+
const { getOrganizationId } = require("../getOrganizationId.js");
12+
13+
const eg006GetUserProfileByEmail = exports;
14+
const eg = 'eg007' // This example reference.;
15+
const mustAuthenticate = '/ds/mustAuthenticate';
16+
const minimumBufferMin = 3;
17+
18+
/**
19+
* Getting user profile by user ID
20+
* @param {object} req Request obj
21+
* @param {object} res Response obj
22+
*/
23+
eg006GetUserProfileByEmail.createController = async(req, res) => {
24+
// Step 1. Check the token
25+
// At this point we should have a good token. But we
26+
// double-check here to enable a better UX to the user.
27+
const tokenOK = req.dsAuth.checkToken(minimumBufferMin);
28+
if (!tokenOK) {
29+
req.flash('info', 'Sorry, you need to re-authenticate.');
30+
// Save the current operation so it will be resumed after authentication
31+
req.dsAuth.setEg(req, eg);
32+
res.redirect(mustAuthenticate);
33+
}
34+
35+
const { body } = req;
36+
const args = {
37+
accessToken: req.user.accessToken,
38+
userId: validator.escape(body.userId),
39+
accountId: req.session.accountId,
40+
organizationId: req.session.organizationId,
41+
basePath: dsConfig.adminAPIUrl
42+
};
43+
44+
let results = null;
45+
46+
try {
47+
results = await getUserProfileByUserId(args)
48+
} catch (error) {
49+
// we can pull the DocuSign error code and message from the response body
50+
const errorBody = error && error.response && error.response.body;
51+
const errorCode = errorBody && errorBody.errorCode;
52+
const errorMessage = errorBody && errorBody.message;
53+
54+
// In production, may want to provide customized error messages and
55+
// remediation advice to the user.
56+
res.render('pages/error', { err: error, errorCode, errorMessage });
57+
}
58+
if (results) {
59+
res.render('pages/example_done', {
60+
title: "Get user profile by user ID",
61+
h1: "Get user profile by user ID",
62+
message: "Results from UsersApi:getUserDSProfile method:",
63+
json: JSON.stringify(results)
64+
});
65+
}
66+
}
67+
68+
/**
69+
* Form page for this application
70+
*/
71+
eg006GetUserProfileByEmail.getController = async (req, res) => {
72+
// Check that the authentication token is ok with a long buffer time.
73+
// If needed, now is the best time to ask the user to authenticate
74+
// since they have not yet entered any information into the form.
75+
76+
let tokenOK = req.dsAuth.checkToken();
77+
if (tokenOK) {
78+
try {
79+
await getOrganizationId(req);
80+
81+
const args = {
82+
accessToken: req.user.accessToken,
83+
accountId: req.session.accountId,
84+
basePath: req.session.basePath,
85+
organizationId: req.session.organizationId
86+
};
87+
88+
sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
89+
res.render('pages/admin-examples/eg007GetUserProfileByUserId', {
90+
eg: eg,
91+
csrfToken: req.csrfToken(),
92+
title: "Get user profile by user ID",
93+
sourceFile: sourceFile,
94+
sourceUrl: dsConfig.githubExampleUrl + "admin/examples/" + sourceFile,
95+
documentation: dsConfig.documentation + eg,
96+
showDoc: dsConfig.documentation
97+
});
98+
}
99+
catch (error) {
100+
const errorBody = error && error.response && error.response.body;
101+
const errorCode = errorBody && errorBody.errorCode;
102+
const errorMessage = errorBody && errorBody.message;
103+
104+
// In production, may want to provide customized error messages and
105+
// remediation advice to the user.
106+
res.render('pages/error', { err: error, errorCode, errorMessage });
107+
}
108+
109+
} else {
110+
// Save the current operation so it will be resumed after authentication
111+
req.dsAuth.setEg(req, eg);
112+
res.redirect(mustAuthenticate);
113+
}
114+
}
115+

lib/admin/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ module.exports.eg002admin = require('./eg002CreateCLMESignUser');
33
module.exports.eg003admin = require('./eg003BulkExportUserData');
44
module.exports.eg004admin = require('./eg004ImportUser');
55
module.exports.eg005admin = require('./eg005AuditUsers');
6+
module.exports.eg006admin = require('./eg006GetUserProfileByEmail');
7+
module.exports.eg007admin = require('./eg007GetUserProfileByUserId');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const docusignAdmin = require('docusign-admin');
2+
3+
/**
4+
* This function gets a user profile by email
5+
*/
6+
const getUserProfileByEmail = async(args) => {
7+
// Data for this method
8+
// args.basePath
9+
// args.accessToken
10+
// args.email
11+
12+
// Step 2 start
13+
const apiClient = new docusignAdmin.ApiClient();
14+
apiClient.setBasePath(args.basePath);
15+
apiClient.addDefaultHeader("Authorization", "Bearer " + args.accessToken);
16+
// Step 2 end
17+
18+
// Step 3 start
19+
const usersApi = new docusignAdmin.UsersApi(apiClient);
20+
return usersApi.getUserDSProfilesByEmail(args.organizationId, { email: args.email });
21+
// Step 3 end
22+
}
23+
24+
module.exports = { getUserProfileByEmail };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const docusignAdmin = require('docusign-admin');
2+
3+
/**
4+
* This function gets a user profile by user ID
5+
*/
6+
const getUserProfileByUserId = async(args) => {
7+
// Data for this method
8+
// args.basePath
9+
// args.accessToken
10+
// args.userId
11+
12+
// Step 2 start
13+
const apiClient = new docusignAdmin.ApiClient();
14+
apiClient.setBasePath(args.basePath);
15+
apiClient.addDefaultHeader("Authorization", "Bearer " + args.accessToken);
16+
// Step 2 end
17+
18+
// Step 3 start
19+
const usersApi = new docusignAdmin.UsersApi(apiClient);
20+
return usersApi.getUserDSProfile(args.organizationId, args.userId);
21+
// Step 3 end
22+
}
23+
24+
module.exports = { getUserProfileByUserId };

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"body-parser": "^1.19.0",
3131
"cookie-parser": "^1.4.4",
3232
"csurf": "^1.10.0",
33-
"docusign-admin": "^1.0.2",
33+
"docusign-admin": "^1.1.0",
3434
"docusign-click": "^1.1.0",
3535
"docusign-esign": "^5.15.0",
3636
"docusign-monitor": "^1.0.1",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<% include ../../partials/examplesHead %>
2+
3+
<h4>6. Get user profile by email</h4>
4+
<p>
5+
Demonstrates how to get user profile data by email.
6+
</p>
7+
8+
<% include ../../partials/docBody %>
9+
10+
<p>API method used:
11+
UsersApi::getUserDSProfilesByEmail.
12+
</p>
13+
14+
<% include ../../partials/gitSource %>
15+
16+
<form class="eg" action="" method="post" data-busy="form">
17+
<div class="form-group">
18+
<label for="email">Email</label>
19+
<input type="email" class="form-control" id="email" name="email"
20+
aria-describedby="emailHelp" placeholder="pat@example.com" required>
21+
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
22+
</div>
23+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
24+
<button type="submit" class="btn btn-docu">Submit</button>
25+
</form>
26+
27+
<% include ../../partials/examplesFoot %>

0 commit comments

Comments
 (0)