2023-09-20 15:26:52 +02:00
|
|
|
import prisma from '@typebot.io/lib/prisma'
|
2022-02-18 14:57:10 +01:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2023-03-15 08:35:16 +01:00
|
|
|
import {
|
|
|
|
badRequest,
|
|
|
|
methodNotAllowed,
|
|
|
|
notAuthenticated,
|
|
|
|
} from '@typebot.io/lib/api'
|
2022-02-18 14:57:10 +01:00
|
|
|
import { got } from 'got'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser'
|
2023-08-28 09:13:53 +02:00
|
|
|
import { env } from '@typebot.io/env'
|
2022-02-18 14:57:10 +01:00
|
|
|
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
2023-04-03 16:42:10 +02:00
|
|
|
const user = await getAuthenticatedUser(req, res)
|
2022-03-04 17:21:01 +01:00
|
|
|
if (!user) return notAuthenticated(res)
|
2022-05-13 15:22:44 -07:00
|
|
|
const workspaceId = req.query.workspaceId as string | undefined
|
|
|
|
if (!workspaceId) return badRequest(res)
|
2022-02-18 14:57:10 +01:00
|
|
|
if (req.method === 'DELETE') {
|
2022-08-08 08:21:36 +02:00
|
|
|
const domain = req.query.domain as string
|
2022-05-02 07:09:43 -07:00
|
|
|
try {
|
|
|
|
await deleteDomainOnVercel(domain)
|
2022-12-22 17:02:34 +01:00
|
|
|
} catch {
|
|
|
|
/* empty */
|
|
|
|
}
|
2022-02-18 14:57:10 +01:00
|
|
|
const customDomains = await prisma.customDomain.delete({
|
|
|
|
where: { name: domain },
|
|
|
|
})
|
2022-05-14 08:11:26 -07:00
|
|
|
|
2022-02-18 14:57:10 +01:00
|
|
|
return res.send({ customDomains })
|
|
|
|
}
|
|
|
|
return methodNotAllowed(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteDomainOnVercel = (name: string) =>
|
|
|
|
got.delete({
|
2023-08-28 09:13:53 +02:00
|
|
|
url: `https://api.vercel.com/v8/projects/${env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME}/domains/${name}?teamId=${env.VERCEL_TEAM_ID}`,
|
|
|
|
headers: { Authorization: `Bearer ${env.VERCEL_TOKEN}` },
|
2022-02-18 14:57:10 +01:00
|
|
|
})
|
|
|
|
|
2022-12-16 08:39:14 +01:00
|
|
|
export default handler
|