2
0

🐛 (typebot) Make sure old typebot properties are removed when pulled

This commit is contained in:
Baptiste Arnaud
2023-02-19 07:38:41 +01:00
parent c32aadc95b
commit d22cc45a97
2 changed files with 15 additions and 5 deletions

View File

@ -0,0 +1,9 @@
import { omit } from 'utils'
export const removeTypebotOldProperties = (data: unknown) => {
if (!data || typeof data !== 'object') return data
if ('publishedTypebotId' in data) {
return omit(data, 'publishedTypebotId')
}
return data
}

View File

@ -8,6 +8,7 @@ import { Typebot } from 'models'
import { omit } from 'utils' import { omit } from 'utils'
import { getTypebot } from '@/features/typebot/api/utils/getTypebot' import { getTypebot } from '@/features/typebot/api/utils/getTypebot'
import { isReadTypebotForbidden } from '@/features/typebot/api/utils/isReadTypebotForbidden' import { isReadTypebotForbidden } from '@/features/typebot/api/utils/isReadTypebotForbidden'
import { removeTypebotOldProperties } from '@/features/typebot/api/utils/removeTypebotOldProperties'
const handler = async (req: NextApiRequest, res: NextApiResponse) => { const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const user = await getAuthenticatedUser(req) const user = await getAuthenticatedUser(req)
@ -15,7 +16,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const typebotId = req.query.typebotId as string const typebotId = req.query.typebotId as string
if (req.method === 'GET') { if (req.method === 'GET') {
const typebot = await prisma.typebot.findFirst({ const fullTypebot = await prisma.typebot.findFirst({
where: { where: {
id: typebotId, id: typebotId,
isArchived: { not: true }, isArchived: { not: true },
@ -26,16 +27,16 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
webhooks: true, webhooks: true,
}, },
}) })
if (!typebot || (await isReadTypebotForbidden(typebot, user))) if (!fullTypebot || (await isReadTypebotForbidden(fullTypebot, user)))
return res.status(404).send({ typebot: null }) return res.status(404).send({ typebot: null })
const { publishedTypebot, collaborators, webhooks, ...restOfTypebot } = const { publishedTypebot, collaborators, webhooks, ...typebot } =
typebot fullTypebot
const isReadOnly = const isReadOnly =
collaborators.find((c) => c.userId === user.id)?.type === collaborators.find((c) => c.userId === user.id)?.type ===
CollaborationType.READ CollaborationType.READ
return res.send({ return res.send({
typebot: restOfTypebot, typebot: removeTypebotOldProperties(typebot),
publishedTypebot, publishedTypebot,
isReadOnly, isReadOnly,
webhooks, webhooks,