55 */
66
77const path = require ( 'path' ) ;
8- const { applyBrandToTemplate, getBrandsAndTemplates } = require ( '../examples/applyBrandToTemplate' ) ;
8+ const { applyBrandToTemplate, getBrands } = require ( '../examples/applyBrandToTemplate' ) ;
99const validator = require ( 'validator' ) ;
1010const dsConfig = require ( '../../../config/index.js' ) . config ;
1111
@@ -31,51 +31,63 @@ eg030ApplyBrandToTemplate.createController = async (req, res) => {
3131 res . redirect ( mustAuthenticate ) ;
3232 }
3333
34- const { body } = req ;
34+ if ( ! req . session . templateId ) {
35+ res . render ( 'pages/examples/eg030ApplyBrandToTemplate' , {
36+ eg : eg , csrfToken : req . csrfToken ( ) ,
37+ title : "Apply brand to template" ,
38+ templateOk : req . session . templateId ,
39+ sourceFile : path . basename ( __filename ) ,
40+ sourceUrl : dsConfig . githubExampleUrl + 'eSignature/' + path . basename ( __filename ) ,
41+ documentation : dsConfig . documentation + eg ,
42+ showDoc : dsConfig . documentation
43+ } ) ;
44+ } else {
45+ const { body } = req ;
3546
36- //Step 1. Obtain your OAuth token
37- const args = {
38- accessToken : req . user . accessToken , // Represents your {ACCESS_TOKEN}
39- basePath : req . session . basePath ,
40- accountId : req . session . accountId , // Represents your {ACCOUNT_ID}
41- brandId : validator . escape ( body . brandId ) ,
42- templateId : validator . escape ( body . templateId ) ,
43- status : req . status ,
44- templateRoles : [
45- {
46- name : validator . escape ( body . signerName ) ,
47- email : validator . escape ( body . signerEmail ) ,
48- roleName : "signer"
49- } ,
50- {
51- name : validator . escape ( body . ccName ) ,
52- email : validator . escape ( body . ccEmail ) ,
53- roleName : "cc"
54- }
55- ]
56- } ;
57- let results = null ;
47+ //Step 1. Obtain your OAuth token
48+ const args = {
49+ accessToken : req . user . accessToken , // Represents your {ACCESS_TOKEN}
50+ basePath : req . session . basePath ,
51+ accountId : req . session . accountId , // Represents your {ACCOUNT_ID}
52+ brandId : validator . escape ( body . brandId ) ,
53+ templateId : req . session . templateId ,
54+ status : req . status ,
55+ templateRoles : [
56+ {
57+ name : validator . escape ( body . signerName ) ,
58+ email : validator . escape ( body . signerEmail ) ,
59+ roleName : "signer"
60+ } ,
61+ {
62+ name : validator . escape ( body . ccName ) ,
63+ email : validator . escape ( body . ccEmail ) ,
64+ roleName : "cc"
65+ }
66+ ]
67+ } ;
68+ let results = null ;
5869
59- try {
60- // Step 2. Call the eSignature REST API
61- results = await applyBrandToTemplate ( args ) ;
62- }
63- catch ( error ) {
64- const errorBody = error && error . response && error . response . body ;
65- // We can pull the DocuSign error code and message from the response body
66- const errorCode = errorBody && errorBody . errorCode ;
67- const errorMessage = errorBody && errorBody . message ;
68- // In production, you may want to provide customized error messages and
69- // remediation advice to the user
70- res . render ( 'pages/error' , { err : error , errorCode, errorMessage} ) ;
71- }
70+ try {
71+ // Step 2. Call the eSignature REST API
72+ results = await applyBrandToTemplate ( args ) ;
73+ }
74+ catch ( error ) {
75+ const errorBody = error && error . response && error . response . body ;
76+ // We can pull the DocuSign error code and message from the response body
77+ const errorCode = errorBody && errorBody . errorCode ;
78+ const errorMessage = errorBody && errorBody . message ;
79+ // In production, you may want to provide customized error messages and
80+ // remediation advice to the user
81+ res . render ( 'pages/error' , { err : error , errorCode, errorMessage} ) ;
82+ }
7283
73- if ( results ) {
74- res . render ( 'pages/example_done' , {
75- title : "Envelope sent" ,
76- h1 : "Envelope sent" ,
77- message : `The envelope has been created and sent!<br />Envelope ID: ${ results . envelopeId } .`
78- } ) ;
84+ if ( results ) {
85+ res . render ( 'pages/example_done' , {
86+ title : "Envelope sent" ,
87+ h1 : "Envelope sent" ,
88+ message : `The envelope has been created and sent!<br />Envelope ID: ${ results . envelopeId } .`
89+ } ) ;
90+ }
7991 }
8092}
8193
@@ -95,18 +107,18 @@ eg030ApplyBrandToTemplate.getController = async (req, res) => {
95107 basePath : req . session . basePath ,
96108 accountId : req . session . accountId , // represents your {ACCOUNT_ID}
97109 } ;
98- const results = await getBrandsAndTemplates ( args ) ;
110+ const brandsResponse = await getBrands ( args ) ;
99111
100112 sourceFile = ( path . basename ( __filename ) ) [ 5 ] . toLowerCase ( ) + ( path . basename ( __filename ) ) . substr ( 6 ) ;
101113 res . render ( 'pages/examples/eg030ApplyBrandToTemplate' , {
102114 eg : eg , csrfToken : req . csrfToken ( ) ,
103115 title : "Apply brand to template" ,
116+ templateOk : req . session . templateId ,
104117 sourceFile : sourceFile ,
105118 sourceUrl : dsConfig . githubExampleUrl + 'eSignature/examples/' + sourceFile ,
106119 documentation : dsConfig . documentation + eg ,
107120 showDoc : dsConfig . documentation ,
108- brands : results . brandsResponse . brands || [ ] ,
109- templates : results . templatesResponse . envelopeTemplates || [ ]
121+ brands : brandsResponse . brands || [ ]
110122 } ) ;
111123 } else {
112124 // Save the current operation so it will be resumed after authentication
0 commit comments