@@ -10,6 +10,7 @@ const { formatString, API_TYPES } = require('../../utils.js');
1010const { getExampleByNumber } = require ( '../../manifestService' ) ;
1111const dsConfig = require ( '../../../config/index.js' ) . config ;
1212const { getMaestroWorkflows, triggerWorkflow } = require ( '../examples/triggerWorkflow' ) ;
13+ const { createWorkflow, publishWorkflow } = require ( '../workflowUtils.js' ) ;
1314
1415const eg001TriggerWorkflow = exports ;
1516const exampleNumber = 1 ;
@@ -51,39 +52,103 @@ eg001TriggerWorkflow.createController = async (req, res) => {
5152 } ;
5253
5354 const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
55+ try {
56+ const { instanceUrl, instanceId } = await triggerWorkflow ( args , req . session . workflowId ) ;
57+ req . session . instanceId = instanceId ;
58+
59+ return res . render ( 'pages/maestro-examples/eg001EmbedWorkflow' , {
60+ title : example . ExampleName ,
61+ message : formatString ( example . ResultsPageText , JSON . stringify ( instanceId ) ) ,
62+ instanceUrl,
63+ } ) ;
64+ } catch ( error ) {
65+ const errorCode = error ?. response ?. statusCode ;
66+ const errorMessage = error ?. response ?. body ?. message ;
67+ return res . render ( 'pages/error' , { err : error , errorCode, errorMessage } ) ;
68+ }
69+ } ;
70+
71+ /**
72+ * Form page for this application
73+ */
74+ eg001TriggerWorkflow . getController = async ( req , res ) => {
75+ // Check that the authentication token is ok with a long buffer time.
76+ // If needed, now is the best time to ask the user to authenticate
77+ // since they have not yet entered any information into the form.
78+ const isTokenOK = req . dsAuth . checkToken ( ) ;
79+ if ( ! isTokenOK ) {
80+ // Save the current operation so it will be resumed after authentication
81+ req . dsAuth . setEg ( req , eg ) ;
82+ return res . redirect ( mustAuthenticate ) ;
83+ }
84+
85+ const args = {
86+ accessToken : req . user . accessToken ,
87+ basePath : dsConfig . maestroApiUrl ,
88+ accountId : req . session . accountId ,
89+ } ;
90+
91+ const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
92+ const sourceFile =
93+ path . basename ( __filename ) [ 5 ] . toLowerCase ( ) +
94+ path . basename ( __filename ) . substr ( 6 ) ;
95+
5496 try {
5597 const workflows = await getMaestroWorkflows ( args ) ;
98+ if ( ! workflows . data || workflows . data . length === 0 || ! workflows . data . find ( wf => wf . name === workflowName ) ) {
99+ if ( ! req . session . templateId ) {
100+ return res . render ( 'pages/maestro-examples/eg001TriggerWorkflow' , {
101+ eg : eg ,
102+ csrfToken : req . csrfToken ( ) ,
103+ example : example ,
104+ templateOk : false ,
105+ sourceFile : sourceFile ,
106+ sourceUrl : dsConfig . githubExampleUrl + 'maestro/examples/' + sourceFile ,
107+ documentation : dsConfig . documentation + eg ,
108+ showDoc : dsConfig . documentation ,
109+ } ) ;
110+ }
111+
112+ const newWorkflow = await createWorkflow ( args , req . session . templateId ) ;
113+ const consentUrl = await publishWorkflow ( args , newWorkflow . workflowDefinitionId ) ;
56114
57- if ( workflows . data && workflows . data . length > 0 ) {
58- const workflow = workflows . data . find ( wf => wf . status === 'active' && wf . name === workflowName ) ;
59- if ( workflow ) {
60- const { instanceUrl, instanceId } = await triggerWorkflow ( args , workflow . id ) ;
61- req . session . workflowId = workflow . id ;
62- req . session . instanceId = instanceId ;
63-
64- return res . render ( 'pages/maestro-examples/eg001EmbedWorkflow' , {
65- title : example . ExampleName ,
66- message : formatString ( example . ResultsPageText , JSON . stringify ( instanceId ) ) ,
67- instanceUrl,
68- } ) ;
69- }
115+ req . session . workflowId = newWorkflow . workflowDefinitionId ;
116+
117+ if ( consentUrl ) {
118+ return res . render ( 'pages/maestro-examples/eg001PublishWorkflow' , {
119+ eg : eg ,
120+ csrfToken : req . csrfToken ( ) ,
121+ example : example ,
122+ message : example . AdditionalPage [ 0 ] . ResultsPageText ,
123+ consentUrl
124+ } ) ;
125+ }
70126 }
71127
72- const errorCode = '404' ;
73- const errorMessage = 'No active workflow found' ;
74- const errorInfo = example . CustomErrorTexts [ 0 ] . ErrorMessage ;
75- return res . render ( 'pages/error' , { errorCode, errorMessage, errorInfo } ) ;
128+ const workflow = workflows . data . find ( wf => wf . name === workflowName ) ;
129+ req . session . workflowId = workflow . id ;
76130 } catch ( error ) {
77131 const errorCode = error ?. response ?. statusCode ;
78132 const errorMessage = error ?. response ?. body ?. message ;
79133 return res . render ( 'pages/error' , { err : error , errorCode, errorMessage } ) ;
80134 }
135+
136+ res . render ( 'pages/maestro-examples/eg001TriggerWorkflow' , {
137+ eg : eg ,
138+ csrfToken : req . csrfToken ( ) ,
139+ example : example ,
140+ sourceFile : sourceFile ,
141+ templateOk : true ,
142+ sourceUrl : dsConfig . githubExampleUrl + 'maestro/examples/' + sourceFile ,
143+ documentation : dsConfig . documentation + eg ,
144+ showDoc : dsConfig . documentation ,
145+ } ) ;
81146} ;
82147
83148/**
84- * Form page for this application
149+ * Publish workflow
85150 */
86- eg001TriggerWorkflow . getController = async ( req , res ) => {
151+ eg001TriggerWorkflow . publishController = async ( req , res ) => {
87152 // Check that the authentication token is ok with a long buffer time.
88153 // If needed, now is the best time to ask the user to authenticate
89154 // since they have not yet entered any information into the form.
@@ -94,6 +159,29 @@ eg001TriggerWorkflow.getController = async (req, res) => {
94159 return res . redirect ( mustAuthenticate ) ;
95160 }
96161
162+ const args = {
163+ accessToken : req . user . accessToken ,
164+ basePath : dsConfig . maestroApiUrl ,
165+ accountId : req . session . accountId ,
166+ } ;
167+
168+ try {
169+ const consentUrl = await publishWorkflow ( args , req . session . workflowId ) ;
170+
171+ if ( consentUrl ) {
172+ return res . render ( 'pages/maestro-examples/eg001PublishWorkflow' , {
173+ eg : eg ,
174+ csrfToken : req . csrfToken ( ) ,
175+ example : example ,
176+ message : example . AdditionalPage [ 0 ] . ResultsPageText ,
177+ consentUrl
178+ } ) ;
179+ }
180+ } catch ( error ) {
181+ const errorCode = error ?. response ?. statusCode ;
182+ const errorMessage = error ?. response ?. body ?. message ;
183+ return res . render ( 'pages/error' , { err : error , errorCode, errorMessage } ) ;
184+ }
97185 const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
98186 const sourceFile =
99187 path . basename ( __filename ) [ 5 ] . toLowerCase ( ) +
@@ -104,6 +192,7 @@ eg001TriggerWorkflow.getController = async (req, res) => {
104192 csrfToken : req . csrfToken ( ) ,
105193 example : example ,
106194 sourceFile : sourceFile ,
195+ templateOk : true ,
107196 sourceUrl : dsConfig . githubExampleUrl + 'maestro/examples/' + sourceFile ,
108197 documentation : dsConfig . documentation + eg ,
109198 showDoc : dsConfig . documentation ,
0 commit comments