feat(api): ✨ Add get sample result endpoint
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import { ResultWithAnswers, Typebot, VariableWithValue } from 'models'
|
||||
import { ResultWithAnswers, VariableWithValue } from 'models'
|
||||
import useSWRInfinite from 'swr/infinite'
|
||||
import { fetcher } from './utils'
|
||||
import { stringify } from 'qs'
|
||||
import { Answer } from 'db'
|
||||
import { byId, isDefined, sendRequest } from 'utils'
|
||||
import { isDefined, sendRequest } from 'utils'
|
||||
|
||||
const paginationLimit = 50
|
||||
|
||||
@ -112,28 +112,3 @@ export const convertResultsToTableData = (results?: ResultWithAnswers[]) =>
|
||||
return { ...o, [variable.id]: variable.value }
|
||||
}, {}),
|
||||
}))
|
||||
|
||||
export const parseAnswers = (
|
||||
result: ResultWithAnswers,
|
||||
{ blocks, variables }: Pick<Typebot, 'blocks' | 'variables'>
|
||||
) => ({
|
||||
submittedAt: result.createdAt,
|
||||
...[...result.answers, ...result.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
|
||||
if (!key) return o
|
||||
return {
|
||||
...o,
|
||||
[key]: answer.content,
|
||||
}
|
||||
}
|
||||
const variable = answerOrVariable as VariableWithValue
|
||||
if (isDefined(o[variable.id])) return o
|
||||
return { ...o, [variable.id]: variable.value }
|
||||
}, {}),
|
||||
})
|
||||
|
Reference in New Issue
Block a user