2
0

🐛 (webhook) Parse test variables in webhook body sample

Closes #305
This commit is contained in:
Baptiste Arnaud
2023-02-13 14:49:00 +01:00
parent 3728bca173
commit 8a02c701da
5 changed files with 36 additions and 10 deletions

View File

@ -178,6 +178,7 @@ export const executeWebhook =
body: webhook.body, body: webhook.body,
resultValues, resultValues,
groupId, groupId,
variables,
}) })
const { data: body, isJson } = const { data: body, isJson } =
bodyContent && webhook.method !== HttpMethod.GET bodyContent && webhook.method !== HttpMethod.GET
@ -259,17 +260,22 @@ const getBodyContent =
body, body,
resultValues, resultValues,
groupId, groupId,
variables,
}: { }: {
body?: string | null body?: string | null
resultValues?: ResultValues resultValues?: ResultValues
groupId: string groupId: string
variables: Variable[]
}): Promise<string | undefined> => { }): Promise<string | undefined> => {
if (!body) return if (!body) return
return body === '{{state}}' return body === '{{state}}'
? JSON.stringify( ? JSON.stringify(
resultValues resultValues
? parseAnswers(typebot, linkedTypebots)(resultValues) ? parseAnswers(typebot, linkedTypebots)(resultValues)
: await parseSampleResult(typebot, linkedTypebots)(groupId) : await parseSampleResult(typebot, linkedTypebots)(
groupId,
variables
)
) )
: body : body
} }

View File

@ -7,6 +7,7 @@ import {
Block, Block,
Typebot, Typebot,
TypebotLinkBlock, TypebotLinkBlock,
Variable,
} from 'models' } from 'models'
import { isInputBlock, byId, isNotDefined } from 'utils' import { isInputBlock, byId, isNotDefined } from 'utils'
import { parseResultHeader } from 'utils/results' import { parseResultHeader } from 'utils/results'
@ -17,7 +18,8 @@ export const parseSampleResult =
linkedTypebots: (Typebot | PublicTypebot)[] linkedTypebots: (Typebot | PublicTypebot)[]
) => ) =>
async ( async (
currentGroupId: string currentGroupId: string,
variables: Variable[]
): Promise<Record<string, string | boolean | undefined>> => { ): Promise<Record<string, string | boolean | undefined>> => {
const header = parseResultHeader(typebot, linkedTypebots) const header = parseResultHeader(typebot, linkedTypebots)
const linkedInputBlocks = await extractLinkedInputBlocks( const linkedInputBlocks = await extractLinkedInputBlocks(
@ -28,7 +30,7 @@ export const parseSampleResult =
return { return {
message: 'This is a sample result, it has been generated ⬇️', message: 'This is a sample result, it has been generated ⬇️',
'Submitted at': new Date().toISOString(), 'Submitted at': new Date().toISOString(),
...parseResultSample(linkedInputBlocks, header), ...parseResultSample(linkedInputBlocks, header, variables),
} }
} }
@ -78,7 +80,8 @@ const extractLinkedInputBlocks =
const parseResultSample = ( const parseResultSample = (
inputBlocks: InputBlock[], inputBlocks: InputBlock[],
headerCells: ResultHeaderCell[] headerCells: ResultHeaderCell[],
variables: Variable[]
) => ) =>
headerCells.reduce<Record<string, string | boolean | undefined>>( headerCells.reduce<Record<string, string | boolean | undefined>>(
(resultSample, cell) => { (resultSample, cell) => {
@ -86,14 +89,23 @@ const parseResultSample = (
cell.blocks?.some((block) => block.id === inputBlock.id) cell.blocks?.some((block) => block.id === inputBlock.id)
) )
if (isNotDefined(inputBlock)) { if (isNotDefined(inputBlock)) {
if (cell.variableIds) if (cell.variableIds) {
const variableValue = variables.find(
(variable) =>
cell.variableIds?.includes(variable.id) && variable.value
)?.value
return { return {
...resultSample, ...resultSample,
[cell.label]: 'content', [cell.label]: variableValue ?? 'content',
} }
}
return resultSample 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 { return {
...resultSample, ...resultSample,
[cell.label]: value, [cell.label]: value,

View File

@ -143,6 +143,7 @@ export const executeWebhook =
body: webhook.body, body: webhook.body,
resultValues, resultValues,
groupId, groupId,
variables,
}) })
const { data: body, isJson } = const { data: body, isJson } =
bodyContent && webhook.method !== HttpMethod.GET bodyContent && webhook.method !== HttpMethod.GET
@ -227,17 +228,22 @@ const getBodyContent =
body, body,
resultValues, resultValues,
groupId, groupId,
variables,
}: { }: {
body?: string | null body?: string | null
resultValues?: ResultValues resultValues?: ResultValues
groupId: string groupId: string
variables: Variable[]
}): Promise<string | undefined> => { }): Promise<string | undefined> => {
if (!body) return if (!body) return
return body === '{{state}}' return body === '{{state}}'
? JSON.stringify( ? JSON.stringify(
resultValues resultValues
? parseAnswers(typebot, linkedTypebots)(resultValues) ? parseAnswers(typebot, linkedTypebots)(resultValues)
: await parseSampleResult(typebot, linkedTypebots)(groupId) : await parseSampleResult(typebot, linkedTypebots)(
groupId,
variables
)
) )
: body : body
} }

View File

@ -29,7 +29,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
user, user,
})([]) })([])
return res.send( return res.send(
await parseSampleResult(typebot, linkedTypebots)(block.groupId) await parseSampleResult(typebot, linkedTypebots)(block.groupId, [])
) )
} }
methodNotAllowed(res) methodNotAllowed(res)

View File

@ -24,7 +24,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
typebots: [typebot], typebots: [typebot],
user, user,
})([]) })([])
return res.send(await parseSampleResult(typebot, linkedTypebots)(groupId)) return res.send(
await parseSampleResult(typebot, linkedTypebots)(groupId, [])
)
} }
methodNotAllowed(res) methodNotAllowed(res)
} }