@ -0,0 +1,31 @@
|
||||
import { deepParseVariable } from '@/features/variables/utils'
|
||||
import {
|
||||
SessionState,
|
||||
VariableWithValue,
|
||||
ChoiceInputBlock,
|
||||
ItemType,
|
||||
} from 'models'
|
||||
import { isDefined } from 'utils'
|
||||
|
||||
export const injectVariableValuesInButtonsInputBlock =
|
||||
(variables: SessionState['typebot']['variables']) =>
|
||||
(block: ChoiceInputBlock): ChoiceInputBlock => {
|
||||
if (block.options.dynamicVariableId) {
|
||||
const variable = variables.find(
|
||||
(variable) =>
|
||||
variable.id === block.options.dynamicVariableId &&
|
||||
isDefined(variable.value)
|
||||
) as VariableWithValue | undefined
|
||||
if (!variable || typeof variable.value === 'string') return block
|
||||
return {
|
||||
...block,
|
||||
items: variable.value.map((item, idx) => ({
|
||||
id: idx.toString(),
|
||||
type: ItemType.BUTTON,
|
||||
blockId: block.id,
|
||||
content: item,
|
||||
})),
|
||||
}
|
||||
}
|
||||
return deepParseVariable(variables)(block)
|
||||
}
|
@ -11,11 +11,12 @@ import Stripe from 'stripe'
|
||||
import { decrypt } from 'utils/api/encryption'
|
||||
|
||||
export const computePaymentInputRuntimeOptions =
|
||||
(state: SessionState) => (options: PaymentInputOptions) =>
|
||||
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
|
||||
(options: PaymentInputOptions) =>
|
||||
createStripePaymentIntent(state)(options)
|
||||
|
||||
const createStripePaymentIntent =
|
||||
(state: SessionState) =>
|
||||
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
|
||||
async (options: PaymentInputOptions): Promise<PaymentInputRuntimeOptions> => {
|
||||
const {
|
||||
isPreview,
|
||||
|
@ -53,24 +53,21 @@ export const getRow = async (
|
||||
})
|
||||
return { outgoingEdgeId, logs: log ? [log] : undefined }
|
||||
}
|
||||
const randomIndex = Math.floor(Math.random() * filteredRows.length)
|
||||
const extractingColumns = cellsToExtract
|
||||
.map((cell) => cell.column)
|
||||
.filter(isNotEmpty)
|
||||
const selectedRow = filteredRows
|
||||
.map((row) =>
|
||||
extractingColumns.reduce<{ [key: string]: string }>(
|
||||
(obj, column) => ({ ...obj, [column]: row[column] }),
|
||||
{}
|
||||
)
|
||||
const selectedRows = filteredRows.map((row) =>
|
||||
extractingColumns.reduce<{ [key: string]: string }>(
|
||||
(obj, column) => ({ ...obj, [column]: row[column] }),
|
||||
{}
|
||||
)
|
||||
.at(randomIndex)
|
||||
if (!selectedRow) return { outgoingEdgeId }
|
||||
)
|
||||
if (!selectedRows) return { outgoingEdgeId }
|
||||
|
||||
const newVariables = options.cellsToExtract.reduce<VariableWithValue[]>(
|
||||
(newVariables, cell) => {
|
||||
const existingVariable = variables.find(byId(cell.variableId))
|
||||
const value = selectedRow[cell.column ?? ''] ?? null
|
||||
const value = selectedRows.map((row) => row[cell.column ?? ''])
|
||||
if (!existingVariable) return newVariables
|
||||
return [
|
||||
...newVariables,
|
||||
|
@ -71,7 +71,7 @@ const sendEmail = async ({
|
||||
}: SendEmailOptions & {
|
||||
typebotId: string
|
||||
resultId?: string
|
||||
fileUrls?: string
|
||||
fileUrls?: string | string[]
|
||||
}) => {
|
||||
const { name: replyToName } = parseEmailRecipient(replyTo)
|
||||
|
||||
@ -121,7 +121,11 @@ const sendEmail = async ({
|
||||
to: recipients,
|
||||
replyTo,
|
||||
subject,
|
||||
attachments: fileUrls?.split(', ').map((url) => ({ path: url })),
|
||||
attachments: fileUrls
|
||||
? (typeof fileUrls === 'string' ? fileUrls.split(', ') : fileUrls).map(
|
||||
(url) => ({ path: url })
|
||||
)
|
||||
: undefined,
|
||||
...emailBody,
|
||||
}
|
||||
try {
|
||||
|
@ -50,8 +50,8 @@ export const executeWebhookBlock = async (
|
||||
return { outgoingEdgeId: block.outgoingEdgeId, logs: [log] }
|
||||
}
|
||||
const preparedWebhook = prepareWebhookAttributes(webhook, block.options)
|
||||
const resultValues = result && (await getResultValues(result.id))
|
||||
if (!resultValues) return { outgoingEdgeId: block.outgoingEdgeId }
|
||||
const resultValues =
|
||||
(result && (await getResultValues(result.id))) ?? undefined
|
||||
const webhookResponse = await executeWebhook({ typebot })(
|
||||
preparedWebhook,
|
||||
typebot.variables,
|
||||
@ -139,8 +139,8 @@ export const executeWebhook =
|
||||
webhook: Webhook,
|
||||
variables: Variable[],
|
||||
groupId: string,
|
||||
resultValues: ResultValues,
|
||||
resultId: string
|
||||
resultValues?: ResultValues,
|
||||
resultId?: string
|
||||
): Promise<WebhookResponse> => {
|
||||
if (!webhook.url || !webhook.method)
|
||||
return {
|
||||
|
@ -83,7 +83,7 @@ const parseResultSample = (
|
||||
headerCells: ResultHeaderCell[],
|
||||
variables: Variable[]
|
||||
) =>
|
||||
headerCells.reduce<Record<string, string | boolean | undefined>>(
|
||||
headerCells.reduce<Record<string, string | string[] | undefined>>(
|
||||
(resultSample, cell) => {
|
||||
const inputBlock = inputBlocks.find((inputBlock) =>
|
||||
cell.blocks?.some((block) => block.id === inputBlock.id)
|
||||
|
@ -34,7 +34,9 @@ const executeComparison =
|
||||
if (!comparison?.variableId) return false
|
||||
const inputValue = (
|
||||
variables.find((v) => v.id === comparison.variableId)?.value ?? ''
|
||||
).trim()
|
||||
)
|
||||
.toString()
|
||||
.trim()
|
||||
const value = parseVariables(variables)(comparison.value).trim()
|
||||
if (isNotDefined(value)) return false
|
||||
switch (comparison.comparisonOperator) {
|
||||
|
@ -232,7 +232,8 @@ export const isReplyValid = (inputValue: string, block: Block): boolean => {
|
||||
case InputBlockType.URL:
|
||||
return validateUrl(inputValue)
|
||||
case InputBlockType.CHOICE:
|
||||
if (block.options.isMultipleChoice) return true
|
||||
if (block.options.isMultipleChoice || block.options.dynamicVariableId)
|
||||
return true
|
||||
return validateButtonInput(block, inputValue)
|
||||
}
|
||||
return true
|
||||
|
@ -20,6 +20,7 @@ import { executeLogic } from './executeLogic'
|
||||
import { getNextGroup } from './getNextGroup'
|
||||
import { executeIntegration } from './executeIntegration'
|
||||
import { computePaymentInputRuntimeOptions } from '@/features/blocks/inputs/payment/api'
|
||||
import { injectVariableValuesInButtonsInputBlock } from '@/features/blocks/inputs/buttons/api/utils/injectVariableValuesInButtonsInputBlock'
|
||||
|
||||
export const executeGroup =
|
||||
(state: SessionState, currentReply?: ChatReply) =>
|
||||
@ -49,13 +50,7 @@ export const executeGroup =
|
||||
if (isInputBlock(block))
|
||||
return {
|
||||
messages,
|
||||
input: deepParseVariable(newSessionState.typebot.variables)({
|
||||
...block,
|
||||
runtimeOptions: await computeRuntimeOptions(newSessionState)(block),
|
||||
prefilledValue: getPrefilledInputValue(
|
||||
newSessionState.typebot.variables
|
||||
)(block),
|
||||
}),
|
||||
input: await injectVariablesValueInBlock(newSessionState)(block),
|
||||
newSessionState: {
|
||||
...newSessionState,
|
||||
currentBlock: {
|
||||
@ -110,7 +105,7 @@ export const executeGroup =
|
||||
}
|
||||
|
||||
const computeRuntimeOptions =
|
||||
(state: SessionState) =>
|
||||
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
|
||||
(block: InputBlock): Promise<RuntimeOptions> | undefined => {
|
||||
switch (block.type) {
|
||||
case InputBlockType.PAYMENT: {
|
||||
@ -122,10 +117,13 @@ const computeRuntimeOptions =
|
||||
const getPrefilledInputValue =
|
||||
(variables: SessionState['typebot']['variables']) => (block: InputBlock) => {
|
||||
return (
|
||||
variables.find(
|
||||
(variable) =>
|
||||
variable.id === block.options.variableId && isDefined(variable.value)
|
||||
)?.value ?? undefined
|
||||
variables
|
||||
.find(
|
||||
(variable) =>
|
||||
variable.id === block.options.variableId &&
|
||||
isDefined(variable.value)
|
||||
)
|
||||
?.value?.toString() ?? undefined
|
||||
)
|
||||
}
|
||||
|
||||
@ -150,3 +148,24 @@ const parseBubbleBlock =
|
||||
return deepParseVariable(variables)(block)
|
||||
}
|
||||
}
|
||||
|
||||
const injectVariablesValueInBlock =
|
||||
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
|
||||
async (block: InputBlock): Promise<ChatReply['input']> => {
|
||||
switch (block.type) {
|
||||
case InputBlockType.CHOICE: {
|
||||
return injectVariableValuesInButtonsInputBlock(state.typebot.variables)(
|
||||
block
|
||||
)
|
||||
}
|
||||
default: {
|
||||
return deepParseVariable(state.typebot.variables)({
|
||||
...block,
|
||||
runtimeOptions: await computeRuntimeOptions(state)(block),
|
||||
prefilledValue: getPrefilledInputValue(state.typebot.variables)(
|
||||
block
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,10 @@ export const parseVariables =
|
||||
if (!variable) return ''
|
||||
if (options.fieldToParse === 'id') return variable.id
|
||||
const { value } = variable
|
||||
if (options.escapeForJson) return jsonParse(value)
|
||||
if (options.escapeForJson)
|
||||
return jsonParse(
|
||||
typeof value !== 'string' ? JSON.stringify(value) : value
|
||||
)
|
||||
const parsedValue = safeStringify(value)
|
||||
if (!parsedValue) return ''
|
||||
return parsedValue
|
||||
@ -67,9 +70,10 @@ export const safeStringify = (val: unknown): string | null => {
|
||||
|
||||
export const parseCorrectValueType = (
|
||||
value: Variable['value']
|
||||
): string | boolean | number | null | undefined => {
|
||||
): string | string[] | boolean | number | null | undefined => {
|
||||
if (value === null) return null
|
||||
if (value === undefined) return undefined
|
||||
if (typeof value !== 'string') return value
|
||||
const isNumberStartingWithZero =
|
||||
value.startsWith('0') && !value.startsWith('0.') && value.length > 1
|
||||
if (typeof value === 'string' && isNumberStartingWithZero) return value
|
||||
@ -152,7 +156,9 @@ const updateResultVariables =
|
||||
if (!result) return []
|
||||
const serializedNewVariables = newVariables.map((variable) => ({
|
||||
...variable,
|
||||
value: safeStringify(variable.value),
|
||||
value: Array.isArray(variable.value)
|
||||
? variable.value.map(safeStringify).filter(isDefined)
|
||||
: safeStringify(variable.value),
|
||||
}))
|
||||
|
||||
const updatedVariables = [
|
||||
@ -181,7 +187,9 @@ const updateTypebotVariables =
|
||||
(newVariables: VariableWithUnknowValue[]): Variable[] => {
|
||||
const serializedNewVariables = newVariables.map((variable) => ({
|
||||
...variable,
|
||||
value: safeStringify(variable.value),
|
||||
value: Array.isArray(variable.value)
|
||||
? variable.value.map(safeStringify).filter(isDefined)
|
||||
: safeStringify(variable.value),
|
||||
}))
|
||||
|
||||
return [
|
||||
|
Reference in New Issue
Block a user