Skip to content

Commit dedc219

Browse files
author
nianiB9
committed
2 parents 6d187c6 + 7d45005 commit dedc219

File tree

10 files changed

+425
-13
lines changed

10 files changed

+425
-13
lines changed

config/appsettings.example.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"clickAPIUrl": "https://demo.docusign.net/clickapi",
3030
"adminAPIUrl": "https://api-d.docusign.net/management",
3131
"monitorApiUrl": "https://lens-d.docusign.net",
32-
"webformsApiUrl": "https://apps-d.docusign.com/api/webforms/v1.1",
32+
"webformsApiUrl": "https://apps-d.docusign.com/api/webforms",
3333
"iamBasePath": "https://api-d.docusign.com/v1",
3434
"codeExamplesManifest": "https://raw.githubusercontent.com/docusign/code-examples-csharp/master/manifest/CodeExamplesManifest.json"
3535
}

demo_documents/web-form-config.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const {
5656
} = require('./lib/admin/controllers');
5757

5858
const { eg001connect } = require('./lib/connect/controllers');
59-
const { eg001webforms } = require('./lib/webforms/controllers');
59+
const { eg001webforms, eg002webforms } = require('./lib/webforms/controllers');
6060
const { eg004notary } = require('./lib/notary/controllers');
6161
const { eg001fields } = require('./lib/connectedFields/controllers');
6262
const { eg001Navigator, eg002Navigator } = require('./lib/navigator/controllers');
@@ -287,7 +287,11 @@ app.get('/cneg001', eg001connect.getController)
287287
app.get('/weg001', eg001webforms.getController)
288288
.get('/weg001webForm', eg001webforms.getWebFormCreateController)
289289
.post('/weg001', eg001webforms.createWebFormTemplate)
290-
.post('/weg001webForm', eg001webforms.createWebFormInstance);
290+
.post('/weg001webForm', eg001webforms.createWebFormInstance)
291+
.get('/weg002', eg002webforms.getController)
292+
.get('/weg002webForm', eg002webforms.getWebFormCreateController)
293+
.post('/weg002', eg002webforms.createWebFormTemplate)
294+
.post('/weg002webForm', eg002webforms.createWebFormInstance);
291295

