2
0

refactor: ♻️ Rename step to block

This commit is contained in:
Baptiste Arnaud
2022-06-11 07:27:38 +02:00
parent 8751766d0e
commit 2df8338505
297 changed files with 4292 additions and 3989 deletions

View File

@ -17,14 +17,17 @@ export const getServerSideProps: GetServerSideProps = async (
try {
if (!host) return { props: {} }
const viewerUrls = (process.env.NEXT_PUBLIC_VIEWER_URL ?? '').split(',')
const isMatchingViewerUrl = viewerUrls.some(
(url) =>
host.split(':')[0].includes(url.split('//')[1].split(':')[0]) ||
(forwardedHost &&
forwardedHost
.split(':')[0]
.includes(url.split('//')[1].split(':')[0]))
)
const isMatchingViewerUrl =
process.env.NEXT_PUBLIC_E2E_TEST === 'enabled'
? true
: viewerUrls.some(
(url) =>
host.split(':')[0].includes(url.split('//')[1].split(':')[0]) ||
(forwardedHost &&
forwardedHost
.split(':')[0]
.includes(url.split('//')[1].split(':')[0]))
)
const customDomain = `${forwardedHost ?? host}${
pathname === '/' ? '' : pathname
}`

View File

@ -10,7 +10,7 @@ import {
Webhook,
WebhookOptions,
WebhookResponse,
WebhookStep,
WebhookBlock,
} from 'models'
import { parseVariables } from 'bot-engine'
import { NextApiRequest, NextApiResponse } from 'next'
@ -38,7 +38,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await cors(req, res)
if (req.method === 'POST') {
const typebotId = req.query.typebotId.toString()
const stepId = req.query.blockId.toString()
const blockId = req.query.blockId.toString()
const resultId = req.query.resultId as string | undefined
const { resultValues, variables } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
@ -51,19 +51,19 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
include: { webhooks: true },
})) as unknown as (Typebot & { webhooks: Webhook[] }) | null
if (!typebot) return notFound(res)
const step = typebot.blocks
.flatMap((b) => b.steps)
.find(byId(stepId)) as WebhookStep
const webhook = typebot.webhooks.find(byId(step.webhookId))
const block = typebot.groups
.flatMap((g) => g.blocks)
.find(byId(blockId)) as WebhookBlock
const webhook = typebot.webhooks.find(byId(block.webhookId))
if (!webhook)
return res
.status(404)
.send({ statusCode: 404, data: { message: `Couldn't find webhook` } })
const preparedWebhook = prepareWebhookAttributes(webhook, step.options)
const preparedWebhook = prepareWebhookAttributes(webhook, block.options)
const result = await executeWebhook(typebot)(
preparedWebhook,
variables,
step.blockId,
block.groupId,
resultValues,
resultId
)
@ -89,7 +89,7 @@ export const executeWebhook =
async (
webhook: Webhook,
variables: Variable[],
blockId: string,
groupId: string,
resultValues?: ResultValues,
resultId?: string
): Promise<WebhookResponse> => {
@ -130,7 +130,7 @@ export const executeWebhook =
)({
body: webhook.body,
resultValues,
blockId,
groupId,
})
: undefined
const request = {
@ -191,33 +191,33 @@ export const executeWebhook =
const getBodyContent =
(
typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>,
typebot: Pick<Typebot | PublicTypebot, 'groups' | 'variables' | 'edges'>,
linkedTypebots: (Typebot | PublicTypebot)[]
) =>
async ({
body,
resultValues,
blockId,
groupId,
}: {
body?: string | null
resultValues?: ResultValues
blockId: string
groupId: string
}): Promise<string | undefined> => {
if (!body) return
return body === '{{state}}'
? JSON.stringify(
resultValues
? parseAnswers({
blocks: [
...typebot.blocks,
...linkedTypebots.flatMap((t) => t.blocks),
groups: [
...typebot.groups,
...linkedTypebots.flatMap((t) => t.groups),
],
variables: [
...typebot.variables,
...linkedTypebots.flatMap((t) => t.variables),
],
})(resultValues)
: await parseSampleResult(typebot, linkedTypebots)(blockId)
: await parseSampleResult(typebot, linkedTypebots)(groupId)
)
: body
}

View File

@ -10,7 +10,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (!user) return res.status(401).json({ message: 'Not authenticated' })
if (req.method === 'GET') {
const typebotId = req.query.typebotId.toString()
const stepId = req.query.blockId.toString()
const blockId = req.query.blockId.toString()
const typebot = (await prisma.typebot.findFirst({
where: {
id: typebotId,
@ -18,13 +18,13 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
},
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
const step = typebot.blocks
.flatMap((b) => b.steps)
.find((s) => s.id === stepId)
if (!step) return res.status(404).send({ message: 'Block not found' })
const block = typebot.groups
.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)
return res.send(
await parseSampleResult(typebot, linkedTypebots)(step.blockId)
await parseSampleResult(typebot, linkedTypebots)(block.groupId)
)
}
methodNotAllowed(res)

View File

@ -6,7 +6,7 @@ import {
Variable,
Webhook,
WebhookOptions,
WebhookStep,
WebhookBlock,
} from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { byId, initMiddleware, methodNotAllowed, notFound } from 'utils'
@ -19,8 +19,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await cors(req, res)
if (req.method === 'POST') {
const typebotId = req.query.typebotId.toString()
const groupId = req.query.groupId.toString()
const blockId = req.query.blockId.toString()
const stepId = req.query.stepId.toString()
const resultId = req.query.resultId as string | undefined
const { resultValues, variables } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
@ -33,19 +33,19 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
include: { webhooks: true },
})) as unknown as (Typebot & { webhooks: Webhook[] }) | null
if (!typebot) return notFound(res)
const step = typebot.blocks
.find(byId(blockId))
?.steps.find(byId(stepId)) as WebhookStep
const webhook = typebot.webhooks.find(byId(step.webhookId))
const block = typebot.groups
.find(byId(groupId))
?.blocks.find(byId(blockId)) as WebhookBlock
const webhook = typebot.webhooks.find(byId(block.webhookId))
if (!webhook)
return res
.status(404)
.send({ statusCode: 404, data: { message: `Couldn't find webhook` } })
const preparedWebhook = prepareWebhookAttributes(webhook, step.options)
const preparedWebhook = prepareWebhookAttributes(webhook, block.options)
const result = await executeWebhook(typebot)(
preparedWebhook,
variables,
blockId,
groupId,
resultValues,
resultId
)

View File

@ -10,7 +10,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (!user) return res.status(401).json({ message: 'Not authenticated' })
if (req.method === 'GET') {
const typebotId = req.query.typebotId.toString()
const blockId = req.query.blockId.toString()
const groupId = req.query.groupId.toString()
const typebot = (await prisma.typebot.findFirst({
where: {
id: typebotId,
@ -19,7 +19,7 @@ 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)
return res.send(await parseSampleResult(typebot, linkedTypebots)(blockId))
return res.send(await parseSampleResult(typebot, linkedTypebots)(groupId))
}
methodNotAllowed(res)
}

View File

@ -1,6 +1,6 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { Typebot, WebhookStep } from 'models'
import { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { byId, methodNotAllowed } from 'utils'
@ -14,8 +14,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(403).send({ message: 'url is missing in body' })
const { url } = body
const typebotId = req.query.typebotId.toString()
const groupId = req.query.groupId.toString()
const blockId = req.query.blockId.toString()
const stepId = req.query.stepId.toString()
const typebot = (await prisma.typebot.findFirst({
where: {
id: typebotId,
@ -24,9 +24,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
try {
const { webhookId } = typebot.blocks
.find(byId(blockId))
?.steps.find(byId(stepId)) as WebhookStep
const { webhookId } = typebot.groups
.find(byId(groupId))
?.blocks.find(byId(blockId)) as WebhookBlock
await prisma.webhook.upsert({
where: { id: webhookId },
update: { url, body: '{{state}}', method: 'POST' },
@ -37,7 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
} catch (err) {
return res
.status(400)
.send({ message: "stepId doesn't point to a Webhook step" })
.send({ message: "blockId doesn't point to a Webhook block" })
}
}
return methodNotAllowed(res)

View File

@ -1,6 +1,6 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { Typebot, WebhookStep } from 'models'
import { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { byId, methodNotAllowed } from 'utils'
@ -10,8 +10,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (!user) return res.status(401).json({ message: 'Not authenticated' })
if (req.method === 'POST') {
const typebotId = req.query.typebotId.toString()
const groupId = req.query.groupId.toString()
const blockId = req.query.blockId.toString()
const stepId = req.query.stepId.toString()
const typebot = (await prisma.typebot.findFirst({
where: {
id: typebotId,
@ -20,9 +20,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
try {
const { webhookId } = typebot.blocks
.find(byId(blockId))
?.steps.find(byId(stepId)) as WebhookStep
const { webhookId } = typebot.groups
.find(byId(groupId))
?.blocks.find(byId(blockId)) as WebhookBlock
await prisma.webhook.update({
where: { id: webhookId },
data: { url: null },
@ -32,7 +32,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
} catch (err) {
return res
.status(400)
.send({ message: "stepId doesn't point to a Webhook step" })
.send({ message: "blockId doesn't point to a Webhook block" })
}
}
return methodNotAllowed(res)

View File

@ -1,6 +1,6 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { Typebot, WebhookStep } from 'models'
import { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { byId, methodNotAllowed } from 'utils'
@ -14,7 +14,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(403).send({ message: 'url is missing in body' })
const { url } = body
const typebotId = req.query.typebotId.toString()
const stepId = req.query.blockId.toString()
const blockId = req.query.blockId.toString()
const typebot = (await prisma.typebot.findFirst({
where: {
id: typebotId,
@ -23,9 +23,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
try {
const { webhookId } = typebot.blocks
.flatMap((b) => b.steps)
.find(byId(stepId)) as WebhookStep
const { webhookId } = typebot.groups
.flatMap((g) => g.blocks)
.find(byId(blockId)) as WebhookBlock
await prisma.webhook.upsert({
where: { id: webhookId },
update: { url, body: '{{state}}', method: 'POST' },
@ -36,7 +36,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
} catch (err) {
return res
.status(400)
.send({ message: "blockId doesn't point to a Webhook step" })
.send({ message: "groupId doesn't point to a Webhook block" })
}
}
return methodNotAllowed(res)

View File

@ -1,6 +1,6 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { Typebot, WebhookStep } from 'models'
import { Typebot, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { byId, methodNotAllowed } from 'utils'
@ -10,7 +10,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (!user) return res.status(401).json({ message: 'Not authenticated' })
if (req.method === 'POST') {
const typebotId = req.query.typebotId.toString()
const stepId = req.query.blockId.toString()
const blockId = req.query.blockId.toString()
const typebot = (await prisma.typebot.findFirst({
where: {
id: typebotId,
@ -19,9 +19,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
try {
const { webhookId } = typebot.blocks
.flatMap((b) => b.steps)
.find(byId(stepId)) as WebhookStep
const { webhookId } = typebot.groups
.flatMap((g) => g.blocks)
.find(byId(blockId)) as WebhookBlock
await prisma.webhook.update({
where: { id: webhookId },
data: { url: null },
@ -31,7 +31,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
} catch (err) {
return res
.status(400)
.send({ message: "blockId doesn't point to a Webhook step" })
.send({ message: "groupId doesn't point to a Webhook block" })
}
}
return methodNotAllowed(res)

View File

@ -11,10 +11,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
) as Answer
const result = await prisma.answer.upsert({
where: {
resultId_blockId_stepId: {
resultId_blockId_groupId: {
resultId: answer.resultId,
groupId: answer.groupId,
blockId: answer.blockId,
stepId: answer.stepId,
},
},
create: answer,

View File

@ -1,9 +1,9 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { Block, WebhookStep } from 'models'
import { Group, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { byId, isWebhookStep, methodNotAllowed } from 'utils'
import { byId, isWebhookBlock, methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
@ -15,24 +15,25 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
id: typebotId,
workspace: { members: { some: { userId: user.id } } },
},
select: { blocks: true, webhooks: true },
select: { groups: true, webhooks: true },
})
const emptyWebhookSteps = (typebot?.blocks as Block[]).reduce<
const emptyWebhookBlocks = (typebot?.groups as Group[]).reduce<
{ blockId: string; name: string; url: string | undefined }[]
>((emptyWebhookSteps, block) => {
const steps = block.steps.filter((step) =>
isWebhookStep(step)
) as WebhookStep[]
>((emptyWebhookBlocks, group) => {
const blocks = group.blocks.filter((block) =>
isWebhookBlock(block)
) as WebhookBlock[]
return [
...emptyWebhookSteps,
...steps.map((s) => ({
blockId: s.id,
name: `${block.title} > ${s.id}`,
url: typebot?.webhooks.find(byId(s.webhookId))?.url ?? undefined,
...emptyWebhookBlocks,
...blocks.map((b) => ({
blockId: b.id,
name: `${group.title} > ${b.id}`,
url: typebot?.webhooks.find(byId(b.webhookId))?.url ?? undefined,
})),
]
}, [])
return res.send({ blocks: emptyWebhookSteps })
console.log({ blocks: emptyWebhookBlocks })
return res.send({ blocks: emptyWebhookBlocks })
}
return methodNotAllowed(res)
}

View File

@ -1,9 +1,9 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { Block } from 'models'
import { Group, WebhookBlock } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { byId, isNotDefined, isWebhookStep, methodNotAllowed } from 'utils'
import { byId, isNotDefined, isWebhookBlock, methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
@ -15,26 +15,28 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
id: typebotId,
workspace: { members: { some: { userId: user.id } } },
},
select: { blocks: true, webhooks: true },
select: { groups: true, webhooks: true },
})
const emptyWebhookSteps = (typebot?.blocks as Block[]).reduce<
{ blockId: string; id: string; name: string }[]
>((emptyWebhookSteps, block) => {
const steps = block.steps.filter(
(step) =>
isWebhookStep(step) &&
isNotDefined(typebot?.webhooks.find(byId(step.webhookId))?.url)
const emptyWebhookBlocks = (typebot?.groups as Group[]).reduce<
{ groupId: string; id: string; name: string }[]
>((emptyWebhookBlocks, group) => {
const blocks = group.blocks.filter(
(block) =>
isWebhookBlock(block) &&
isNotDefined(
typebot?.webhooks.find(byId((block as WebhookBlock).webhookId))?.url
)
)
return [
...emptyWebhookSteps,
...steps.map((s) => ({
...emptyWebhookBlocks,
...blocks.map((s) => ({
id: s.id,
blockId: s.blockId,
name: `${block.title} > ${s.id}`,
groupId: s.groupId,
name: `${group.title} > ${s.id}`,
})),
]
}, [])
return res.send({ steps: emptyWebhookSteps })
return res.send({ blocks: emptyWebhookBlocks })
}
return methodNotAllowed(res)
}