From 9bb559174ad8aabf0f3a43961f821bd78c0d436b Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 9 Nov 2023 08:52:00 +0100 Subject: [PATCH] :bug: (typebotLink) Fix link to first group with start event --- .../logic/typebotLink/executeTypebotLink.ts | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/bot-engine/blocks/logic/typebotLink/executeTypebotLink.ts b/packages/bot-engine/blocks/logic/typebotLink/executeTypebotLink.ts index 527d9192a43..5670d7cd722 100644 --- a/packages/bot-engine/blocks/logic/typebotLink/executeTypebotLink.ts +++ b/packages/bot-engine/blocks/logic/typebotLink/executeTypebotLink.ts @@ -10,7 +10,7 @@ import { } from '@typebot.io/schemas' import { ExecuteLogicResponse } from '../../../types' import { createId } from '@paralleldrive/cuid2' -import { isNotDefined } from '@typebot.io/lib/utils' +import { isNotDefined, byId } from '@typebot.io/lib/utils' import { createResultIfNotExist } from '../../../queries/createResultIfNotExist' import prisma from '@typebot.io/lib/prisma' import { defaultTypebotLinkOptions } from '@typebot.io/schemas/features/blocks/logic/typebotLink/constants' @@ -47,11 +47,7 @@ export const executeTypebotLink = async ( return { outgoingEdgeId: block.outgoingEdgeId, logs } } newSessionState = await addLinkedTypebotToState(state, block, linkedTypebot) - nextGroupId = - block.options?.groupId ?? - linkedTypebot.groups.find((group) => - group.blocks.some((block) => block.type === 'start') - )?.id + nextGroupId = getNextGroupId(block.options?.groupId, linkedTypebot) } if (!nextGroupId) { @@ -255,3 +251,17 @@ const fetchTypebot = async (state: SessionState, typebotId: string) => { id: typebotId, }) } + +const getNextGroupId = ( + groupId: string | undefined, + typebot: TypebotInSession +) => { + if (groupId) return groupId + if (typebot.version === '6') { + const startEdge = typebot.edges.find(byId(typebot.events[0].outgoingEdgeId)) + return startEdge?.to.groupId + } + return typebot.groups.find((group) => + group.blocks.some((block) => block.type === 'start') + )?.id +}