292296
app.get('/neg004', eg004notary.getController)
293297
.post('/neg004', eg004notary.createController);
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* @file
3+
* Example 002: Create a remote instance of a Web Form
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const validator = require('validator');
9+
const { API_TYPES, replaceTemplateId } = require('../../utils.js');
10+
const { getExampleByNumber } = require('../../manifestService');
11+
const dsConfig = require('../../../config/index.js').config;
12+
const { createWebFormInstance, createWebFormTemplate, listWebForms } = require('../examples/createRemoteInstance');
13+
14+
const eg002CreateRemoteInstance = exports;
15+
const exampleNumber = 2;
16+
const eg = `weg00${exampleNumber}`; // This example reference.
17+
const api = API_TYPES.WEBFORMS;
18+
const mustAuthenticate = '/ds/mustAuthenticate';
19+
const minimumBufferMin = 3;
20+
const demoDocsPath = path.resolve(__dirname, '../../../demo_documents');
21+
const docFile = 'World_Wide_Corp_Web_Form.pdf';
22+
const webFormConfigFile = 'web-form-config.json';
23+
24+
/**
25+
* Create the web form template
26+
* @param {object} req Request obj
27+
* @param {object} res Response obj
28+
*/
29+
eg002CreateRemoteInstance.createWebFormTemplate = async (req, res) => {
30+
// Step 1. Check the token
31+
// At this point we should have a good token. But we
32+
// double-check here to enable a better UX to the user.
33+
const isTokenOK = req.dsAuth.checkToken(minimumBufferMin);
34+
if (!isTokenOK) {
35+
req.flash('info', 'Sorry, you need to re-authenticate.');
36+
// Save the current operation so it will be resumed after authentication
37+
req.dsAuth.setEg(req, eg);
38+
return res.redirect(mustAuthenticate);
39+
}
40+
41+
// Step 2. Call the worker method
42+
const args = {
43+
accessToken: req.user.accessToken,
44+
basePath: req.session.basePath,
45+
accountId: req.session.accountId,
46+
templateName: 'Web Form Example Template',
47+
docFile: path.resolve(demoDocsPath, docFile),
48+
};
49+
let webFormTemplateId = null;
50+
51+
try {
52+
webFormTemplateId = await createWebFormTemplate(args);
53+
replaceTemplateId(path.resolve(demoDocsPath, webFormConfigFile), webFormTemplateId);
54+
} catch (error) {
55+
const errorBody = error && error.response && error.response.body;
56+
// we can pull the DocuSign error code and message from the response body
57+
const errorCode = errorBody && errorBody.errorCode;
58+
const errorMessage = errorBody && errorBody.message;
59+
// In production, may want to provide customized error messages and
60+
// remediation advice to the user.
61+
return res.render('pages/error', { err: error, errorCode, errorMessage });
62+
}
63+
if (webFormTemplateId) {
64+
req.session.webFormTemplateId = webFormTemplateId;
65+
return res.redirect(`/${eg}webForm`);
66+
}
67+
};
68+
69+
/**
70+
* Create the web form instance
71+
* @param {object} req Request obj
72+
* @param {object} res Response obj
73+
*/
74+
eg002CreateRemoteInstance.createWebFormInstance = async (req, res) => {
75+
// Step 1. Check the token
76+
// At this point we should have a good token. But we
77+
// double-check here to enable a better UX to the user.
78+
const isTokenOK = req.dsAuth.checkToken(minimumBufferMin);
79+
if (!isTokenOK) {
80+
req.flash('info', 'Sorry, you need to re-authenticate.');
81+
// Save the current operation so it will be resumed after authentication
82+
req.dsAuth.setEg(req, eg);
83+
return res.redirect(mustAuthenticate);
84+
}
85+
86+
// Step 2. Call the worker method
87+
const args = {
88+
accessToken: req.user.accessToken,
89+
basePath: dsConfig.webformsApiUrl,
90+
accountId: req.session.accountId,
91+
signerEmail: validator.escape(dsConfig.signerEmail),
92+
signerName: validator.escape(dsConfig.signerName),
93+
formName: 'Web Form Example Template',
94+
};
95+
let results = null;
96+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
97+
98+
try {
99+
const forms = await listWebForms(args);
100+
101+
if (!forms.items || forms.items.length === 0) {
102+
const errorCode = '404';
103+
const errorMessage = 'No Web Forms found';
104+
const errorInfo = example.CustomErrorTexts[0].ErrorMessage;
105+
return res.render('pages/error', { errorCode, errorMessage, errorInfo });
106+
}
107+
108+
results = await createWebFormInstance(forms.items[0].id, args);
109+
} catch (error) {
110+
const errorBody = error?.response?.body || error?.body;
111+
const errorCode = errorBody?.errorCode || errorBody?.status;
112+
const errorMessage = errorBody?.message;
113+
// In production, may want to provide customized error messages and
114+
// remediation advice to the user.
115+
return res.render('pages/error', { err: error, errorCode, errorMessage });
116+
}
117+
if (results) {
118+
res.render('pages/example_done', {
119+
title: example.ExampleName,
120+
message: formatString(example.ResultsPageText, results.envelopes[0].id, results.id),
121+
});
122+
}
123+
};
124+
125+
/**
126+
* Form page for this application
127+
*/
128+
eg002CreateRemoteInstance.getController = (req, res) => {
129+
// Check that the authentication token is ok with a long buffer time.
130+
// If needed, now is the best time to ask the user to authenticate
131+
// since they have not yet entered any information into the form.
132+
const isTokenOK = req.dsAuth.checkToken();
133+
if (!isTokenOK) {
134+
// Save the current operation so it will be resumed after authentication
135+
req.dsAuth.setEg(req, eg);
136+
return res.redirect(mustAuthenticate);
137+
}
138+
139+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
140+
const sourceFile =
141+
path.basename(__filename)[5].toLowerCase() +
142+
path.basename(__filename).substr(6);
143+
return res.render('pages/webforms-examples/eg002CreateRemoteInstance', {
144+
eg: eg,
145+
csrfToken: req.csrfToken(),
146+
example: example,
147+
sourceFile: sourceFile,
148+
sourceUrl: dsConfig.githubExampleUrl + 'webforms/examples/' + sourceFile,
149+
documentation: dsConfig.documentation + eg,
150+
showDoc: dsConfig.documentation,
151+
});
152+
};
153+
154+
/**
155+
* Form page for this application
156+
*/
157+
eg002CreateRemoteInstance.getWebFormCreateController = (req, res) => {
158+
// Check that the authentication token is ok with a long buffer time.
159+
// If needed, now is the best time to ask the user to authenticate
160+
// since they have not yet entered any information into the form.
161+
const isTokenOK = req.dsAuth.checkToken();
162+
if (!isTokenOK) {
163+
// Save the current operation so it will be resumed after authentication
164+
req.dsAuth.setEg(req, eg);
165+
return res.redirect(mustAuthenticate);
166+
}
167+
168+
if (!req.session.webFormTemplateId) {
169+
return res.redirect(`/${eg}`);
170+
}
171+
172+
const example = getExampleByNumber(res.locals.manifest, exampleNumber, api);
173+
const additionalPageData = example.AdditionalPage.find(p => p.Name === 'create_web_form');
174+
return res.render('pages/webforms-examples/eg002WebFormCreate', {
175+
eg: eg,
176+
csrfToken: req.csrfToken(),
177+
title: example.ExampleName,
178+
description: formatString(additionalPageData.ResultsPageText, 'demo_documents'),
179+
example: example,
180+
});
181+
};

lib/webforms/controllers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
module.exports.eg001webforms = require('./eg001CreateInstance');
1+
module.exports.eg001webforms = require('./eg001CreateInstance');
2+
module.exports.eg002webforms = require('./eg002CreateRemoteInstance');

0 commit comments

Comments
 (0)