2024-01-22 14:54:45 +01:00
|
|
|
import { TRPCError } from '@trpc/server'
|
2024-05-15 14:24:55 +02:00
|
|
|
import { TypebotInSession } from '@typebot.io/schemas'
|
2024-01-22 14:54:45 +01:00
|
|
|
|
|
|
|
export const getFirstEdgeId = ({
|
2024-05-15 14:24:55 +02:00
|
|
|
typebot,
|
2024-01-22 14:54:45 +01:00
|
|
|
startEventId,
|
|
|
|
}: {
|
2024-05-15 14:24:55 +02:00
|
|
|
typebot: Pick<TypebotInSession, 'events' | 'groups' | 'version'>
|
2024-01-22 14:54:45 +01:00
|
|
|
startEventId: string | undefined
|
|
|
|
}) => {
|
|
|
|
if (startEventId) {
|
|
|
|
const event = typebot.events?.find((e) => e.id === startEventId)
|
|
|
|
if (!event)
|
|
|
|
throw new TRPCError({
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
message: "Start event doesn't exist",
|
|
|
|
})
|
|
|
|
return event.outgoingEdgeId
|
|
|
|
}
|
2024-05-15 14:24:55 +02:00
|
|
|
if (typebot.version === '6') return typebot.events?.[0].outgoingEdgeId
|
2024-03-04 14:14:06 +01:00
|
|
|
return typebot.groups.at(0)?.blocks.at(0)?.outgoingEdgeId
|
2024-01-22 14:54:45 +01:00
|
|
|
}
|