2023-01-16 12:13:21 +01:00
|
|
|
import { TRPCError } from '@trpc/server'
|
2022-11-29 10:02:40 +01:00
|
|
|
import { ChatReply, SessionState } from 'models'
|
|
|
|
|
import { executeGroup } from './executeGroup'
|
|
|
|
|
import { getNextGroup } from './getNextGroup'
|
|
|
|
|
|
|
|
|
|
export const startBotFlow = async (
|
2023-01-16 12:13:21 +01:00
|
|
|
state: SessionState,
|
|
|
|
|
startGroupId?: string
|
|
|
|
|
): Promise<ChatReply & { newSessionState: SessionState }> => {
|
|
|
|
|
if (startGroupId) {
|
|
|
|
|
const group = state.typebot.groups.find(
|
|
|
|
|
(group) => group.id === startGroupId
|
|
|
|
|
)
|
|
|
|
|
if (!group)
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: "startGroupId doesn't exist",
|
|
|
|
|
})
|
|
|
|
|
return executeGroup(state)(group)
|
|
|
|
|
}
|
2022-11-29 10:02:40 +01:00
|
|
|
const firstEdgeId = state.typebot.groups[0].blocks[0].outgoingEdgeId
|
2023-01-16 12:13:21 +01:00
|
|
|
if (!firstEdgeId) return { messages: [], newSessionState: state }
|
2022-11-29 10:02:40 +01:00
|
|
|
const nextGroup = getNextGroup(state)(firstEdgeId)
|
2023-01-16 12:13:21 +01:00
|
|
|
if (!nextGroup) return { messages: [], newSessionState: state }
|
2022-11-29 10:02:40 +01:00
|
|
|
return executeGroup(state)(nextGroup.group)
|
|
|
|
|
}
|