From a936bc206adb446fb4bd469e8681b09dafe3a57a Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Tue, 4 Jun 2024 15:41:18 +0200 Subject: [PATCH] :ambulance: Fix webhook sample result parsing --- .../integrations/webhook/parseSampleResult.ts | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts b/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts index 5d2fca584..f512a0a45 100644 --- a/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts +++ b/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts @@ -181,35 +181,38 @@ const getGroupIdsLinkedToGroup = ( typebot: Pick, direction: 'backward' | 'forward', - existingGroupIds: string[] = [] + visitedGroupIds: string[] = [] ) => (groupId: string): string[] => { - if (existingGroupIds.includes(groupId)) return existingGroupIds - const groups = typebot.edges.reduce((groupIds, edge) => { + const linkedGroupIds = typebot.edges.reduce((groupIds, edge) => { const fromGroupId = typebot.groups.find((g) => g.blocks.some( (b) => 'blockId' in edge.from && b.id === edge.from.blockId ) )?.id if (!fromGroupId) return groupIds - if (direction === 'forward') - return (!existingGroupIds || - !existingGroupIds?.includes(edge.to.groupId)) && + if (direction === 'forward') { + if ( + (!visitedGroupIds || !visitedGroupIds?.includes(edge.to.groupId)) && fromGroupId === groupId - ? groupIds.concat(edge.to.groupId) - : groupIds - return (!existingGroupIds || !existingGroupIds.includes(fromGroupId)) && + ) { + visitedGroupIds.push(edge.to.groupId) + return groupIds.concat(edge.to.groupId) + } + return groupIds + } + if ( + !visitedGroupIds.includes(fromGroupId) && edge.to.groupId === groupId - ? groupIds.concat(fromGroupId) - : groupIds + ) { + visitedGroupIds.push(fromGroupId) + return groupIds.concat(fromGroupId) + } + return groupIds }, []) - return groups.concat( - groups.flatMap( - getGroupIdsLinkedToGroup( - typebot, - direction, - existingGroupIds.concat(groups) - ) + return linkedGroupIds.concat( + linkedGroupIds.flatMap( + getGroupIdsLinkedToGroup(typebot, direction, visitedGroupIds) ) ) }