2023-03-03 15:03:31 +01:00
|
|
|
import {
|
|
|
|
|
addEdgeToTypebot,
|
|
|
|
|
createPortalEdge,
|
2023-03-15 12:21:52 +01:00
|
|
|
} from '@/features/chat/helpers/addEdgeToTypebot'
|
|
|
|
|
import { ExecuteLogicResponse } from '@/features/chat/types'
|
2023-03-03 15:03:31 +01:00
|
|
|
import { TRPCError } from '@trpc/server'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { SessionState } from '@typebot.io/schemas'
|
|
|
|
|
import { JumpBlock } from '@typebot.io/schemas/features/blocks/logic/jump'
|
2023-03-03 15:03:31 +01:00
|
|
|
|
|
|
|
|
export const executeJumpBlock = (
|
|
|
|
|
state: SessionState,
|
|
|
|
|
{ groupId, blockId }: JumpBlock['options']
|
|
|
|
|
): ExecuteLogicResponse => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const { typebot } = state.typebotsQueue[0]
|
|
|
|
|
const groupToJumpTo = typebot.groups.find((group) => group.id === groupId)
|
2023-03-03 15:03:31 +01:00
|
|
|
const blockToJumpTo =
|
|
|
|
|
groupToJumpTo?.blocks.find((block) => block.id === blockId) ??
|
|
|
|
|
groupToJumpTo?.blocks[0]
|
|
|
|
|
|
|
|
|
|
if (!blockToJumpTo?.groupId)
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'INTERNAL_SERVER_ERROR',
|
|
|
|
|
message: 'Block to jump to is not found',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const portalEdge = createPortalEdge({
|
|
|
|
|
to: { groupId: blockToJumpTo?.groupId, blockId: blockToJumpTo?.id },
|
|
|
|
|
})
|
|
|
|
|
const newSessionState = addEdgeToTypebot(state, portalEdge)
|
|
|
|
|
|
|
|
|
|
return { outgoingEdgeId: portalEdge.id, newSessionState }
|
|
|
|
|
}
|