2
0

feat(engine): Add {{state}} to body to get form state

This commit is contained in:
Baptiste Arnaud
2022-02-22 06:55:15 +01:00
parent 1b900b3f5d
commit d0994e6577
8 changed files with 89 additions and 19 deletions

View File

@ -1,4 +1,11 @@
import { Typebot, Answer, VariableWithValue, ResultWithAnswers } from 'models'
import {
Typebot,
Answer,
VariableWithValue,
ResultWithAnswers,
PublicTypebot,
Block,
} from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { byId, isDefined } from '.'
@ -24,17 +31,27 @@ export const initMiddleware =
})
export const parseAnswers =
({ blocks, variables }: Pick<Typebot, 'blocks' | 'variables'>) =>
(result: ResultWithAnswers) => ({
submittedAt: result.createdAt,
...[...result.answers, ...result.prefilledVariables].reduce<{
({
blocks,
variables,
}: Pick<Typebot | PublicTypebot, 'blocks' | 'variables'>) =>
({
createdAt,
answers,
prefilledVariables,
}: Pick<
ResultWithAnswers,
'createdAt' | 'answers' | 'prefilledVariables'
>) => ({
submittedAt: createdAt,
...[...answers, ...prefilledVariables].reduce<{
[key: string]: string
}>((o, answerOrVariable) => {
if ('blockId' in answerOrVariable) {
const answer = answerOrVariable as Answer
const key = answer.variableId
? variables.find(byId(answer.variableId))?.name
: blocks.find(byId(answer.blockId))?.title
: (blocks as Block[]).find(byId(answer.blockId))?.title
if (!key) return o
return {
...o,