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