2
0

🐛 (webhook) Prioritize variables parsing over answers

This commit is contained in:
Baptiste Arnaud
2022-12-23 14:59:34 +01:00
parent 11ff7eab56
commit 64cd31cf13
2 changed files with 22 additions and 19 deletions

View File

@@ -255,6 +255,7 @@ const convertKeyValueTableToObject = (
}, {}) }, {})
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const safeJsonParse = (json: string): { data: any; isJson: boolean } => { const safeJsonParse = (json: string): { data: any; isJson: boolean } => {
try { try {
return { data: JSON.parse(json), isJson: true } return { data: JSON.parse(json), isJson: true }

View File

@@ -190,26 +190,28 @@ export const parseAnswers =
...[...answers, ...resultVariables].reduce<{ ...[...answers, ...resultVariables].reduce<{
[key: string]: string [key: string]: string
}>((o, answerOrVariable) => { }>((o, answerOrVariable) => {
if ('blockId' in answerOrVariable) { const isVariable = !('blockId' in answerOrVariable)
const answer = answerOrVariable as Answer if (isVariable) {
const key = answer.variableId const variable = answerOrVariable as VariableWithValue
? header.find( if (variable.value === null) return o
(cell) => return { ...o, [variable.name]: variable.value }
answer.variableId && }
cell.variableIds?.includes(answer.variableId) const answer = answerOrVariable as Answer
)?.label const key = answer.variableId
: header.find((cell) => ? header.find(
cell.blocks?.some((block) => block.id === answer.blockId) (cell) =>
)?.label answer.variableId &&
if (!key) return o cell.variableIds?.includes(answer.variableId)
return { )?.label
...o, : header.find((cell) =>
[key]: answer.content.toString(), cell.blocks?.some((block) => block.id === answer.blockId)
} )?.label
if (!key) return o
if (isDefined(o[key])) return o
return {
...o,
[key]: answer.content.toString(),
} }
const variable = answerOrVariable as VariableWithValue
if (isDefined(o[variable.name]) || variable.value === null) return o
return { ...o, [variable.name]: variable.value }
}, {}), }, {}),
} }
} }