@@ -113,7 +113,26 @@ const hasInvalidElements = (elements: any[]): boolean => {
113113 } ) ;
114114} ;
115115
116- const buildConditionString = ( group : any ) : string => {
116+ const getAssignedVariableNames = ( nodes : Node [ ] ) : Set < string > => {
117+ const names = new Set ( [ 'chatId' , 'authorId' , 'input' , 'buttons' , 'res' ] ) ;
118+ for ( const node of nodes ) {
119+ const data = node . data as NodeDataProps | undefined ;
120+ if ( data ?. stepType === StepType . Assign && Array . isArray ( data . assignElements ) ) {
121+ for ( const e of data . assignElements ) {
122+ const key = e . key ?. replaceAll ( '${' , '' ) . replaceAll ( '}' , '' ) . trim ( ) ;
123+ if ( key ) names . add ( key ) ;
124+ }
125+ }
126+ }
127+ return names ;
128+ } ;
129+
130+ const buildConditionString = ( group : any , assignedVariableNames : Set < string > ) : string => {
131+ const formatField = ( rawField : string ) : string => {
132+ if ( assignedVariableNames . has ( rawField ) ) return rawField ;
133+ return isNumericString ( rawField ) ? rawField : `"${ rawField } "` ;
134+ } ;
135+
117136 if ( 'children' in group ) {
118137 const subgroup = group as Group ;
119138 if ( subgroup . children . length === 0 ) {
@@ -122,12 +141,14 @@ const buildConditionString = (group: any): string => {
122141
123142 const conditions = subgroup . children . map ( ( child ) => {
124143 if ( 'children' in child ) {
125- return `(${ buildConditionString ( child ) } )` ;
144+ return `(${ buildConditionString ( child , assignedVariableNames ) } )` ;
126145 } else {
127146 const rule = child ;
147+ const rawField = rule . field . replaceAll ( '${' , '' ) . replaceAll ( '}' , '' ) ;
128148 const absoluteValue = removeWrapperQuotes ( rule . value . replaceAll ( '${' , '' ) . replaceAll ( '}' , '' ) ) ;
129149 const value = isNumericString ( absoluteValue ) ? absoluteValue : `"${ absoluteValue } "` ;
130- return `${ rule . field . replaceAll ( '${' , '' ) . replaceAll ( '}' , '' ) } ${ rule . operator } ${ value } ` ;
150+ const field = formatField ( rawField ) ;
151+ return `${ field } ${ rule . operator } ${ value } ` ;
131152 }
132153 } ) ;
133154
@@ -138,9 +159,11 @@ const buildConditionString = (group: any): string => {
138159 }
139160 } else {
140161 const rule = group as Rule ;
162+ const rawField = rule . field . replaceAll ( '${' , '' ) . replaceAll ( '}' , '' ) ;
141163 const absoluteValue = removeWrapperQuotes ( rule . value . replaceAll ( '${' , '' ) . replaceAll ( '}' , '' ) ) ;
142164 const value = isNumericString ( absoluteValue ) ? absoluteValue : `"${ absoluteValue } "` ;
143- return `${ rule . field . replaceAll ( '${' , '' ) . replaceAll ( '}' , '' ) } ${ rule . operator } ${ value } ` ;
165+ const field = formatField ( rawField ) ;
166+ return `${ field } ${ rule . operator } ${ value } ` ;
144167 }
145168} ;
146169
@@ -615,10 +638,11 @@ function handleConditionStep(
615638 throw new Error ( i18next . t ( 'toast.missing-condition-rules' ) ?? 'Error' ) ;
616639 }
617640
641+ const assignedVariableNames = getAssignedVariableNames ( nodes ) ;
618642 finishedFlow . set ( parentStepName , {
619643 switch : [
620644 {
621- condition : `\${${ buildConditionString ( parentNode . data . rules ) } }` ,
645+ condition : `\${${ buildConditionString ( parentNode . data . rules , assignedVariableNames ) } }` ,
622646 next : toSnakeCase ( firstChild ?. data ?. label ?? '' ) ?? '' ,
623647 } ,
624648 ] ,
0 commit comments