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+
0 commit comments