@ -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()
|
||||
|
Reference in New Issue
Block a user