feat(api): ✨ Add list results endpoint
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
import { Result as ResultFromPrisma } from 'db'
|
||||
import { VariableWithValue } from '.'
|
||||
import { Answer, VariableWithValue } from '.'
|
||||
|
||||
export type Result = Omit<
|
||||
ResultFromPrisma,
|
||||
'createdAt' | 'prefilledVariables'
|
||||
> & { createdAt: string; prefilledVariables: VariableWithValue[] }
|
||||
|
||||
export type ResultWithAnswers = Result & { answers: Answer[] }
|
||||
|
@ -1,4 +1,6 @@
|
||||
import { Typebot, Answer, VariableWithValue, ResultWithAnswers } from 'models'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { byId, isDefined } from '.'
|
||||
|
||||
export const methodNotAllowed = (res: NextApiResponse) =>
|
||||
res.status(405).json({ message: 'Method Not Allowed' })
|
||||
@ -20,3 +22,27 @@ export const initMiddleware =
|
||||
return resolve(result)
|
||||
})
|
||||
})
|
||||
|
||||
export const parseAnswers =
|
||||
({ blocks, variables }: Pick<Typebot, 'blocks' | 'variables'>) =>
|
||||
(result: ResultWithAnswers) => ({
|
||||
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