2
0

🩹 Still accept old bot property when importing

This commit is contained in:
Baptiste Arnaud
2023-01-30 07:36:35 +01:00
parent afbb97c6d7
commit 42d4bc3882

View File

@ -10,6 +10,7 @@ import {
import { getAuthenticatedUser } from '@/features/auth/api'
import { parseNewTypebot } from '@/features/dashboard'
import { NewTypebotProps } from '@/features/dashboard/api/parseNewTypebot'
import { omit } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req)
@ -87,13 +88,15 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
select: { plan: true },
})
if (!workspace) return notFound(res, "Couldn't find workspace")
const data = (
const data =
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as NewTypebotProps | Omit<NewTypebotProps, 'groups'>
const formattedData = removeOldProperties(data) as
| NewTypebotProps
| Omit<NewTypebotProps, 'groups'>
const typebot = await prisma.typebot.create({
data:
'groups' in data
? data
'groups' in formattedData
? formattedData
: parseNewTypebot({
ownerAvatarUrl: user.image ?? undefined,
isBrandingEnabled: workspace.plan === Plan.FREE,
@ -112,4 +115,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
}
const removeOldProperties = (data: unknown) => {
if (data && typeof data === 'object' && 'publishedTypebotId' in data) {
return omit(data, 'publishedTypebotId')
}
return data
}
export default handler