2022-03-21 17:05:51 +01:00
|
|
|
import {
|
|
|
|
InputStep,
|
|
|
|
InputStepType,
|
|
|
|
PublicTypebot,
|
|
|
|
ResultHeaderCell,
|
|
|
|
Typebot,
|
|
|
|
} from 'models'
|
|
|
|
import { isInputStep, byId, parseResultHeader, isNotDefined } from 'utils'
|
2022-02-22 10:16:35 +01:00
|
|
|
|
|
|
|
export const parseSampleResult =
|
|
|
|
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
|
|
|
|
(currentBlockId: string): Record<string, string> => {
|
2022-03-21 17:05:51 +01:00
|
|
|
const header = parseResultHeader(typebot)
|
|
|
|
const previousInputSteps = getPreviousInputSteps(typebot)({
|
|
|
|
blockId: currentBlockId,
|
|
|
|
})
|
2022-02-22 10:16:35 +01:00
|
|
|
return {
|
|
|
|
message: 'This is a sample result, it has been generated ⬇️',
|
|
|
|
'Submitted at': new Date().toISOString(),
|
2022-03-21 17:05:51 +01:00
|
|
|
...parseBlocksResultSample(previousInputSteps, header),
|
2022-02-22 10:16:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const parseBlocksResultSample = (
|
2022-03-21 17:05:51 +01:00
|
|
|
inputSteps: InputStep[],
|
|
|
|
header: ResultHeaderCell[]
|
2022-02-22 10:16:35 +01:00
|
|
|
) =>
|
2022-03-21 17:05:51 +01:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}, {})
|
2022-02-22 10:16:35 +01:00
|
|
|
|
|
|
|
const getSampleValue = (step: InputStep) => {
|
|
|
|
switch (step.type) {
|
|
|
|
case InputStepType.CHOICE:
|
2022-03-01 07:46:13 +01:00
|
|
|
return step.options.isMultipleChoice
|
|
|
|
? step.items.map((i) => i.content).join(', ')
|
|
|
|
: step.items[0].content ?? 'Item'
|
2022-02-22 10:16:35 +01:00
|
|
|
case InputStepType.DATE:
|
|
|
|
return new Date().toUTCString()
|
|
|
|
case InputStepType.EMAIL:
|
|
|
|
return 'test@email.com'
|
|
|
|
case InputStepType.NUMBER:
|
|
|
|
return '20'
|
|
|
|
case InputStepType.PHONE:
|
|
|
|
return '+33665566773'
|
|
|
|
case InputStepType.TEXT:
|
|
|
|
return 'answer value'
|
|
|
|
case InputStepType.URL:
|
|
|
|
return 'https://test.com'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 17:05:51 +01:00
|
|
|
const getPreviousInputSteps =
|
|
|
|
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
|
2022-03-21 18:52:24 +01:00
|
|
|
({ blockId }: { blockId: string }): InputStep[] => {
|
2022-03-21 17:05:51 +01:00
|
|
|
const previousInputSteps = getPreviousInputStepsInBlock(typebot)({
|
|
|
|
blockId,
|
|
|
|
})
|
|
|
|
const previousBlockIds = getPreviousBlockIds(typebot)(blockId)
|
|
|
|
return [
|
|
|
|
...previousInputSteps,
|
|
|
|
...previousBlockIds.flatMap((blockId) =>
|
2022-03-21 18:52:24 +01:00
|
|
|
getPreviousInputStepsInBlock(typebot)({ blockId })
|
2022-03-21 17:05:51 +01:00
|
|
|
),
|
|
|
|
]
|
|
|
|
}
|
2022-02-22 10:16:35 +01:00
|
|
|
|
|
|
|
const getPreviousBlockIds =
|
2022-03-21 18:52:24 +01:00
|
|
|
(
|
|
|
|
typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>,
|
|
|
|
existingBlockIds?: string[]
|
|
|
|
) =>
|
2022-02-22 10:16:35 +01:00
|
|
|
(blockId: string): string[] => {
|
2022-03-21 17:05:51 +01:00
|
|
|
const previousBlocks = typebot.edges.reduce<string[]>(
|
|
|
|
(blockIds, edge) =>
|
2022-03-21 18:52:24 +01:00
|
|
|
(!existingBlockIds || !existingBlockIds.includes(edge.from.blockId)) &&
|
2022-03-21 17:05:51 +01:00
|
|
|
edge.to.blockId === blockId
|
|
|
|
? [...blockIds, edge.from.blockId]
|
|
|
|
: blockIds,
|
|
|
|
[]
|
|
|
|
)
|
2022-03-21 18:52:24 +01:00
|
|
|
const newBlocks = [...(existingBlockIds ?? []), ...previousBlocks]
|
2022-02-22 10:16:35 +01:00
|
|
|
return previousBlocks.concat(
|
2022-03-21 18:52:24 +01:00
|
|
|
previousBlocks.flatMap(getPreviousBlockIds(typebot, newBlocks))
|
2022-02-22 10:16:35 +01:00
|
|
|
)
|
|
|
|
}
|
2022-03-21 17:05:51 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|