2
0
Files
bot/apps/viewer/src/features/chat/api/utils/startBotFlow.ts

27 lines
922 B
TypeScript
Raw Normal View History

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 (
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
if (!firstEdgeId) return { messages: [], newSessionState: state }
2022-11-29 10:02:40 +01:00
const nextGroup = getNextGroup(state)(firstEdgeId)
if (!nextGroup) return { messages: [], newSessionState: state }
2022-11-29 10:02:40 +01:00
return executeGroup(state)(nextGroup.group)
}