From 8a02c701da2970346de179881b18402df0cffd5e Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 13 Feb 2023 14:49:00 +0100 Subject: [PATCH] :bug: (webhook) Parse test variables in webhook body sample Closes #305 --- .../webhook/api/utils/executeWebhookBlock.ts | 8 ++++++- .../webhook/api/utils/parseSampleResult.ts | 24 ++++++++++++++----- .../blocks/[blockId]/executeWebhook.ts | 8 ++++++- .../blocks/[blockId]/sampleResult.ts | 2 +- .../[blockId]/steps/[stepId]/sampleResult.ts | 4 +++- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/apps/viewer/src/features/blocks/integrations/webhook/api/utils/executeWebhookBlock.ts b/apps/viewer/src/features/blocks/integrations/webhook/api/utils/executeWebhookBlock.ts index ddaa7f350a..2489a5cb2a 100644 --- a/apps/viewer/src/features/blocks/integrations/webhook/api/utils/executeWebhookBlock.ts +++ b/apps/viewer/src/features/blocks/integrations/webhook/api/utils/executeWebhookBlock.ts @@ -178,6 +178,7 @@ export const executeWebhook = body: webhook.body, resultValues, groupId, + variables, }) const { data: body, isJson } = bodyContent && webhook.method !== HttpMethod.GET @@ -259,17 +260,22 @@ const getBodyContent = body, resultValues, groupId, + variables, }: { body?: string | null resultValues?: ResultValues groupId: string + variables: Variable[] }): Promise => { if (!body) return return body === '{{state}}' ? JSON.stringify( resultValues ? parseAnswers(typebot, linkedTypebots)(resultValues) - : await parseSampleResult(typebot, linkedTypebots)(groupId) + : await parseSampleResult(typebot, linkedTypebots)( + groupId, + variables + ) ) : body } diff --git a/apps/viewer/src/features/blocks/integrations/webhook/api/utils/parseSampleResult.ts b/apps/viewer/src/features/blocks/integrations/webhook/api/utils/parseSampleResult.ts index a774a6bab3..e4d5e72447 100644 --- a/apps/viewer/src/features/blocks/integrations/webhook/api/utils/parseSampleResult.ts +++ b/apps/viewer/src/features/blocks/integrations/webhook/api/utils/parseSampleResult.ts @@ -7,6 +7,7 @@ import { Block, Typebot, TypebotLinkBlock, + Variable, } from 'models' import { isInputBlock, byId, isNotDefined } from 'utils' import { parseResultHeader } from 'utils/results' @@ -17,7 +18,8 @@ export const parseSampleResult = linkedTypebots: (Typebot | PublicTypebot)[] ) => async ( - currentGroupId: string + currentGroupId: string, + variables: Variable[] ): Promise> => { const header = parseResultHeader(typebot, linkedTypebots) const linkedInputBlocks = await extractLinkedInputBlocks( @@ -28,7 +30,7 @@ export const parseSampleResult = return { message: 'This is a sample result, it has been generated ⬇️', 'Submitted at': new Date().toISOString(), - ...parseResultSample(linkedInputBlocks, header), + ...parseResultSample(linkedInputBlocks, header, variables), } } @@ -78,7 +80,8 @@ const extractLinkedInputBlocks = const parseResultSample = ( inputBlocks: InputBlock[], - headerCells: ResultHeaderCell[] + headerCells: ResultHeaderCell[], + variables: Variable[] ) => headerCells.reduce>( (resultSample, cell) => { @@ -86,14 +89,23 @@ const parseResultSample = ( cell.blocks?.some((block) => block.id === inputBlock.id) ) if (isNotDefined(inputBlock)) { - if (cell.variableIds) + if (cell.variableIds) { + const variableValue = variables.find( + (variable) => + cell.variableIds?.includes(variable.id) && variable.value + )?.value return { ...resultSample, - [cell.label]: 'content', + [cell.label]: variableValue ?? 'content', } + } + return resultSample } - const value = getSampleValue(inputBlock) + const variableValue = variables.find( + (variable) => cell.variableIds?.includes(variable.id) && variable.value + )?.value + const value = variableValue ?? getSampleValue(inputBlock) return { ...resultSample, [cell.label]: value, diff --git a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts index eb59848852..b0106f8ca1 100644 --- a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts +++ b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts @@ -143,6 +143,7 @@ export const executeWebhook = body: webhook.body, resultValues, groupId, + variables, }) const { data: body, isJson } = bodyContent && webhook.method !== HttpMethod.GET @@ -227,17 +228,22 @@ const getBodyContent = body, resultValues, groupId, + variables, }: { body?: string | null resultValues?: ResultValues groupId: string + variables: Variable[] }): Promise => { if (!body) return return body === '{{state}}' ? JSON.stringify( resultValues ? parseAnswers(typebot, linkedTypebots)(resultValues) - : await parseSampleResult(typebot, linkedTypebots)(groupId) + : await parseSampleResult(typebot, linkedTypebots)( + groupId, + variables + ) ) : body } diff --git a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/sampleResult.ts b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/sampleResult.ts index 40258ce2be..fcc473f9ce 100644 --- a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/sampleResult.ts +++ b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/sampleResult.ts @@ -29,7 +29,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { user, })([]) return res.send( - await parseSampleResult(typebot, linkedTypebots)(block.groupId) + await parseSampleResult(typebot, linkedTypebots)(block.groupId, []) ) } methodNotAllowed(res) diff --git a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/sampleResult.ts b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/sampleResult.ts index 059a79c152..3ff90179dd 100644 --- a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/sampleResult.ts +++ b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/sampleResult.ts @@ -24,7 +24,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { typebots: [typebot], user, })([]) - return res.send(await parseSampleResult(typebot, linkedTypebots)(groupId)) + return res.send( + await parseSampleResult(typebot, linkedTypebots)(groupId, []) + ) } methodNotAllowed(res) }