2
0

fix(webhook): 🐛 Sample result w/ multi input blocks

This commit is contained in:
Baptiste Arnaud
2022-03-21 17:05:51 +01:00
parent 5d3010d280
commit 7399140e49
12 changed files with 421 additions and 202 deletions

View File

@ -1,39 +1,46 @@
import { Block, InputStep, InputStepType, PublicTypebot, Typebot } from 'models'
import { isInputStep, byId, isDefined } from 'utils'
import {
InputStep,
InputStepType,
PublicTypebot,
ResultHeaderCell,
Typebot,
} from 'models'
import { isInputStep, byId, parseResultHeader, isNotDefined } from 'utils'
export const parseSampleResult =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
(currentBlockId: string): Record<string, string> => {
const previousBlocks = (typebot.blocks as Block[]).filter((b) =>
getPreviousBlockIds(typebot)(currentBlockId).includes(b.id)
)
const parsedBlocks = parseBlocksResultSample(typebot, previousBlocks)
const header = parseResultHeader(typebot)
const previousInputSteps = getPreviousInputSteps(typebot)({
blockId: currentBlockId,
})
return {
message: 'This is a sample result, it has been generated ⬇️',
'Submitted at': new Date().toISOString(),
...parsedBlocks,
...parseVariablesHeaders(typebot, parsedBlocks),
...parseBlocksResultSample(previousInputSteps, header),
}
}
const parseBlocksResultSample = (
typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables'>,
blocks: Block[]
inputSteps: InputStep[],
header: ResultHeaderCell[]
) =>
blocks
.filter((block) => typebot && block.steps.some((step) => isInputStep(step)))
.reduce<Record<string, string>>((blocks, block) => {
const inputStep = block.steps.find((step) => isInputStep(step))
if (!inputStep || !isInputStep(inputStep)) return blocks
const matchedVariableName =
inputStep.options.variableId &&
typebot.variables.find(byId(inputStep.options.variableId))?.name
const value = getSampleValue(inputStep)
return {
...blocks,
[matchedVariableName ?? block.title]: value,
}
}, {})
header.reduce<Record<string, string>>((steps, cell) => {
const inputStep = inputSteps.find((step) => step.id === cell.stepId)
if (isNotDefined(inputStep)) {
if (cell.variableId)
return {
...steps,
[cell.label]: 'content',
}
return steps
}
const value = getSampleValue(inputStep)
return {
...steps,
[cell.label]: value,
}
}, {})
const getSampleValue = (step: InputStep) => {
switch (step.type) {
@ -56,27 +63,46 @@ const getSampleValue = (step: InputStep) => {
}
}
const parseVariablesHeaders = (
typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables'>,
parsedBlocks: Record<string, string>
) =>
typebot.variables.reduce<Record<string, string>>((headers, v) => {
if (parsedBlocks[v.name]) return headers
return {
...headers,
[v.name]: 'value',
}
}, {})
const getPreviousInputSteps =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
({ blockId, stepId }: { blockId: string; stepId?: string }): InputStep[] => {
const previousInputSteps = getPreviousInputStepsInBlock(typebot)({
blockId,
stepId,
})
const previousBlockIds = getPreviousBlockIds(typebot)(blockId)
return [
...previousInputSteps,
...previousBlockIds.flatMap((blockId) =>
getPreviousInputSteps(typebot)({ blockId })
),
]
}
const getPreviousBlockIds =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
(blockId: string): string[] => {
const previousBlocks = typebot.edges
.map((edge) =>
edge.to.blockId === blockId ? edge.from.blockId : undefined
)
.filter(isDefined)
const previousBlocks = typebot.edges.reduce<string[]>(
(blockIds, edge) =>
edge.to.blockId === blockId
? [...blockIds, edge.from.blockId]
: blockIds,
[]
)
return previousBlocks.concat(
previousBlocks.flatMap(getPreviousBlockIds(typebot))
)
}
const getPreviousInputStepsInBlock =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
({ blockId, stepId }: { blockId: string; stepId?: string }) => {
const currentBlock = typebot.blocks.find(byId(blockId))
if (!currentBlock) return []
const inputSteps: InputStep[] = []
for (const step of currentBlock.steps) {
if (step.id === stepId) break
if (isInputStep(step)) inputSteps.push(step)
}
return inputSteps
}