2
0

feat(editor): Restore published version button

Had to migrate webhooks into a standalone table
This commit is contained in:
Baptiste Arnaud
2022-03-01 07:13:09 +01:00
parent 0df719d531
commit e17a1a0869
46 changed files with 578 additions and 348 deletions

View File

@ -22,10 +22,17 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
publishedTypebot: true,
owner: { select: { email: true, name: true, image: true } },
collaborators: { select: { userId: true, type: true } },
webhooks: true,
},
})
if (!typebot) return res.send({ typebot: null })
const { publishedTypebot, owner, collaborators, ...restOfTypebot } = typebot
const {
publishedTypebot,
owner,
collaborators,
webhooks,
...restOfTypebot
} = typebot
const isReadOnly =
collaborators.find((c) => c.userId === user.id)?.type ===
CollaborationType.READ
@ -34,6 +41,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
publishedTypebot,
owner,
isReadOnly,
webhooks,
})
}

View File

@ -0,0 +1,21 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { defaultWebhookAttributes } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import { methodNotAllowed, notAuthenticated } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (req.method === 'POST') {
const typebotId = req.query.typebotId as string
const webhook = await prisma.webhook.create({
data: { typebotId, ...defaultWebhookAttributes },
})
return res.send({ webhook })
}
methodNotAllowed(res)
}
export default withSentry(handler)

View File

@ -0,0 +1,68 @@
import { withSentry } from '@sentry/nextjs'
import { CollaborationType } from 'db'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from 'services/api/utils'
import {
badRequest,
forbidden,
methodNotAllowed,
notAuthenticated,
} from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
const webhookId = req.query.webhookId as string
if (req.method === 'GET') {
const webhook = await prisma.webhook.findFirst({
where: {
id: webhookId,
typebot: {
OR: [
{ ownerId: user.id },
{
collaborators: {
some: {
userId: user.id,
},
},
},
],
},
},
})
return res.send({ webhook })
}
if (req.method === 'PUT') {
const data = req.body
if (!('typebotId' in data)) return badRequest(res)
const typebot = await prisma.typebot.findFirst({
where: {
OR: [
{ id: data.typebotId, ownerId: user.id },
{
collaborators: {
some: {
userId: user.id,
type: CollaborationType.WRITE,
},
},
},
],
},
})
if (!typebot) return forbidden(res)
const webhook = await prisma.webhook.upsert({
where: {
id: webhookId,
},
create: data,
update: data,
})
return res.send({ webhook })
}
methodNotAllowed(res)
}
export default withSentry(handler)