2
0

⚗️ Implement bot v2 MVP (#194)

Closes #190
This commit is contained in:
Baptiste Arnaud
2022-12-22 17:02:34 +01:00
committed by GitHub
parent e55823e011
commit 1a3869ae6d
202 changed files with 8060 additions and 1152 deletions

View File

@ -1,6 +1,8 @@
import { Prisma, PrismaClient, User, WorkspaceRole } from 'db'
import { env, isNotEmpty } from 'utils'
import { PrismaClient } from 'db'
import { promptAndSetEnvironment } from './utils'
import { groupSchema } from 'models'
import { readFileSync, writeFileSync } from 'fs'
import { exit } from 'process'
const executePlayground = async () => {
await promptAndSetEnvironment()
@ -13,78 +15,27 @@ const executePlayground = async () => {
console.log(e.params)
console.log(e.duration, 'ms')
})
const now = new Date()
const typebot = await prisma.typebot.findFirst({
where: parseReadFilter('6rw8KvRZe7UbHcJrs8Ui4S', {
email: '',
id: 'ckzmhmiey001009mnzt5nkxu8',
}),
select: { id: true },
})
const typebots = JSON.parse(readFileSync('typebots.json', 'utf-8')) as any[]
if (!typebot) return
const totalViews = await prisma.result.count({
where: {
typebotId: {
in: await filterAuthorizedTypebotIds(
prisma,
{
typebotIds: '6rw8KvRZe7UbHcJrs8Ui4S',
user: { email: '', id: 'ckzmhmiey001009mnzt5nkxu8' },
},
'read'
),
},
},
})
const totalStarts = await prisma.result.count({
where: {
typebot: { id: typebot.id },
answers: { some: {} },
},
})
const totalCompleted = await prisma.result.count({
where: {
typebot: { id: typebot.id },
isCompleted: true,
},
})
}
const filterAuthorizedTypebotIds = async (
prisma: PrismaClient,
{
typebotIds,
user,
}: {
typebotIds: string | string[]
user: Pick<User, 'id' | 'email'>
},
role: 'read' | 'write'
) => {
const typebots = await prisma.typebot.findMany({
where: {
id: { in: typebotIds },
workspace:
(role === 'read' && user.email === process.env.ADMIN_EMAIL) ||
isNotEmpty(env('E2E_TEST'))
? undefined
: {
members: {
some: {
userId: user.id,
role:
role === 'write' ? { not: WorkspaceRole.GUEST } : undefined,
},
},
},
},
select: { id: true },
})
return typebots.map((typebot) => typebot.id)
for (const typebot of typebots) {
for (const group of typebot.groups) {
const parser = groupSchema.safeParse(group)
if ('error' in parser) {
console.log(
group.id,
parser.error.issues.map((issue) =>
JSON.stringify({
message: issue.message,
path: issue.path,
})
)
)
writeFileSync('failedTypebot.json', JSON.stringify(typebot))
exit()
}
}
}
}
executePlayground()