♻️ Introduce typebot v6 with events (#1013)

Closes #885
This commit is contained in:
Baptiste Arnaud
2023-11-08 15:34:16 +01:00
committed by GitHub
parent 68e4fc71fb
commit 35300eaf34
634 changed files with 58971 additions and 31449 deletions

View File

@@ -1,61 +0,0 @@
import prisma from '@typebot.io/lib/prisma'
import { InputBlockType, PublicTypebot } from '@typebot.io/schemas'
import { NextApiRequest, NextApiResponse } from 'next'
import { canPublishFileInput } from '@/helpers/databaseRules'
import {
badRequest,
methodNotAllowed,
notAuthenticated,
} from '@typebot.io/lib/api'
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req, res)
if (!user) return notAuthenticated(res)
try {
if (req.method === 'POST') {
const workspaceId = req.query.workspaceId as string | undefined
if (!workspaceId) return badRequest(res, 'workspaceId is required')
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Omit<PublicTypebot, 'id'>
const typebotContainsFileInput = data.groups
.flatMap((g) => g.blocks)
.some((b) => b.type === InputBlockType.FILE)
if (
typebotContainsFileInput &&
!(await canPublishFileInput({ userId: user.id, workspaceId, res }))
)
return
const publicTypebot = await prisma.publicTypebot.create({
data: { ...data },
include: {
typebot: { select: { name: true } },
},
})
await sendTelemetryEvents([
{
name: 'Typebot published',
userId: user.id,
workspaceId,
typebotId: publicTypebot.typebotId,
data: {
isFirstPublish: true,
name: publicTypebot.typebot.name,
},
},
])
return res.send(publicTypebot)
}
return methodNotAllowed(res)
} catch (err) {
console.error(err)
if (err instanceof Error) {
return res.status(500).send({ title: err.name, message: err.message })
}
return res.status(500).send({ message: 'An error occured', error: err })
}
}
export default handler

View File

@@ -1,68 +0,0 @@
import prisma from '@typebot.io/lib/prisma'
import { InputBlockType, PublicTypebot } from '@typebot.io/schemas'
import { NextApiRequest, NextApiResponse } from 'next'
import { canPublishFileInput, canWriteTypebots } from '@/helpers/databaseRules'
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser'
import {
badRequest,
methodNotAllowed,
notAuthenticated,
} from '@typebot.io/lib/api'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req, res)
if (!user) return notAuthenticated(res)
const id = req.query.id as string
const workspaceId = req.query.workspaceId as string | undefined
if (req.method === 'PUT') {
const data = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as PublicTypebot
if (!workspaceId) return badRequest(res, 'workspaceId is required')
const typebotContainsFileInput = data.groups
.flatMap((g) => g.blocks)
.some((b) => b.type === InputBlockType.FILE)
if (
typebotContainsFileInput &&
!(await canPublishFileInput({ userId: user.id, workspaceId, res }))
)
return
const publicTypebot = await prisma.publicTypebot.update({
where: { id },
data,
include: {
typebot: { select: { name: true } },
},
})
await sendTelemetryEvents([
{
name: 'Typebot published',
userId: user.id,
workspaceId,
typebotId: publicTypebot.typebotId,
data: {
name: publicTypebot.typebot.name,
},
},
])
return res.send({ typebot: publicTypebot })
}
if (req.method === 'DELETE') {
const publishedTypebotId = req.query.id as string
const typebotId = req.query.typebotId as string | undefined
if (!typebotId) return badRequest(res, 'typebotId is required')
await prisma.publicTypebot.deleteMany({
where: {
id: publishedTypebotId,
typebot: canWriteTypebots(typebotId, user),
},
})
return res.send({ success: true })
}
return methodNotAllowed(res)
}
export default handler

View File

@@ -182,7 +182,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
})
for (const typebot of typebots) {
const settings = typebot.settings as Settings
if (settings.general.isBrandingEnabled) continue
if (settings.general?.isBrandingEnabled) continue
await prisma.typebot.updateMany({
where: { id: typebot.id },
data: {
@@ -205,7 +205,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
?.settings as Settings | null
if (
!publishedTypebotSettings ||
publishedTypebotSettings?.general.isBrandingEnabled
publishedTypebotSettings?.general?.isBrandingEnabled
)
continue
await prisma.publicTypebot.updateMany({