From 409c55308e5e7b6bf91f62f722db78cdb04cc0c3 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 30 May 2024 10:19:19 +0200 Subject: [PATCH] :bug: Fix sample result parsing on complex flows Make sure to ignore already visited group ids --- .../integrations/webhook/parseSampleResult.ts | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts b/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts index e3499f054..5d2fca584 100644 --- a/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts +++ b/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts @@ -166,22 +166,25 @@ const walkEdgesAndExtract = )({ groupId: currentGroupId, }) - const otherGroupIds = getGroupIds(typebot, direction)(currentGroupId) - return [ - ...blocksInGroup, - ...otherGroupIds.flatMap((groupId) => + const otherGroupIds = getGroupIdsLinkedToGroup( + typebot, + direction + )(currentGroupId) + return blocksInGroup.concat( + otherGroupIds.flatMap((groupId) => extractBlocksInGroup(type, typebot)({ groupId }) - ), - ] + ) + ) } -const getGroupIds = +const getGroupIdsLinkedToGroup = ( typebot: Pick, direction: 'backward' | 'forward', - existingGroupIds?: string[] + existingGroupIds: string[] = [] ) => (groupId: string): string[] => { + if (existingGroupIds.includes(groupId)) return existingGroupIds const groups = typebot.edges.reduce((groupIds, edge) => { const fromGroupId = typebot.groups.find((g) => g.blocks.some( @@ -193,16 +196,21 @@ const getGroupIds = return (!existingGroupIds || !existingGroupIds?.includes(edge.to.groupId)) && fromGroupId === groupId - ? [...groupIds, edge.to.groupId] + ? groupIds.concat(edge.to.groupId) : groupIds return (!existingGroupIds || !existingGroupIds.includes(fromGroupId)) && edge.to.groupId === groupId - ? [...groupIds, fromGroupId] + ? groupIds.concat(fromGroupId) : groupIds }, []) - const newGroups = [...(existingGroupIds ?? []), ...groups] return groups.concat( - groups.flatMap(getGroupIds(typebot, direction, newGroups)) + groups.flatMap( + getGroupIdsLinkedToGroup( + typebot, + direction, + existingGroupIds.concat(groups) + ) + ) ) }