@@ -95,7 +95,7 @@ medusaIntegrationTestRunner({
9595 } )
9696 } )
9797
98- describe ( "POST /admin/workflow-execution /[workflow_id]/steps/failure" , function ( ) {
98+ describe ( "POST /admin/workflows-executions /[workflow_id]/steps/failure" , function ( ) {
9999 it ( "should set step as failed" , async ( ) => {
100100 const stepId = 'test-step'
101101 const step = createStep ( {
@@ -143,6 +143,54 @@ medusaIntegrationTestRunner({
143143 } )
144144 } )
145145
146+ describe ( "POST /admin/workflows-executions/[workflow_id]/steps/success" , function ( ) {
147+ it ( "should set step as successful" , async ( ) => {
148+ const stepId = 'test-step'
149+ const step = createStep ( {
150+ name : stepId ,
151+ async : true ,
152+ } , ( ) => { } )
153+
154+ const workflowId = 'test-workflow'
155+ createWorkflow ( {
156+ name : workflowId ,
157+ retentionTime : 60 ,
158+ } , ( ) => {
159+ step ( )
160+ return new WorkflowResponse ( void 0 )
161+ } )
162+
163+ const transactionId = "test-transaction"
164+ const engine = container . resolve ( Modules . WORKFLOW_ENGINE ) as IWorkflowEngineService
165+ await engine . run ( workflowId , {
166+ transactionId
167+ } )
168+ let workflowDetail = ( await api . get ( `/admin/workflows-executions/${ workflowId } /${ transactionId } ` , adminHeaders ) ) . data . workflow_execution
169+
170+ expect ( workflowDetail . state ) . toBe ( TransactionState . INVOKING )
171+
172+ const setSuccessResponse = await api . post ( `/admin/workflows-executions/${ workflowId } /steps/success` , {
173+ transaction_id : transactionId ,
174+ step_id : stepId
175+ } , adminHeaders )
176+
177+ expect ( setSuccessResponse . status ) . toBe ( 200 )
178+ expect ( setSuccessResponse . data ) . toEqual (
179+ expect . objectContaining ( {
180+ success : true ,
181+ } )
182+ )
183+
184+ workflowDetail = ( await api . get ( `/admin/workflows-executions/${ workflowId } /${ transactionId } ` , adminHeaders ) ) . data . workflow_execution
185+
186+ expect ( workflowDetail ) . toEqual (
187+ expect . objectContaining ( {
188+ state : TransactionState . DONE ,
189+ } )
190+ )
191+ } )
192+ } )
193+
146194 describe ( "Workflow Orchestrator module subscribe" , function ( ) {
147195 it ( "should subscribe to a workflow and receive the response when it finishes" , async ( ) => {
148196 const step1 = createStep ( { name : "step1" } , async ( ) => {
@@ -275,26 +323,84 @@ medusaIntegrationTestRunner({
275323
276324 const onWorkflowFinishSpy = jest . fn ( )
277325
278- const onWorkflowFinishPromise = new Promise < void > ( ( resolve ) => {
279- void workflowOrcModule . subscribe ( {
280- workflowId : workflowId ,
281- transactionId,
282- subscriber : ( event ) => {
283- console . log ( "event" , event )
284- if ( event . eventType === "onFinish" ) {
285- onWorkflowFinishSpy ( )
286- workflowOrcModule . run ( workflow2Id , {
287- transactionId : transactionId2 ,
288- } )
289- resolve ( )
326+ const onWorkflowFinishPromise = new Promise < void > ( async ( resolve ) => {
327+ const subscriptionStream = await api . get (
328+ `/admin/workflows-executions/${ workflowId } /${ transactionId } /subscribe` ,
329+ {
330+ ...adminHeaders ,
331+ Accept : "text/event-stream" ,
332+ }
333+ )
334+
335+ const reader = subscriptionStream . body ?. getReader ( )
336+ const decoder = new TextDecoder ( "utf-8" )
337+
338+ let buffer = ""
339+
340+ while ( true ) {
341+ const { done, value } = await reader ! . read ( )
342+ if ( done ) break
343+
344+ buffer += decoder . decode ( value , { stream : true } )
345+
346+ let lines = buffer . split ( "\n" )
347+ buffer = lines . pop ( ) || ""
348+
349+ for ( const line of lines ) {
350+ if ( line . startsWith ( "data: " ) ) {
351+ const data = line . replace ( "data: " , "" ) . trim ( )
352+ const event = JSON . parse ( data )
353+ console . log ( "event" , event )
354+ if ( event . event_type === "onFinish" ) {
355+ onWorkflowFinishSpy ( )
356+ workflowOrcModule . run ( workflow2Id , {
357+ transactionId : transactionId2 ,
358+ } )
359+ resolve ( )
360+ }
290361 }
291- } ,
292- } )
362+ }
363+ }
293364 } )
294365
295366 const onWorkflow2FinishSpy = jest . fn ( )
296367
297- const workflow2FinishPromise = new Promise < void > ( ( resolve ) => {
368+ const workflow2FinishPromise = new Promise < void > ( async ( resolve ) => {
369+ const subscriptionStream = await api . get (
370+ `/admin/workflows-executions/${ workflow2Id } /subscribe` ,
371+ {
372+ ...adminHeaders ,
373+ Accept : "text/event-stream" ,
374+ }
375+ )
376+
377+ const reader = subscriptionStream . body ?. getReader ( )
378+ const decoder = new TextDecoder ( "utf-8" )
379+
380+ let buffer = ""
381+
382+ while ( true ) {
383+ const { done, value } = await reader ! . read ( )
384+ if ( done ) break
385+
386+ buffer += decoder . decode ( value , { stream : true } )
387+
388+ let lines = buffer . split ( "\n" )
389+ buffer = lines . pop ( ) || ""
390+
391+ for ( const line of lines ) {
392+ if ( line . startsWith ( "data: " ) ) {
393+ const data = line . replace ( "data: " , "" ) . trim ( )
394+ const event = JSON . parse ( data )
395+ console . log ( "event" , event )
396+ if ( event . event_type === "onFinish" ) {
397+ onWorkflowFinishSpy ( )
398+ resolve ( )
399+ }
400+ }
401+ }
402+ }
403+
298404 void workflowOrcModule . subscribe ( {
299405 workflowId : workflow2Id ,
300406 subscriber : ( event ) => {
0 commit comments