2
0

🐛 Make sure variables are properly overwritten

And also correctly mark result as completed with new engine
This commit is contained in:
Baptiste Arnaud
2023-02-21 19:08:21 +01:00
parent 83ae57cf0c
commit 148315f6ee
2 changed files with 38 additions and 34 deletions

View File

@ -21,7 +21,12 @@ import {
Typebot,
Variable,
} from 'models'
import { continueBotFlow, getSession, startBotFlow } from '../utils'
import {
continueBotFlow,
getSession,
setResultAsCompleted,
startBotFlow,
} from '../utils'
import { omit } from 'utils'
export const sendMessageProcedure = publicProcedure
@ -77,6 +82,9 @@ export const sendMessageProcedure = publicProcedure
},
})
if (!input && session.state.result?.hasStarted)
await setResultAsCompleted(session.state.result.id)
return {
messages,
input,

View File

@ -5,7 +5,7 @@ import {
validatePhoneNumber,
} from '@/features/blocks/inputs/phone/api'
import { validateUrl } from '@/features/blocks/inputs/url/api'
import { parseVariables } from '@/features/variables'
import { parseVariables, updateVariables } from '@/features/variables'
import prisma from '@/lib/prisma'
import { TRPCError } from '@trpc/server'
import got from 'got'
@ -17,7 +17,6 @@ import {
InputBlock,
InputBlockType,
SessionState,
Variable,
} from 'models'
import { isInputBlock, isNotDefined } from 'utils'
import { executeGroup } from './executeGroup'
@ -58,19 +57,11 @@ export const continueBotFlow =
if (formattedReply && !isReplyValid(formattedReply, block))
return parseRetryMessage(block)
const newVariables = await processAndSaveAnswer(
const newSessionState = await processAndSaveAnswer(
state,
block
)(formattedReply)
const newSessionState = {
...state,
typebot: {
...state.typebot,
variables: newVariables,
},
}
const groupHasMoreBlocks = blockIndex < group.blocks.length - 1
const nextEdgeId = getOutgoingEdgeId(newSessionState)(block, formattedReply)
@ -93,38 +84,36 @@ export const continueBotFlow =
}
const processAndSaveAnswer =
(
state: Pick<SessionState, 'result' | 'typebot' | 'isPreview'>,
block: InputBlock
) =>
async (reply: string | null): Promise<Variable[]> => {
if (!reply) return state.typebot.variables
(state: SessionState, block: InputBlock) =>
async (reply: string | null): Promise<SessionState> => {
if (!reply) return state
if (!state.isPreview && state.result) {
await saveAnswer(state.result.id, block)(reply)
if (!state.result.hasStarted) await setResultAsStarted(state.result.id)
}
const newVariables = saveVariableValueIfAny(state, block)(reply)
return newVariables
const newState = await saveVariableValueIfAny(state, block)(reply)
return {
...newState,
result: newState.result
? { ...newState.result, hasStarted: true }
: undefined,
}
}
const saveVariableValueIfAny =
(state: Pick<SessionState, 'result' | 'typebot'>, block: InputBlock) =>
(reply: string): Variable[] => {
if (!block.options.variableId) return state.typebot.variables
const variable = state.typebot.variables.find(
(state: SessionState, block: InputBlock) =>
async (reply: string): Promise<SessionState> => {
if (!block.options.variableId) return state
const foundVariable = state.typebot.variables.find(
(variable) => variable.id === block.options.variableId
)
if (!variable) return state.typebot.variables
if (!foundVariable) return state
return [
...state.typebot.variables.filter(
(variable) => variable.id !== block.options.variableId
),
{
...variable,
value: reply,
},
]
const newSessionState = await updateVariables(state)([
{ ...foundVariable, value: reply },
])
return newSessionState
}
const setResultAsStarted = async (resultId: string) => {
@ -134,6 +123,13 @@ const setResultAsStarted = async (resultId: string) => {
})
}
export const setResultAsCompleted = async (resultId: string) => {
await prisma.result.update({
where: { id: resultId },
data: { isCompleted: true },
})
}
const parseRetryMessage = (
block: InputBlock
): Pick<ChatReply, 'messages' | 'input'> => {