@@ -10,6 +10,7 @@ const { formatString, API_TYPES } = require('../../utils.js');
10
10
const { getExampleByNumber } = require ( '../../manifestService' ) ;
11
11
const dsConfig = require ( '../../../config/index.js' ) . config ;
12
12
const { getMaestroWorkflows, triggerWorkflow } = require ( '../examples/triggerWorkflow' ) ;
13
+ const { createWorkflow, publishWorkflow } = require ( '../workflowUtils.js' ) ;
13
14
14
15
const eg001TriggerWorkflow = exports ;
15
16
const exampleNumber = 1 ;
@@ -51,39 +52,103 @@ eg001TriggerWorkflow.createController = async (req, res) => {
51
52
} ;
52
53
53
54
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
+
54
96
try {
55
97
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 ) ;
56
114
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
+ }
70
126
}
71
127
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 ;
76
130
} catch ( error ) {
77
131
const errorCode = error ?. response ?. statusCode ;
78
132
const errorMessage = error ?. response ?. body ?. message ;
79
133
return res . render ( 'pages/error' , { err : error , errorCode, errorMessage } ) ;
80
134
}
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
+ } ) ;
81
146
} ;
82
147
83
148
/**
84
- * Form page for this application
149
+ * Publish workflow
85
150
*/
86
- eg001TriggerWorkflow . getController = async ( req , res ) => {
151
+ eg001TriggerWorkflow . publishController = async ( req , res ) => {
87
152
// Check that the authentication token is ok with a long buffer time.
88
153
// If needed, now is the best time to ask the user to authenticate
89
154
// since they have not yet entered any information into the form.
@@ -94,6 +159,29 @@ eg001TriggerWorkflow.getController = async (req, res) => {
94
159
return res . redirect ( mustAuthenticate ) ;
95
160
}
96
161
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
+ }
97
185
const example = getExampleByNumber ( res . locals . manifest , exampleNumber , api ) ;
98
186
const sourceFile =
99
187
path . basename ( __filename ) [ 5 ] . toLowerCase ( ) +
@@ -104,6 +192,7 @@ eg001TriggerWorkflow.getController = async (req, res) => {
104
192
csrfToken : req . csrfToken ( ) ,
105
193
example : example ,
106
194
sourceFile : sourceFile ,
195
+ templateOk : true ,
107
196
sourceUrl : dsConfig . githubExampleUrl + 'maestro/examples/' + sourceFile ,
108
197
documentation : dsConfig . documentation + eg ,
109
198
showDoc : dsConfig . documentation ,
0 commit comments