2
0

feat(webhook): ️ Show linked typebots results in webhook sample

This commit is contained in:
Baptiste Arnaud
2022-04-21 09:18:35 -07:00
parent 937621ee07
commit 12f43cdb88
13 changed files with 249 additions and 88 deletions

View File

@ -1,13 +0,0 @@
import { NextApiRequest, NextApiResponse } from 'next'
import Cors from 'cors'
import { initMiddleware, methodNotAllowed } from 'utils'
const cors = initMiddleware(Cors())
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await cors(req, res)
if (req.method === 'GET') return res.status(200).send({ message: 'success' })
return methodNotAllowed(res)
}
export default handler

View File

@ -27,7 +27,11 @@ import { stringify } from 'qs'
import { withSentry } from '@sentry/nextjs'
import Cors from 'cors'
import { parseSampleResult } from 'services/api/webhooks'
import { saveErrorLog, saveSuccessLog } from 'services/api/utils'
import {
getLinkedTypebots,
saveErrorLog,
saveSuccessLog,
} from 'services/api/utils'
const cors = initMiddleware(Cors())
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
@ -117,9 +121,13 @@ export const executeWebhook =
convertKeyValueTableToObject(webhook.queryParams, variables)
)
const contentType = headers ? headers['Content-Type'] : undefined
const linkedTypebots = await getLinkedTypebots(typebot)
const body =
webhook.method !== HttpMethod.GET
? getBodyContent(typebot)({
? await getBodyContent(
typebot,
linkedTypebots
)({
body: webhook.body,
resultValues,
blockId,
@ -178,8 +186,11 @@ export const executeWebhook =
}
const getBodyContent =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
({
(
typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>,
linkedTypebots: (Typebot | PublicTypebot)[]
) =>
async ({
body,
resultValues,
blockId,
@ -187,13 +198,22 @@ const getBodyContent =
body?: string | null
resultValues?: ResultValues
blockId: string
}): string | undefined => {
}): Promise<string | undefined> => {
if (!body) return
return body === '{{state}}'
? JSON.stringify(
resultValues
? parseAnswers(typebot)(resultValues)
: parseSampleResult(typebot)(blockId)
? parseAnswers({
blocks: [
...typebot.blocks,
...linkedTypebots.flatMap((t) => t.blocks),
],
variables: [
...typebot.variables,
...linkedTypebots.flatMap((t) => t.variables),
],
})(resultValues)
: await parseSampleResult(typebot, linkedTypebots)(blockId)
)
: body
}

View File

@ -1,7 +1,7 @@
import prisma from 'libs/prisma'
import { Typebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { authenticateUser, getLinkedTypebots } from 'services/api/utils'
import { parseSampleResult } from 'services/api/webhooks'
import { methodNotAllowed } from 'utils'
@ -19,7 +19,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.flatMap((b) => b.steps)
.find((s) => s.id === stepId)
if (!step) return res.status(404).send({ message: 'Block not found' })
return res.send(parseSampleResult(typebot)(step.blockId))
const linkedTypebots = await getLinkedTypebots(typebot, user)
return res.send(
await parseSampleResult(typebot, linkedTypebots)(step.blockId)
)
}
methodNotAllowed(res)
}

View File

@ -1,7 +1,7 @@
import prisma from 'libs/prisma'
import { Typebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { authenticateUser, getLinkedTypebots } from 'services/api/utils'
import { parseSampleResult } from 'services/api/webhooks'
import { methodNotAllowed } from 'utils'
@ -15,7 +15,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
where: { id_ownerId: { id: typebotId, ownerId: user.id } },
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
return res.send(parseSampleResult(typebot)(blockId))
const linkedTypebots = await getLinkedTypebots(typebot, user)
return res.send(await parseSampleResult(typebot, linkedTypebots)(blockId))
}
methodNotAllowed(res)
}