2023-09-20 15:26:52 +02:00
|
|
|
import { addEdgeToTypebot, createPortalEdge } from '../../../addEdgeToTypebot'
|
|
|
|
|
import { ExecuteLogicResponse } from '../../../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,
|
2023-11-08 15:34:16 +01:00
|
|
|
{ groupId, blockId }: JumpBlock['options'] = {}
|
2023-03-03 15:03:31 +01:00
|
|
|
): ExecuteLogicResponse => {
|
2023-11-08 15:34:16 +01:00
|
|
|
if (!groupId) return { outgoingEdgeId: undefined }
|
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]
|
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
if (!blockToJumpTo)
|
2023-03-03 15:03:31 +01:00
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'INTERNAL_SERVER_ERROR',
|
|
|
|
|
message: 'Block to jump to is not found',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const portalEdge = createPortalEdge({
|
2023-11-08 15:34:16 +01:00
|
|
|
to: { groupId, blockId: blockToJumpTo?.id },
|
2023-03-03 15:03:31 +01:00
|
|
|
})
|
|
|
|
|
const newSessionState = addEdgeToTypebot(state, portalEdge)
|
|
|
|
|
|
|
|
|
|
return { outgoingEdgeId: portalEdge.id, newSessionState }
|
|
|
|
|
}
|