@@ -9,7 +9,7 @@ const validator = require('validator');
99const { formatString, API_TYPES } = require ( '../../utils.js' ) ;
1010const { getExampleByNumber } = require ( '../../manifestService' ) ;
1111const dsConfig = require ( '../../../config/index.js' ) . config ;
12- const { getWorkflowDefinition, triggerWorkflow } = require ( '../examples/triggerWorkflow' ) ;
12+ const { getWorkflowDefinitions , getWorkflowDefinition, triggerWorkflow } = require ( '../examples/triggerWorkflow' ) ;
1313const { createWorkflow, publishWorkflow } = require ( '../workflowUtils.js' ) ;
1414
1515const eg001TriggerWorkflow = exports ;
@@ -18,6 +18,7 @@ const eg = `mseg00${exampleNumber}`; // This example reference.
1818const api = API_TYPES . MAESTRO ;
1919const mustAuthenticate = '/ds/mustAuthenticate' ;
2020const minimumBufferMin = 3 ;
21+ const workflowName = 'Example workflow - send invite to signer' ;
2122
2223
2324/**
@@ -47,7 +48,8 @@ eg001TriggerWorkflow.createController = async (req, res) => {
4748 ccName : validator . escape ( body . ccName ) ,
4849 workflowId : req . session . workflowId ,
4950 accessToken : req . user . accessToken ,
50- basePath : dsConfig . maestroApiIrl ,
51+ basePath : dsConfig . maestroApiUrl ,
52+ triggerWorkflowBasePath : dsConfig . maestroTriggerWorkflowApiUrl ,
5153 accountId : req . session . accountId ,
5254 } ;
5355 let results = null ;
@@ -56,13 +58,16 @@ eg001TriggerWorkflow.createController = async (req, res) => {
5658 const workflow = await getWorkflowDefinition ( args ) ;
5759 results = await triggerWorkflow ( workflow , args ) ;
5860 } catch ( error ) {
59- const errorBody = error && error . response && error . response . body ;
60- // we can pull the DocuSign error code and message from the response body
61- const errorCode = errorBody && errorBody . errorCode ;
62- const errorMessage = errorBody && errorBody . message ;
63- // In production, may want to provide customized error messages and
64- // remediation advice to the user.
65- res . render ( 'pages/error' , { err : error , errorCode, errorMessage } ) ;
61+ const errorCode = error ?. response ?. statusCode ;
62+ const errorMessage = error ?. response ?. body ?. message ;
63+ let errorInfo ;
64+
65+ // use custom error message if Maestro is not enabled for the account
66+ if ( errorCode === 403 ) {
67+ errorInfo = formatString ( res . locals . manifest . SupportingTexts . ContactSupportToEnableFeature , 'Maestro' ) ;
68+ }
69+
70+ return res . render ( 'pages/error' , { err : error , errorCode, errorMessage, errorInfo } ) ;
6671 }
6772 if ( results ) {
6873 req . session . instanceId = results . instanceId ; // Save for use by other examples
@@ -90,33 +95,128 @@ eg001TriggerWorkflow.getController = async (req, res) => {
9095 return res . redirect ( mustAuthenticate ) ;
9196 }
9297
98+ const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
99+ const additionalPageData = example . AdditionalPage . filter ( p => p . Name === 'publish_workflow' ) [ 0 ] ;
100+ const sourceFile =
101+ path . basename ( __filename ) [ 5 ] . toLowerCase ( ) +
102+ path . basename ( __filename ) . substr ( 6 ) ;
103+
93104 const args = {
94105 templateId : req . session . templateId ,
95106 accessToken : req . user . accessToken ,
96- basePath : dsConfig . maestroApiIrl ,
107+ basePath : dsConfig . maestroApiUrl ,
97108 accountId : req . session . accountId ,
98109 } ;
99110
100- // if there is no workflow, then create one
101- if ( ! req . session . workflowId ) {
102- if ( ! req . session . templateId ) {
103- return res . redirect ( '/eg008' ) ;
111+ try {
112+ const workflows = await getWorkflowDefinitions ( args ) ;
113+
114+ if ( workflows . count > 0 ) {
115+ const workflow = workflows . value
116+ . filter ( workflow => workflow . name === workflowName )
117+ . sort ( ( wf1 , wf2 ) => wf2 . lastUpdatedDate - wf1 . lastUpdatedDate ) [ 0 ] ;
118+ if ( workflow ) {
119+ req . session . workflowId = workflow . id ;
120+ }
104121 }
105122
106- req . session . workflowId = await createWorkflow ( args ) ;
123+ // if there is no workflow, then create one
124+ if ( ! req . session . workflowId ) {
125+ if ( ! req . session . templateId ) {
126+ return res . render ( 'pages/maestro-examples/eg001TriggerWorkflow' , {
127+ eg : eg ,
128+ csrfToken : req . csrfToken ( ) ,
129+ example : example ,
130+ templateOk : false ,
131+ sourceFile : sourceFile ,
132+ sourceUrl : dsConfig . githubExampleUrl + 'maestro/examples/' + sourceFile ,
133+ documentation : dsConfig . documentation + eg ,
134+ showDoc : dsConfig . documentation ,
135+ } ) ;
136+ }
137+
138+ req . session . workflowId = await createWorkflow ( args ) ;
139+
140+ const consentUrl = await publishWorkflow ( args , req . session . workflowId ) ;
141+ if ( consentUrl ) {
142+ return res . render ( 'pages/maestro-examples/eg001PublishWorkflow' , {
143+ example,
144+ consentUrl,
145+ message : additionalPageData . ResultsPageText ,
146+ csrfToken : req . csrfToken ( ) ,
147+ } ) ;
148+ }
149+ }
150+ } catch ( error ) {
151+ const errorCode = error ?. response ?. statusCode ;
152+ const errorMessage = error ?. response ?. body ?. message ;
153+ let errorInfo ;
154+
155+ // use custom error message if Maestro is not enabled for the account
156+ if ( errorCode === 403 ) {
157+ errorInfo = formatString ( res . locals . manifest . SupportingTexts . ContactSupportToEnableFeature , 'Maestro' ) ;
158+ }
159+
160+ return res . render ( 'pages/error' , { err : error , errorCode, errorMessage, errorInfo } ) ;
107161 }
108162
109- if ( ! req . session . workflowPublished ) {
163+ res . render ( 'pages/maestro-examples/eg001TriggerWorkflow' , {
164+ eg : eg ,
165+ csrfToken : req . csrfToken ( ) ,
166+ example : example ,
167+ sourceFile : sourceFile ,
168+ templateOk : true ,
169+ sourceUrl : dsConfig . githubExampleUrl + 'maestro/examples/' + sourceFile ,
170+ documentation : dsConfig . documentation + eg ,
171+ showDoc : dsConfig . documentation ,
172+ } ) ;
173+ } ;
174+
175+ /**
176+ * Publish workflow page
177+ */
178+ eg001TriggerWorkflow . publishController = async ( req , res ) => {
179+ // Check that the authentication token is ok with a long buffer time.
180+ // If needed, now is the best time to ask the user to authenticate
181+ // since they have not yet entered any information into the form.
182+ const isTokenOK = req . dsAuth . checkToken ( ) ;
183+ if ( ! isTokenOK ) {
184+ // Save the current operation so it will be resumed after authentication
185+ req . dsAuth . setEg ( req , eg ) ;
186+ return res . redirect ( mustAuthenticate ) ;
187+ }
188+
189+ const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
190+ const additionalPageData = example . AdditionalPage . filter ( p => p . Name === 'publish_workflow' ) [ 0 ] ;
191+
192+ try {
193+ const args = {
194+ accessToken : req . user . accessToken ,
195+ basePath : dsConfig . maestroApiUrl ,
196+ accountId : req . session . accountId ,
197+ } ;
110198 const consentUrl = await publishWorkflow ( args , req . session . workflowId ) ;
111199 if ( consentUrl ) {
112- const redirectUrl = `${ consentUrl } &host=${ dsConfig . appUrl } /${ eg } ` ;
113- return res . redirect ( redirectUrl ) ;
200+ return res . render ( 'pages/maestro-examples/eg001PublishWorkflow' , {
201+ example,
202+ consentUrl,
203+ message : additionalPageData . ResultsPageText ,
204+ csrfToken : req . csrfToken ( ) ,
205+ } ) ;
206+ }
207+ } catch ( error ) {
208+ const errorCode = error ?. response ?. statusCode ;
209+ const errorMessage = error ?. response ?. body ?. message ;
210+ let errorInfo ;
211+
212+ // use custom error message if Maestro is not enabled for the account
213+ if ( errorCode === 403 ) {
214+ errorInfo = formatString ( res . locals . manifest . SupportingTexts . ContactSupportToEnableFeature , 'Maestro' ) ;
114215 }
115216
116- req . session . workflowPublished = true ;
217+ return res . render ( 'pages/error' , { err : error , errorCode , errorMessage , errorInfo } ) ;
117218 }
118219
119- const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
120220 const sourceFile =
121221 path . basename ( __filename ) [ 5 ] . toLowerCase ( ) +
122222 path . basename ( __filename ) . substr ( 6 ) ;
@@ -125,10 +225,9 @@ eg001TriggerWorkflow.getController = async (req, res) => {
125225 csrfToken : req . csrfToken ( ) ,
126226 example : example ,
127227 sourceFile : sourceFile ,
228+ templateOk : true ,
128229 sourceUrl : dsConfig . githubExampleUrl + 'maestro/examples/' + sourceFile ,
129230 documentation : dsConfig . documentation + eg ,
130231 showDoc : dsConfig . documentation ,
131232 } ) ;
132233} ;
133-
134-
0 commit comments