Skip to content

Commit

Permalink
fix(webhook): 🐛 Sample result w/ loops
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 21, 2022
1 parent 7399140 commit 11101a4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions apps/viewer/services/api/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,36 @@ const getSampleValue = (step: InputStep) => {

const getPreviousInputSteps =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
({ blockId, stepId }: { blockId: string; stepId?: string }): InputStep[] => {
({ blockId }: { blockId: string }): InputStep[] => {
const previousInputSteps = getPreviousInputStepsInBlock(typebot)({
blockId,
stepId,
})
const previousBlockIds = getPreviousBlockIds(typebot)(blockId)
return [
...previousInputSteps,
...previousBlockIds.flatMap((blockId) =>
getPreviousInputSteps(typebot)({ blockId })
getPreviousInputStepsInBlock(typebot)({ blockId })
),
]
}

const getPreviousBlockIds =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
(
typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>,
existingBlockIds?: string[]
) =>
(blockId: string): string[] => {
const previousBlocks = typebot.edges.reduce<string[]>(
(blockIds, edge) =>
(!existingBlockIds || !existingBlockIds.includes(edge.from.blockId)) &&
edge.to.blockId === blockId
? [...blockIds, edge.from.blockId]
: blockIds,
[]
)
const newBlocks = [...(existingBlockIds ?? []), ...previousBlocks]
return previousBlocks.concat(
previousBlocks.flatMap(getPreviousBlockIds(typebot))
previousBlocks.flatMap(getPreviousBlockIds(typebot, newBlocks))
)
}

Expand Down

0 comments on commit 11101a4

Please sign in to comment.