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, Typebot,
Variable, Variable,
} from 'models' } from 'models'
import { continueBotFlow, getSession, startBotFlow } from '../utils' import {
continueBotFlow,
getSession,
setResultAsCompleted,
startBotFlow,
} from '../utils'
import { omit } from 'utils' import { omit } from 'utils'
export const sendMessageProcedure = publicProcedure 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 { return {
messages, messages,
input, input,

View File

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