2023-03-15 08:35:16 +01:00
|
|
|
import { SessionState, SetVariableBlock, Variable } from '@typebot.io/schemas'
|
|
|
|
|
import { byId } from '@typebot.io/lib'
|
2023-03-15 12:21:52 +01:00
|
|
|
import { ExecuteLogicResponse } from '@/features/chat/types'
|
|
|
|
|
import { updateVariables } from '@/features/variables/updateVariables'
|
|
|
|
|
import { parseVariables } from '@/features/variables/parseVariables'
|
|
|
|
|
import { parseGuessedValueType } from '@/features/variables/parseGuessedValueType'
|
2023-04-14 12:11:42 +02:00
|
|
|
import { parseScriptToExecuteClientSideAction } from '../script/executeScript'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2023-07-18 14:31:20 +02:00
|
|
|
export const executeSetVariable = (
|
2022-11-29 10:02:40 +01:00
|
|
|
state: SessionState,
|
2023-05-26 09:20:22 +02:00
|
|
|
block: SetVariableBlock
|
2023-07-18 14:31:20 +02:00
|
|
|
): ExecuteLogicResponse => {
|
2022-11-29 10:02:40 +01:00
|
|
|
const { variables } = state.typebot
|
|
|
|
|
if (!block.options?.variableId)
|
|
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
|
|
|
|
}
|
2023-06-15 14:45:42 +02:00
|
|
|
const expressionToEvaluate = getExpressionToEvaluate(state.result.id)(
|
|
|
|
|
block.options
|
|
|
|
|
)
|
|
|
|
|
const isCustomValue = !block.options.type || block.options.type === 'Custom'
|
|
|
|
|
if (
|
|
|
|
|
expressionToEvaluate &&
|
|
|
|
|
((isCustomValue && block.options.isExecutedOnClient) ||
|
|
|
|
|
block.options.type === 'Moment of the day')
|
|
|
|
|
) {
|
2023-04-14 12:11:42 +02:00
|
|
|
const scriptToExecute = parseScriptToExecuteClientSideAction(
|
|
|
|
|
state.typebot.variables,
|
2023-06-15 14:45:42 +02:00
|
|
|
expressionToEvaluate
|
2023-04-14 12:11:42 +02:00
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
|
|
|
|
clientSideActions: [
|
|
|
|
|
{
|
|
|
|
|
setVariable: {
|
|
|
|
|
scriptToExecute,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-11 17:17:24 -04:00
|
|
|
const evaluatedExpression = expressionToEvaluate
|
|
|
|
|
? evaluateSetVariableExpression(variables)(expressionToEvaluate)
|
2022-11-29 10:02:40 +01:00
|
|
|
: undefined
|
|
|
|
|
const existingVariable = variables.find(byId(block.options.variableId))
|
|
|
|
|
if (!existingVariable) return { outgoingEdgeId: block.outgoingEdgeId }
|
|
|
|
|
const newVariable = {
|
|
|
|
|
...existingVariable,
|
|
|
|
|
value: evaluatedExpression,
|
|
|
|
|
}
|
2023-07-18 14:31:20 +02:00
|
|
|
const newSessionState = updateVariables(state)([newVariable])
|
2022-11-29 10:02:40 +01:00
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
|
|
|
|
newSessionState,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const evaluateSetVariableExpression =
|
|
|
|
|
(variables: Variable[]) =>
|
|
|
|
|
(str: string): unknown => {
|
2023-02-22 12:21:11 +01:00
|
|
|
const isSingleVariable =
|
|
|
|
|
str.startsWith('{{') && str.endsWith('}}') && str.split('{{').length === 2
|
|
|
|
|
if (isSingleVariable) return parseVariables(variables)(str)
|
2022-11-29 10:02:40 +01:00
|
|
|
const evaluating = parseVariables(variables, { fieldToParse: 'id' })(
|
|
|
|
|
str.includes('return ') ? str : `return ${str}`
|
|
|
|
|
)
|
|
|
|
|
try {
|
|
|
|
|
const func = Function(...variables.map((v) => v.id), evaluating)
|
2023-03-15 12:21:52 +01:00
|
|
|
return func(...variables.map((v) => parseGuessedValueType(v.value)))
|
2022-11-29 10:02:40 +01:00
|
|
|
} catch (err) {
|
2023-02-16 10:19:11 +01:00
|
|
|
return parseVariables(variables)(str)
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-11 17:17:24 -04:00
|
|
|
|
|
|
|
|
const getExpressionToEvaluate =
|
|
|
|
|
(resultId: string | undefined) =>
|
|
|
|
|
(options: SetVariableBlock['options']): string | null => {
|
|
|
|
|
switch (options.type) {
|
|
|
|
|
case 'Today':
|
|
|
|
|
return 'new Date().toISOString()'
|
|
|
|
|
case 'Tomorrow': {
|
|
|
|
|
return 'new Date(Date.now() + 86400000).toISOString()'
|
|
|
|
|
}
|
|
|
|
|
case 'Yesterday': {
|
|
|
|
|
return 'new Date(Date.now() - 86400000).toISOString()'
|
|
|
|
|
}
|
|
|
|
|
case 'Random ID': {
|
|
|
|
|
return 'Math.random().toString(36).substring(2, 15)'
|
|
|
|
|
}
|
|
|
|
|
case 'User ID': {
|
|
|
|
|
return resultId ?? 'Math.random().toString(36).substring(2, 15)'
|
|
|
|
|
}
|
|
|
|
|
case 'Map item with same index': {
|
|
|
|
|
return `const itemIndex = ${options.mapListItemParams?.baseListVariableId}.indexOf(${options.mapListItemParams?.baseItemVariableId})
|
|
|
|
|
return ${options.mapListItemParams?.targetListVariableId}.at(itemIndex)`
|
|
|
|
|
}
|
|
|
|
|
case 'Empty': {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2023-06-15 14:45:42 +02:00
|
|
|
case 'Moment of the day': {
|
|
|
|
|
return `const now = new Date()
|
|
|
|
|
if(now.getHours() < 12) return 'morning'
|
|
|
|
|
if(now.getHours() >= 12 && now.getHours() < 18) return 'afternoon'
|
|
|
|
|
if(now.getHours() >= 18) return 'evening'
|
|
|
|
|
if(now.getHours() >= 22 || now.getHours() < 6) return 'night'`
|
|
|
|
|
}
|
2023-05-11 17:17:24 -04:00
|
|
|
case 'Custom':
|
|
|
|
|
case undefined: {
|
|
|
|
|
return options.expressionToEvaluate ?? null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|