2022-11-15 09:35:48 +01:00
|
|
|
import prisma from '@/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'
|
2022-11-22 17:30:20 +01:00
|
|
|
import { getAuthenticatedUser } from '@/features/auth/api'
|
2022-02-18 14:57:10 +01:00
|
|
|
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
2022-03-04 17:21:01 +01:00
|
|
|
const user = await getAuthenticatedUser(req)
|
|
|
|
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-01-21 18:20:10 +01:00
|
|
|
url: `https://api.vercel.com/v8/projects/${process.env.NEXT_PUBLIC_VERCEL_VIEWER_PROJECT_NAME}/domains/${name}?teamId=${process.env.VERCEL_TEAM_ID}`,
|
2022-02-18 14:57:10 +01:00
|
|
|
headers: { Authorization: `Bearer ${process.env.VERCEL_TOKEN}` },
|
|
|
|
})
|
|
|
|
|
2022-12-16 08:39:14 +01:00
|
|
|
export default handler
|