@@ -254,11 +254,11 @@ function handleWorkflowActions(workflow: any[], credentials: Credentials) {
254254router . put ( '/recordings/:id' , requireSignIn , async ( req : AuthenticatedRequest , res ) => {
255255 try {
256256 const { id } = req . params ;
257- const { name, limit , credentials, targetUrl } = req . body ;
257+ const { name, limits , credentials, targetUrl } = req . body ;
258258
259259 // Validate input
260- if ( ! name && limit === undefined && ! targetUrl ) {
261- return res . status ( 400 ) . json ( { error : 'Either "name", "limit " or "target_url" must be provided.' } ) ;
260+ if ( ! name && ! limits && ! credentials && ! targetUrl ) {
261+ return res . status ( 400 ) . json ( { error : 'Either "name", "limits", "credentials " or "target_url" must be provided.' } ) ;
262262 }
263263
264264 // Fetch the robot by ID
@@ -274,22 +274,26 @@ router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, r
274274 }
275275
276276 if ( targetUrl ) {
277- const updatedWorkflow = robot . recording . workflow . map ( ( step ) => {
278- if ( step . where ?. url && step . where . url !== "about:blank" ) {
279- step . where . url = targetUrl ;
280- }
281-
282- step . what . forEach ( ( action ) => {
277+ const updatedWorkflow = [ ... robot . recording . workflow ] ;
278+
279+ for ( let i = updatedWorkflow . length - 1 ; i >= 0 ; i -- ) {
280+ const step = updatedWorkflow [ i ] ;
281+ for ( let j = 0 ; j < step . what . length ; j ++ ) {
282+ const action = step . what [ j ] ;
283283 if ( action . action === "goto" && action . args ?. length ) {
284- action . args [ 0 ] = targetUrl ;
285- }
286- } ) ;
287-
288- return step ;
289- } ) ;
290284
291- robot . set ( 'recording' , { ...robot . recording , workflow : updatedWorkflow } ) ;
292- robot . changed ( 'recording' , true ) ;
285+ action . args [ 0 ] = targetUrl ;
286+ if ( step . where ?. url && step . where . url !== "about:blank" ) {
287+ step . where . url = targetUrl ;
288+ }
289+
290+ robot . set ( 'recording' , { ...robot . recording , workflow : updatedWorkflow } ) ;
291+ robot . changed ( 'recording' , true ) ;
292+ i = - 1 ;
293+ break ;
294+ }
295+ }
296+ }
293297 }
294298
295299 await robot . save ( ) ;
@@ -300,38 +304,20 @@ router.put('/recordings/:id', requireSignIn, async (req: AuthenticatedRequest, r
300304 workflow = handleWorkflowActions ( workflow , credentials ) ;
301305 }
302306
303- // Update the limit
304- if ( limit !== undefined ) {
305- // Ensure the workflow structure is valid before updating
306- if (
307- workflow . length > 0 &&
308- workflow [ 0 ] ?. what ?. [ 0 ]
309- ) {
310- // Create a new workflow object with the updated limit
311- workflow = workflow . map ( ( step , index ) => {
312- if ( index === 0 ) { // Assuming you want to update the first step
313- return {
314- ...step ,
315- what : step . what . map ( ( action , actionIndex ) => {
316- if ( actionIndex === 0 ) { // Assuming the first action needs updating
317- return {
318- ...action ,
319- args : ( action . args ?? [ ] ) . map ( ( arg , argIndex ) => {
320- if ( argIndex === 0 ) { // Assuming the first argument needs updating
321- return { ...arg , limit } ;
322- }
323- return arg ;
324- } ) ,
325- } ;
326- }
327- return action ;
328- } ) ,
329- } ;
330- }
331- return step ;
332- } ) ;
333- } else {
334- return res . status ( 400 ) . json ( { error : 'Invalid workflow structure for updating limit.' } ) ;
307+ if ( limits && Array . isArray ( limits ) && limits . length > 0 ) {
308+ for ( const limitInfo of limits ) {
309+ const { pairIndex, actionIndex, argIndex, limit } = limitInfo ;
310+
311+ const pair = workflow [ pairIndex ] ;
312+ if ( ! pair || ! pair . what ) continue ;
313+
314+ const action = pair . what [ actionIndex ] ;
315+ if ( ! action || ! action . args ) continue ;
316+
317+ const arg = action . args [ argIndex ] ;
318+ if ( ! arg || typeof arg !== 'object' ) continue ;
319+
320+ ( arg as { limit : number } ) . limit = limit ;
335321 }
336322 }
337323
0 commit comments