2023-09-20 15:26:52 +02:00
|
|
|
import { ExecuteLogicResponse } from '../../../types'
|
2023-04-14 12:11:42 +02:00
|
|
|
import { ScriptBlock, SessionState, Variable } from '@typebot.io/schemas'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { extractVariablesFromText } from '../../../variables/extractVariablesFromText'
|
|
|
|
|
import { parseGuessedValueType } from '../../../variables/parseGuessedValueType'
|
|
|
|
|
import { parseVariables } from '../../../variables/parseVariables'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2023-01-27 15:58:05 +01:00
|
|
|
export const executeScript = (
|
2023-08-24 07:48:30 +02:00
|
|
|
state: SessionState,
|
2023-05-26 09:20:22 +02:00
|
|
|
block: ScriptBlock
|
2022-11-29 10:02:40 +01:00
|
|
|
): ExecuteLogicResponse => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const { variables } = state.typebotsQueue[0].typebot
|
2023-11-08 15:34:16 +01:00
|
|
|
if (!block.options?.content || state.whatsApp)
|
2023-08-29 10:01:28 +02:00
|
|
|
return { outgoingEdgeId: block.outgoingEdgeId }
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2023-04-14 12:11:42 +02:00
|
|
|
const scriptToExecute = parseScriptToExecuteClientSideAction(
|
|
|
|
|
variables,
|
2022-11-29 10:02:40 +01:00
|
|
|
block.options.content
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
2023-01-26 15:26:42 +01:00
|
|
|
clientSideActions: [
|
|
|
|
|
{
|
2023-04-14 12:11:42 +02:00
|
|
|
scriptToExecute: scriptToExecute,
|
2022-11-29 10:02:40 +01:00
|
|
|
},
|
2023-01-26 15:26:42 +01:00
|
|
|
],
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 12:11:42 +02:00
|
|
|
|
|
|
|
|
export const parseScriptToExecuteClientSideAction = (
|
|
|
|
|
variables: Variable[],
|
|
|
|
|
contentToEvaluate: string
|
|
|
|
|
) => {
|
|
|
|
|
const content = parseVariables(variables, { fieldToParse: 'id' })(
|
|
|
|
|
contentToEvaluate
|
|
|
|
|
)
|
|
|
|
|
const args = extractVariablesFromText(variables)(contentToEvaluate).map(
|
|
|
|
|
(variable) => ({
|
|
|
|
|
id: variable.id,
|
|
|
|
|
value: parseGuessedValueType(variable.value),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
content,
|
|
|
|
|
args,
|
|
|
|
|
}
|
|
|
|
|
}
|