Skip to content

Commit 27de9c8

Browse files
committed
click 6
1 parent ba2b457 commit 27de9c8

File tree

5 files changed

+295
-1
lines changed

5 files changed

+295
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const {
3434

3535
const {
3636
eg001click, eg002click, eg003click,
37-
eg004click, eg005click,
37+
eg004click, eg005click, eg006click
3838
} = require("./lib/click/controllers");
3939

4040
const {
@@ -170,6 +170,8 @@ if (examplesApi.examplesApi.isRoomsApi) {
170170
.post('/eg004', eg004click.createController)
171171
.get('/eg005', eg005click.getController)
172172
.post('/eg005', eg005click.createController)
173+
.get('/eg006', eg006click.getController)
174+
.post('/eg006', eg006click.createController)
173175
} else if (examplesApi.examplesApi.isMonitorApi) {
174176
app.get('/eg001', eg001monitor.getController)
175177
.post('/eg001', eg001monitor.createController)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @file
3+
* Example 6: Embed a clickwrap
4+
* @author DocuSign
5+
*/
6+
7+
const path = require("path");
8+
const { embedClickwrap } = require("../examples/embedClickwrap");
9+
const validator = require("validator");
10+
const { getClickwraps } = require("../examples/listClickwraps");
11+
const { getExampleByNumber } = require("../../manifestService");
12+
const dsConfig = require("../../../config/index.js").config;
13+
14+
const eg006EmbedClickwrap = exports;
15+
const exampleNumber = 6;
16+
const eg = `eg00${exampleNumber}`; // This example reference.
17+
const mustAuthenticate = "/ds/mustAuthenticate";
18+
const minimumBufferMin = 3;
19+
//const demoDocumentsPath = path.resolve(__dirname, "../../../demo_documents");
20+
21+
/**
22+
* Create clickwrap
23+
* @param {Object} req Request obj
24+
* @param {Object} res Response obj
25+
*/
26+
eg006EmbedClickwrap.createController = async (req, res) => {
27+
// Step 1. Check the token
28+
// At this point we should have a good token. But we
29+
// double-check here to enable a better UX to the user
30+
const isTokenOK = req.dsAuth.checkToken(minimumBufferMin);
31+
if (!isTokenOK) {
32+
req.flash("info", "Sorry, you need to re-authenticate.");
33+
// Save the current operation so it will be resumed after authentication
34+
req.dsAuth.setEg(req, eg);
35+
return res.redirect(mustAuthenticate);
36+
}
37+
38+
// Get required arguments
39+
const { body } = req;
40+
let results = null;
41+
42+
const documentArgs = {
43+
fullName: validator.escape(body.fullName),
44+
email: validator.escape(body.email),
45+
company: validator.escape(body.company),
46+
jobTitle: validator.escape(body.jobTitle),
47+
date: validator.escape(body.date),
48+
};
49+
50+
const args = {
51+
accessToken: req.user.accessToken,
52+
basePath: dsConfig.clickAPIUrl,
53+
accountId: req.session.accountId,
54+
clickwrapName: req.body.clickwrapName,
55+
clickwrapId: req.body.clickwrapId,
56+
//docFile: path.resolve(demoDocumentsPath, dsConfig.docTermsPdf),
57+
documentArgs: documentArgs
58+
};
59+
60+
61+
// Call the worker method
62+
try {
63+
results = await embedClickwrap(args);
64+
} catch (error) {
65+
const errorBody = error && error.response && error.response.body;
66+
// We can pull the DocuSign error code and message from the response body
67+
const errorCode = errorBody && errorBody.errorCode;
68+
const errorMessage = errorBody && errorBody.message;
69+
// In production, you may want to provide customized error messages and
70+
// remediation advice to the user
71+
res.render("pages/error", { err: error, errorCode, errorMessage });
72+
}
73+
74+
// if (results) {
75+
// // Save for use by other examples that need an clickwrapId
76+
// req.session.clickwrapId = results.clickwrapId;
77+
// req.session.clickwrapName = results.clickwrapName;
78+
// const example = getExampleByNumber(res.locals.manifest, exampleNumber);
79+
// res.render("pages/example_done", {
80+
// title: example.ExampleName,
81+
// message: formatString(example.ResultsPageText, results.clickwrapName),
82+
// json: JSON.stringify(results)
83+
// });
84+
// }
85+
}
86+
87+
88+
/**
89+
* Render page with our form for the example
90+
* @param {Object} req Request obj
91+
* @param {Object} res Response obj
92+
*/
93+
eg006EmbedClickwrap.getController = async (req, res) => {
94+
// Check that the authentication token is okay with a long buffer time.
95+
// If needed, now is the best time to ask the user to authenticate,
96+
// since they have not yet entered any information into the form
97+
const isTokenOK = req.dsAuth.checkToken();
98+
if (!isTokenOK) {
99+
// Save the current operation so it will be resumed after authentication
100+
req.dsAuth.setEg(req, eg);
101+
return res.redirect(mustAuthenticate);
102+
}
103+
104+
const args = {
105+
accessToken: req.user.accessToken,
106+
basePath: dsConfig.clickAPIUrl,
107+
accountId: req.session.accountId,
108+
};
109+
110+
const example = getExampleByNumber(res.locals.manifest, exampleNumber);
111+
const sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
112+
res.render("pages/click-examples/eg006EmbedClickwrap", {
113+
eg: eg, csrfToken: req.csrfToken(),
114+
example: example,
115+
sourceFile: sourceFile,
116+
clickwrapsData: await getClickwraps(args),
117+
sourceUrl: dsConfig.githubExampleUrl + 'click/examples/' + sourceFile,
118+
documentation: dsConfig.documentation + eg,
119+
showDoc: dsConfig.documentation
120+
});
121+
}
122+

lib/click/controllers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ module.exports.eg002click = require('./eg002ActivateClickwrap');
33
module.exports.eg003click = require('./eg003CreateNewClickwrapVersion');
44
module.exports.eg004click = require('./eg004ListClickwraps');
55
module.exports.eg005click = require('./eg005ClickwrapResponses');
6+
module.exports.eg006click = require('./eg006EmbedClickwrap');
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @file
3+
* Example 6: Embed a clickwrap
4+
* @author DocuSign
5+
*/
6+
7+
const fs = require("fs-extra");
8+
const docusignClick = require("docusign-click");
9+
10+
/**
11+
* Work with creating of the clickwrap
12+
* @param {Object} args Arguments for embedding a clickwrap
13+
* @param {string} args.accessToken The access token
14+
* @param {string} args.basePath The API base path URL
15+
* @param {string} args.documentArgs.fullName The email of first signer
16+
* @param {string} args.documentArgs.email The name of first signer
17+
* @param {string} args.documentArgs.company The email of second signer
18+
* @param {string} args.documentArgs.jobTitle The name of second signer
19+
* @param {string} args.documentArgs.date The email of cc recipient
20+
*/
21+
const embedClickwrap = async (args) => {
22+
// Step 3. Construct the request Body
23+
// Create display settings model
24+
// const displaySettings = docusignClick.DisplaySettings.constructFromObject({
25+
// consentButtonText: "I Agree",
26+
// displayName: "Terms of Service",
27+
// downloadable: true,
28+
// format: "modal",
29+
// hasAccept: true,
30+
// mustRead: true,
31+
// requireAccept: true,
32+
// documentDisplay: "document",
33+
// // fullName: args.fullName,
34+
// // email: args.email,
35+
// // company: args.company,
36+
// // title: args.title,
37+
// // date: args.date,
38+
// });
39+
40+
// Create document model
41+
// Read and encode file. Put encoded value to Document entity.
42+
// The reads could raise an exception if the file is not available!
43+
// const documentPdfExample = fs.readFileSync(args.docFile);
44+
// const encodedExampleDocument =
45+
// Buffer.from(documentPdfExample).toString("base64");
46+
// const document = docusignClick.Document.constructFromObject({
47+
// documentBase64: encodedExampleDocument,
48+
// documentName: "Terms of Service",
49+
// fileExtension: "pdf",
50+
// // fullName: args.fullName,
51+
// // email: args.email,
52+
// // company: args.company,
53+
// // title: args.title,
54+
// // date: args.date,
55+
// order: 0,
56+
57+
// });
58+
59+
// Create clickwrapRequest model
60+
// const clickwrapRequest = docusignClick.ClickwrapRequest.constructFromObject({
61+
// displaySettings,
62+
// documents: [document],
63+
// name: args.clickwrapName,
64+
// requireReacceptance: true,
65+
// });
66+
67+
const documentArgs = {
68+
fullName: args.documentArgs.fullName,
69+
email: args.documentArgs.email,
70+
company: args.documentArgs.company,
71+
jobTitle: args.documentArgs.jobTitle,
72+
date: args.documentArgs.date,
73+
};
74+
75+
console.log(documentArgs);
76+
77+
const userAgreement = new docusignClick.UserAgreementRequest.constructFromObject({
78+
clientUserId: documentArgs.email,
79+
documentData: {
80+
fullName: documentArgs.fullName,
81+
email: documentArgs.email,
82+
company: documentArgs.company,
83+
jobTitle: documentArgs.jobTitle,
84+
date: documentArgs.date,
85+
},
86+
// requireReacceptance: true,
87+
});
88+
89+
90+
// Step 4. Call the Click API
91+
const dsApiClient = new docusignClick.ApiClient();
92+
dsApiClient.setBasePath(args.basePath);
93+
dsApiClient.addDefaultHeader("Authorization", "Bearer " + args.accessToken);
94+
const accountApi = new docusignClick.AccountsApi(dsApiClient);
95+
96+
// Embed the clickwrap
97+
const result = await accountApi.createHasAgreed(
98+
args.accountId, args.clickwrapId, {
99+
userAgreement,
100+
});
101+
console.log(`See the embedded clickwrap in the dialog box.`);
102+
return result;
103+
};
104+
105+
module.exports = { embedClickwrap };
106+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<%- include("../../partials/examplesHead") %>
2+
3+
<%- include("../../partials/exampleInfo") %>
4+
5+
<% include("../../partials/functions") %>
6+
7+
<% if (clickwrapsData.clickwraps.length == 0) { %>
8+
<%- formatString(example.RedirectsToOtherCodeExamples[0].RedirectText, formatString('href="eg00{0}"', example.RedirectsToOtherCodeExamples[0].CodeExampleToRedirectTo)) %>
9+
<form class="eg" action="eg001" method="get">
10+
<%- include("../../partials/continueButton") %>
11+
</form>
12+
<% } else { %>
13+
<form class="eg" action="" method="post" data-busy="form-download">
14+
<% if(example.Forms && example.Forms[0].FormName) { %>
15+
<%- example.Forms[0].FormName %>
16+
<% } %>
17+
18+
<div class="form-group">
19+
<label for="clickwrapId"><%= example.Forms[0].Inputs[0].InputName %></label>
20+
<select class="custom-select" id="clickwrapId"
21+
name="clickwrapId">
22+
<% for(var i=0; i < clickwrapsData.clickwraps.length; i++) { %>
23+
<option value="<%=clickwrapsData.clickwraps[i].clickwrapId%>"><%= clickwrapsData.clickwraps[i].clickwrapName %></option>
24+
<% } %>
25+
</select>
26+
</div>
27+
28+
<div class="form-group">
29+
<label for="fullName"><%= example.Forms[0].Inputs[1].InputName %></label>
30+
<input type="text" class="form-control" id="fullName" name="fullName"
31+
placeholder="<%= example.Forms[0].Inputs[1].InputPlaceholder %>" required/>
32+
</div>
33+
34+
<div class="form-group">
35+
<label for="email"><%= example.Forms[0].Inputs[2].InputName %></label>
36+
<input type="email" class="form-control" id="email" name="email"
37+
placeholder="<%= example.Forms[0].Inputs[2].InputPlaceholder %>" required/>
38+
</div>
39+
40+
<div class="form-group">
41+
<label for="company"><%= example.Forms[0].Inputs[3].InputName %></label>
42+
<input type="text" class="form-control" id="company" name="company"
43+
placeholder="<%= example.Forms[0].Inputs[2].InputPlaceholder %>" required/>
44+
</div>
45+
46+
<div class="form-group">
47+
<label for="jobTitle"><%= example.Forms[0].Inputs[4].InputName %></label>
48+
<input type="text" class="form-control" id="jobTitle" name="jobTitle"
49+
placeholder="<%= example.Forms[0].Inputs[3].InputPlaceholder %>" required/>
50+
</div>
51+
52+
<div class="form-group">
53+
<label for="date"><%= example.Forms[0].Inputs[5].InputName %></label>
54+
<input type="date" class="form-control" id="date" name="date"
55+
placeholder="<%= example.Forms[0].Inputs[4].InputPlaceholder %>" required/>
56+
</div>
57+
58+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
59+
<%- include("../../partials/submitButton") %>
60+
</form>
61+
<% } %>
62+
63+
<%- include("../../partials/examplesFoot") %>

0 commit comments

Comments
 (0)