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