From d80cc1b248275f28928c87d076dba3fcfdc11e89 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Wed, 23 Nov 2022 09:17:37 +0100 Subject: [PATCH] :bug: (results) Fix results display when variable has undefined value --- .../results/api/procedures/getResultsProcedure.ts | 10 +++++++++- apps/docs/openapi/builder.json | 3 +-- packages/bot-engine/src/providers/AnswersProvider.tsx | 6 +++--- packages/models/src/features/typebot/variable.ts | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts b/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts index 3fe2e3a2c19..b8af7c269b6 100644 --- a/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts +++ b/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts @@ -3,6 +3,7 @@ import { canReadTypebot } from '@/utils/api/dbRules' import { authenticatedProcedure } from '@/utils/server/trpc' import { TRPCError } from '@trpc/server' import { ResultWithAnswers, resultWithAnswersSchema } from 'models' +import { isDefined } from 'utils' import { z } from 'zod' const maxLimit = 200 @@ -57,5 +58,12 @@ export const getResultsProcedure = authenticatedProcedure nextCursor = nextResult?.id } - return { results, nextCursor } + return { results: parseResults(results), nextCursor } }) + +// TODO: remove once DB entries are fixed +const parseResults = (results: ResultWithAnswers[]): ResultWithAnswers[] => + results.map((result) => ({ + ...result, + variables: result.variables.filter((variable) => isDefined(variable.value)), + })) diff --git a/apps/docs/openapi/builder.json b/apps/docs/openapi/builder.json index bc504e8d94a..9d2754df3d2 100644 --- a/apps/docs/openapi/builder.json +++ b/apps/docs/openapi/builder.json @@ -90,8 +90,7 @@ "type": "string" }, "value": { - "type": "string", - "nullable": true + "type": "string" } }, "required": [ diff --git a/packages/bot-engine/src/providers/AnswersProvider.tsx b/packages/bot-engine/src/providers/AnswersProvider.tsx index 8abd5c4f3c2..6043922d582 100644 --- a/packages/bot-engine/src/providers/AnswersProvider.tsx +++ b/packages/bot-engine/src/providers/AnswersProvider.tsx @@ -1,12 +1,12 @@ import { safeStringify } from '@/features/variables' import { - Answer, AnswerInput, ResultValues, VariableWithUnknowValue, VariableWithValue, } from 'models' import { createContext, ReactNode, useContext, useState } from 'react' +import { isDefined } from 'utils' const answersContext = createContext<{ resultId?: string @@ -49,7 +49,7 @@ export const AnswersProvider = ({ const serializedNewVariables = newVariables.map((variable) => ({ ...variable, value: safeStringify(variable.value), - })) as VariableWithValue[] + })) setResultValues((resultValues) => { const updatedVariables = [ @@ -57,7 +57,7 @@ export const AnswersProvider = ({ serializedNewVariables.every((variable) => variable.id !== v.id) ), ...serializedNewVariables, - ] + ].filter((variable) => isDefined(variable.value)) as VariableWithValue[] if (onVariablesUpdated) onVariablesUpdated(updatedVariables) return { ...resultValues, diff --git a/packages/models/src/features/typebot/variable.ts b/packages/models/src/features/typebot/variable.ts index 16cdf26290f..7d397db9cef 100644 --- a/packages/models/src/features/typebot/variable.ts +++ b/packages/models/src/features/typebot/variable.ts @@ -12,7 +12,7 @@ export const variableSchema = z.object({ export const variableWithValueSchema = z.object({ id: z.string(), name: z.string(), - value: z.string().nullable(), + value: z.string(), }) /**