2
0

fix(results): 🐛 Collect prefilled variables in db

This commit is contained in:
Baptiste Arnaud
2022-02-17 16:08:01 +01:00
parent 0336bc2a42
commit aaf78e8a54
19 changed files with 454 additions and 507 deletions

View File

@ -1,9 +1,9 @@
import { Result } from 'models'
import { Result, VariableWithValue } from 'models'
import useSWRInfinite from 'swr/infinite'
import { fetcher } from './utils'
import { stringify } from 'qs'
import { Answer } from 'db'
import { sendRequest } from 'utils'
import { isDefined, sendRequest } from 'utils'
const paginationLimit = 50
@ -98,8 +98,18 @@ export const parseDateToReadable = (dateStr: string): string => {
export const convertResultsToTableData = (results?: ResultWithAnswers[]) =>
(results ?? []).map((result) => ({
createdAt: parseDateToReadable(result.createdAt),
...result.answers.reduce(
(o, answer) => ({ ...o, [answer.blockId]: answer.content }),
{}
),
...[...result.answers, ...result.prefilledVariables].reduce<{
[key: string]: string
}>((o, answerOrVariable) => {
if ('blockId' in answerOrVariable) {
const answer = answerOrVariable as Answer
return {
...o,
[answer.variableId ?? answer.blockId]: answer.content,
}
}
const variable = answerOrVariable as VariableWithValue
if (isDefined(o[variable.id])) return o
return { ...o, [variable.id]: variable.value }
}, {}),
}))