@ -1,5 +1,5 @@
|
|||||||
import { ExecuteLogicResponse } from '@/features/chat'
|
import { ExecuteLogicResponse } from '@/features/chat'
|
||||||
import { parseVariables } from '@/features/variables'
|
import { findUniqueVariableValue, parseVariables } from '@/features/variables'
|
||||||
import {
|
import {
|
||||||
Comparison,
|
Comparison,
|
||||||
ComparisonOperators,
|
ComparisonOperators,
|
||||||
@ -34,32 +34,53 @@ const executeComparison =
|
|||||||
if (!comparison?.variableId) return false
|
if (!comparison?.variableId) return false
|
||||||
const inputValue =
|
const inputValue =
|
||||||
variables.find((v) => v.id === comparison.variableId)?.value ?? ''
|
variables.find((v) => v.id === comparison.variableId)?.value ?? ''
|
||||||
const value = parseVariables(variables)(comparison.value)
|
const value =
|
||||||
|
findUniqueVariableValue(variables)(comparison.value) ??
|
||||||
|
parseVariables(variables)(comparison.value)
|
||||||
if (isNotDefined(value)) return false
|
if (isNotDefined(value)) return false
|
||||||
switch (comparison.comparisonOperator) {
|
switch (comparison.comparisonOperator) {
|
||||||
case ComparisonOperators.CONTAINS: {
|
case ComparisonOperators.CONTAINS: {
|
||||||
const normalizeString = (value: string) => value.toLowerCase().trim()
|
const contains = (a: string, b: string) => {
|
||||||
const contains = (inputValue: string) =>
|
if (b === '') return false
|
||||||
normalizeString(inputValue).includes(normalizeString(value))
|
return a.toLowerCase().trim().includes(b.toLowerCase().trim())
|
||||||
if (typeof inputValue === 'string') return contains(inputValue)
|
}
|
||||||
return inputValue.some(contains)
|
return compare(contains, inputValue, value)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.EQUAL: {
|
case ComparisonOperators.EQUAL: {
|
||||||
return inputValue === value
|
return compare((a, b) => a === b, inputValue, value)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.NOT_EQUAL: {
|
case ComparisonOperators.NOT_EQUAL: {
|
||||||
return inputValue !== value
|
return compare((a, b) => a !== b, inputValue, value)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.GREATER: {
|
case ComparisonOperators.GREATER: {
|
||||||
if (typeof inputValue !== 'string') return false
|
return compare(
|
||||||
return parseFloat(inputValue) > parseFloat(value)
|
(a, b) => parseFloat(a) > parseFloat(b),
|
||||||
|
inputValue,
|
||||||
|
value
|
||||||
|
)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.LESS: {
|
case ComparisonOperators.LESS: {
|
||||||
if (typeof inputValue !== 'string') return false
|
return compare(
|
||||||
return parseFloat(inputValue) < parseFloat(value)
|
(a, b) => parseFloat(a) < parseFloat(b),
|
||||||
|
inputValue,
|
||||||
|
value
|
||||||
|
)
|
||||||
}
|
}
|
||||||
case ComparisonOperators.IS_SET: {
|
case ComparisonOperators.IS_SET: {
|
||||||
return isDefined(inputValue) && inputValue.length > 0
|
return isDefined(inputValue) && inputValue.length > 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const compare = (
|
||||||
|
func: (a: string, b: string) => boolean,
|
||||||
|
a: string | string[],
|
||||||
|
b: string | string[]
|
||||||
|
): boolean => {
|
||||||
|
if (typeof a === 'string') {
|
||||||
|
if (typeof b === 'string') return func(a, b)
|
||||||
|
return b.some((b) => func(a, b))
|
||||||
|
}
|
||||||
|
if (typeof b === 'string') return a.some((a) => func(a, b))
|
||||||
|
return a.some((a) => b.some((b) => func(a, b)))
|
||||||
|
}
|
||||||
|
@ -218,3 +218,14 @@ const updateTypebotVariables =
|
|||||||
...serializedNewVariables,
|
...serializedNewVariables,
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const findUniqueVariableValue =
|
||||||
|
(variables: Variable[]) =>
|
||||||
|
(value: string | undefined): string | string[] | null => {
|
||||||
|
if (!value || !value.startsWith('{{') || !value.endsWith('}}')) return null
|
||||||
|
const variableName = value.slice(2, -2)
|
||||||
|
const variable = variables.find(
|
||||||
|
(variable) => variable.name === variableName
|
||||||
|
)
|
||||||
|
return variable?.value ?? null
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user