🦴 Add viewer backbone
This commit is contained in:
30
apps/builder/pages/api/publicTypebots.ts
Normal file
30
apps/builder/pages/api/publicTypebots.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import prisma from 'libs/prisma'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
import { methodNotAllowed } from 'services/api/utils'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session = await getSession({ req })
|
||||
|
||||
if (!session?.user)
|
||||
return res.status(401).json({ message: 'Not authenticated' })
|
||||
|
||||
try {
|
||||
if (req.method === 'POST') {
|
||||
const data = JSON.parse(req.body)
|
||||
const typebot = await prisma.publicTypebot.create({
|
||||
data: { ...data },
|
||||
})
|
||||
return res.send(typebot)
|
||||
}
|
||||
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
|
24
apps/builder/pages/api/publicTypebots/[id].ts
Normal file
24
apps/builder/pages/api/publicTypebots/[id].ts
Normal file
@ -0,0 +1,24 @@
|
||||
import prisma from 'libs/prisma'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
import { methodNotAllowed } from 'services/api/utils'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session = await getSession({ req })
|
||||
|
||||
if (!session?.user)
|
||||
return res.status(401).json({ message: 'Not authenticated' })
|
||||
|
||||
const id = req.query.id.toString()
|
||||
if (req.method === 'PUT') {
|
||||
const data = JSON.parse(req.body)
|
||||
const typebots = await prisma.publicTypebot.update({
|
||||
where: { id },
|
||||
data,
|
||||
})
|
||||
return res.send({ typebots })
|
||||
}
|
||||
return methodNotAllowed(res)
|
||||
}
|
||||
|
||||
export default handler
|
@ -13,8 +13,13 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
if (req.method === 'GET') {
|
||||
const typebot = await prisma.typebot.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
publishedTypebot: true,
|
||||
},
|
||||
})
|
||||
return res.send({ typebot })
|
||||
if (!typebot) return res.send({ typebot: null })
|
||||
const { publishedTypebot, ...restOfTypebot } = typebot
|
||||
return res.send({ typebot: restOfTypebot, publishedTypebot })
|
||||
}
|
||||
if (req.method === 'DELETE') {
|
||||
const typebots = await prisma.typebot.delete({
|
||||
|
Reference in New Issue
Block a user