2
0

🔊 Improve invalid typebot update detection

This commit is contained in:
Baptiste Arnaud
2023-01-02 14:44:08 +01:00
parent 7c3b432af5
commit ca2c15dbab
2 changed files with 33 additions and 15 deletions

View File

@@ -57,7 +57,11 @@ 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 = typeof req.body === 'string' ? JSON.parse(req.body) : req.body
const parser = typebotSchema.safeParse(data) const parser = typebotSchema.safeParse({
...data,
updatedAt: new Date(data.updatedAt),
createdAt: new Date(data.createdAt),
})
if ('error' in parser) { if ('error' in parser) {
captureEvent({ captureEvent({
message: 'Typebot schema validation failed', message: 'Typebot schema validation failed',

View File

@@ -1,5 +1,5 @@
import { PrismaClient } from 'db' import { PrismaClient } from 'db'
import { readFileSync, writeFileSync } from 'fs' import { writeFileSync } from 'fs'
import { import {
Block, Block,
BlockOptions, BlockOptions,
@@ -7,20 +7,21 @@ import {
defaultEmailInputOptions, defaultEmailInputOptions,
Group, Group,
InputBlockType, InputBlockType,
PublicTypebot,
publicTypebotSchema,
Theme, Theme,
Typebot, Typebot,
typebotSchema,
} from 'models' } from 'models'
import { isNotDefined } from 'utils' import { isNotDefined } from 'utils'
import { promptAndSetEnvironment } from './utils' import { promptAndSetEnvironment } from './utils'
import { detailedDiff } from 'deep-object-diff' import { detailedDiff } from 'deep-object-diff'
const fixTypebot = (brokenTypebot: Typebot) => const fixTypebot = (brokenTypebot: Typebot | PublicTypebot) =>
({ ({
...brokenTypebot, ...brokenTypebot,
theme: fixTheme(brokenTypebot.theme), theme: fixTheme(brokenTypebot.theme),
groups: fixGroups(brokenTypebot.groups), groups: fixGroups(brokenTypebot.groups),
} satisfies Typebot) } satisfies Typebot | PublicTypebot)
const fixTheme = (brokenTheme: Theme) => const fixTheme = (brokenTheme: Theme) =>
({ ({
@@ -120,18 +121,12 @@ const fixTypebots = async () => {
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'], log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'],
}) })
prisma.$on('query', (e) => { const typebots = await prisma.publicTypebot.findMany()
console.log(e.query)
console.log(e.params)
console.log(e.duration, 'ms')
})
const typebots = JSON.parse(readFileSync('typebots.json', 'utf-8')) as any[]
const total = typebots.length const total = typebots.length
let totalFixed = 0 let totalFixed = 0
let progress = 0 let progress = 0
const fixedTypebots: Typebot[] = [] const fixedTypebots: (Typebot | PublicTypebot)[] = []
const diffs: any[] = [] const diffs: any[] = []
for (const typebot of typebots) { for (const typebot of typebots) {
progress += 1 progress += 1
@@ -140,17 +135,18 @@ const fixTypebots = async () => {
(progress / total) * 100 (progress / total) * 100
)}%) (${totalFixed} fixed typebots)` )}%) (${totalFixed} fixed typebots)`
) )
const parser = typebotSchema.safeParse({ const parser = publicTypebotSchema.safeParse({
...typebot, ...typebot,
updatedAt: new Date(typebot.updatedAt), updatedAt: new Date(typebot.updatedAt),
createdAt: new Date(typebot.createdAt), createdAt: new Date(typebot.createdAt),
}) })
if ('error' in parser) { if ('error' in parser) {
const fixedTypebot = { const fixedTypebot = {
...fixTypebot(typebot), ...fixTypebot(typebot as Typebot | PublicTypebot),
updatedAt: new Date(typebot.updatedAt), updatedAt: new Date(typebot.updatedAt),
createdAt: new Date(typebot.createdAt), createdAt: new Date(typebot.createdAt),
} }
publicTypebotSchema.parse(fixedTypebot)
fixedTypebots.push(fixedTypebot) fixedTypebots.push(fixedTypebot)
totalFixed += 1 totalFixed += 1
diffs.push({ diffs.push({
@@ -168,6 +164,24 @@ const fixTypebots = async () => {
'logs/diffs.json', 'logs/diffs.json',
JSON.stringify(diffs.reverse().slice(0, 100)) JSON.stringify(diffs.reverse().slice(0, 100))
) )
const queries = fixedTypebots.map((fixedTypebot) =>
prisma.publicTypebot.updateMany({
where: { id: fixedTypebot.id },
data: {
...fixedTypebot,
} as any,
})
)
const totalQueries = queries.length
progress = 0
prisma.$on('query', () => {
progress += 1
console.log(`Progress: ${progress}/${totalQueries}`)
})
await prisma.$transaction(queries)
} }
// export const parseZodError = (parser: any) => { // export const parseZodError = (parser: any) => {