🐛 (editor) Fix update typebot when having more than 100.000 results
This commit is contained in:
@ -4,8 +4,7 @@ import { NextApiRequest, NextApiResponse } from 'next'
|
|||||||
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
import { methodNotAllowed, notAuthenticated } from 'utils/api'
|
||||||
import { getAuthenticatedUser } from '@/features/auth/api'
|
import { getAuthenticatedUser } from '@/features/auth/api'
|
||||||
import { archiveResults } from '@/features/results/api'
|
import { archiveResults } from '@/features/results/api'
|
||||||
import { Typebot, typebotSchema } from 'models'
|
import { Typebot } from 'models'
|
||||||
import { captureEvent } from '@sentry/nextjs'
|
|
||||||
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'
|
||||||
@ -69,21 +68,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'PUT') {
|
if (req.method === 'PUT') {
|
||||||
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
const data = (
|
||||||
const parser = typebotSchema.safeParse({
|
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
||||||
...data,
|
) as Typebot
|
||||||
updatedAt: new Date(data.updatedAt),
|
|
||||||
createdAt: new Date(data.createdAt),
|
|
||||||
})
|
|
||||||
if ('error' in parser) {
|
|
||||||
captureEvent({
|
|
||||||
message: 'Typebot schema validation failed',
|
|
||||||
extra: {
|
|
||||||
typebotId: data.id,
|
|
||||||
error: parser.error,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const typebot = await getTypebot({
|
const typebot = await getTypebot({
|
||||||
accessLevel: 'write',
|
accessLevel: 'write',
|
||||||
@ -96,17 +83,22 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
if (!typebot) return res.status(404).send({ message: 'Typebot not found' })
|
if (!typebot) return res.status(404).send({ message: 'Typebot not found' })
|
||||||
|
|
||||||
if ((typebot.updatedAt as Date) > new Date(data.updatedAt))
|
if ((typebot.updatedAt as Date) > new Date(data.updatedAt))
|
||||||
return res.send({ message: 'Found newer version of typebot in database' })
|
return res.send({
|
||||||
const typebots = await prisma.typebot.updateMany({
|
message: 'Found newer version of the typebot in database',
|
||||||
where: { id: typebotId },
|
})
|
||||||
data: removeOldProperties({
|
|
||||||
...data,
|
const updates = {
|
||||||
|
...omit(data, 'id', 'createdAt', 'updatedAt'),
|
||||||
theme: data.theme ?? undefined,
|
theme: data.theme ?? undefined,
|
||||||
settings: data.settings ?? undefined,
|
settings: data.settings ?? undefined,
|
||||||
resultsTablePreferences: data.resultsTablePreferences ?? undefined,
|
resultsTablePreferences: data.resultsTablePreferences ?? undefined,
|
||||||
}) as Prisma.TypebotUpdateInput,
|
} satisfies Prisma.TypebotUpdateInput
|
||||||
|
|
||||||
|
const { count } = await prisma.typebot.updateMany({
|
||||||
|
where: { id: typebotId },
|
||||||
|
data: updates,
|
||||||
})
|
})
|
||||||
return res.send({ typebots })
|
return res.send({ count })
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'PATCH') {
|
if (req.method === 'PATCH') {
|
||||||
@ -126,12 +118,4 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
return methodNotAllowed(res)
|
return methodNotAllowed(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Remove in a month
|
|
||||||
const removeOldProperties = (data: unknown) => {
|
|
||||||
if (data && typeof data === 'object' && 'publishedTypebotId' in data) {
|
|
||||||
return omit(data, 'publishedTypebotId')
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
export default handler
|
export default handler
|
||||||
|
Reference in New Issue
Block a user