2022-11-15 09:35:48 +01:00
|
|
|
import prisma from '@/lib/prisma'
|
2022-11-04 13:53:54 +01:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { methodNotAllowed, notAuthenticated } from '@typebot.io/lib/api'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser'
|
2022-11-04 13:53:54 +01:00
|
|
|
|
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
2023-04-03 16:42:10 +02:00
|
|
|
const user = await getAuthenticatedUser(req, res)
|
2022-11-04 13:53:54 +01:00
|
|
|
if (!user) return notAuthenticated(res)
|
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
|
const publicId = req.query.publicId as string | undefined
|
2023-01-20 11:20:11 +01:00
|
|
|
const exists = await prisma.typebot.count({
|
|
|
|
|
where: { publicId: publicId ?? '' },
|
|
|
|
|
})
|
2022-11-04 13:53:54 +01:00
|
|
|
return res.send({ isAvailable: Boolean(!exists) })
|
|
|
|
|
}
|
|
|
|
|
return methodNotAllowed(res)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 08:39:14 +01:00
|
|
|
export default handler
|