2
0

🐛 (webhook) Fix parent linked typebot data parsing in webhook

This commit is contained in:
Baptiste Arnaud
2022-12-22 11:49:46 +01:00
parent d1b5b6ebe6
commit c3985b0d50
15 changed files with 166 additions and 75 deletions

View File

@ -21,7 +21,10 @@ import Cors from 'cors'
import prisma from '@/lib/prisma'
import { saveErrorLog, saveSuccessLog } from '@/features/logs/api'
import { parseSampleResult } from '@/features/blocks/integrations/webhook/api'
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/api'
import {
getLinkedTypebots,
getLinkedTypebotsChildren,
} from '@/features/blocks/logic/typebotLink/api'
const cors = initMiddleware(Cors())
@ -31,11 +34,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const typebotId = req.query.typebotId as string
const blockId = req.query.blockId as string
const resultId = req.query.resultId as string | undefined
const { resultValues, variables } = (
const { resultValues, variables, parentTypebotIds } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as {
resultValues: ResultValues | undefined
variables: Variable[]
parentTypebotIds: string[]
}
const typebot = (await prisma.typebot.findUnique({
where: { id: typebotId },
@ -51,13 +55,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.status(404)
.send({ statusCode: 404, data: { message: `Couldn't find webhook` } })
const preparedWebhook = prepareWebhookAttributes(webhook, block.options)
const result = await executeWebhook(typebot)(
preparedWebhook,
const result = await executeWebhook(typebot)({
webhook: preparedWebhook,
variables,
block.groupId,
groupId: block.groupId,
resultValues,
resultId
)
resultId,
parentTypebotIds,
})
return res.status(200).send(result)
}
return methodNotAllowed(res)
@ -79,13 +84,21 @@ const checkIfBodyIsAVariable = (body: string) => /^{{.+}}$/.test(body)
export const executeWebhook =
(typebot: Typebot) =>
async (
webhook: Webhook,
variables: Variable[],
groupId: string,
resultValues?: ResultValues,
async ({
webhook,
variables,
groupId,
resultValues,
resultId,
parentTypebotIds = [],
}: {
webhook: Webhook
variables: Variable[]
groupId: string
resultValues?: ResultValues
resultId?: string
): Promise<WebhookResponse> => {
parentTypebotIds: string[]
}): Promise<WebhookResponse> => {
if (!webhook.url || !webhook.method)
return {
statusCode: 400,
@ -114,11 +127,18 @@ export const executeWebhook =
convertKeyValueTableToObject(webhook.queryParams, variables)
)
const contentType = headers ? headers['Content-Type'] : undefined
const linkedTypebots = await getLinkedTypebots(typebot)
const bodyContent = await getBodyContent(
typebot,
linkedTypebots
)({
const linkedTypebotsParents = await getLinkedTypebots({
isPreview: !('typebotId' in typebot),
typebotIds: parentTypebotIds,
})
const linkedTypebotsChildren = await getLinkedTypebotsChildren({
isPreview: !('typebotId' in typebot),
typebots: [typebot],
})([])
const bodyContent = await getBodyContent(typebot, [
...linkedTypebotsParents,
...linkedTypebotsChildren,
])({
body: webhook.body,
resultValues,
groupId,

View File

@ -1,5 +1,5 @@
import { authenticateUser } from '@/features/auth/api'
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/api'
import { getLinkedTypebotsChildren } from '@/features/blocks/logic/typebotLink/api'
import { parseSampleResult } from '@/features/blocks/integrations/webhook/api'
import prisma from '@/lib/prisma'
import { Typebot } from 'models'
@ -23,7 +23,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.flatMap((g) => g.blocks)
.find((s) => s.id === blockId)
if (!block) return res.status(404).send({ message: 'Group not found' })
const linkedTypebots = await getLinkedTypebots(typebot, user)
const linkedTypebots = await getLinkedTypebotsChildren({
isPreview: true,
typebots: [typebot],
user,
})([])
return res.send(
await parseSampleResult(typebot, linkedTypebots)(block.groupId)
)

View File

@ -23,11 +23,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const groupId = req.query.groupId as string
const blockId = req.query.blockId as string
const resultId = req.query.resultId as string | undefined
const { resultValues, variables } = (
const { resultValues, variables, parentTypebotIds } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as {
resultValues: ResultValues
variables: Variable[]
parentTypebotIds: string[]
}
const typebot = (await prisma.typebot.findUnique({
where: { id: typebotId },
@ -43,13 +44,14 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.status(404)
.send({ statusCode: 404, data: { message: `Couldn't find webhook` } })
const preparedWebhook = prepareWebhookAttributes(webhook, block.options)
const result = await executeWebhook(typebot)(
preparedWebhook,
const result = await executeWebhook(typebot)({
webhook: preparedWebhook,
variables,
groupId,
resultValues,
resultId
)
resultId,
parentTypebotIds,
})
return res.status(200).send(result)
}
return methodNotAllowed(res)

View File

@ -1,5 +1,5 @@
import { authenticateUser } from '@/features/auth/api'
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/api'
import { getLinkedTypebotsChildren } from '@/features/blocks/logic/typebotLink/api'
import { parseSampleResult } from '@/features/blocks/integrations/webhook/api'
import prisma from '@/lib/prisma'
import { Typebot } from 'models'
@ -19,7 +19,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
},
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
const linkedTypebots = await getLinkedTypebots(typebot, user)
const linkedTypebots = await getLinkedTypebotsChildren({
isPreview: true,
typebots: [typebot],
user,
})([])
return res.send(await parseSampleResult(typebot, linkedTypebots)(groupId))
}
methodNotAllowed(res)

View File

@ -15,7 +15,7 @@ import Mail from 'nodemailer/lib/mailer'
import { DefaultBotNotificationEmail } from 'emails'
import { render } from '@faire/mjml-react/dist/src/utils/render'
import prisma from '@/lib/prisma'
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/api'
import { getLinkedTypebotsChildren } from '@/features/blocks/logic/typebotLink/api'
const cors = initMiddleware(Cors())
@ -189,7 +189,9 @@ const getEmailBody = async ({
where: { typebotId },
})) as unknown as PublicTypebot
if (!typebot) return
const linkedTypebots = await getLinkedTypebots(typebot)
const linkedTypebots = await getLinkedTypebotsChildren({
typebots: [typebot],
})([])
const answers = parseAnswers(typebot, linkedTypebots)(resultValues)
return {
html: render(