Skip to content

Commit

Permalink
🧐 Add data exploration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jan 8, 2024
1 parent 2385eaf commit 7d6c964
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 16 deletions.
80 changes: 80 additions & 0 deletions packages/scripts/destroyUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { PrismaClient } from '@typebot.io/prisma'
import * as p from '@clack/prompts'
import { promptAndSetEnvironment } from './utils'

const destroyUser = async () => {
await promptAndSetEnvironment('production')

const prisma = new PrismaClient({
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'],
})

prisma.$on('query', (e) => {
console.log(e.query)
console.log(e.params)
console.log(e.duration, 'ms')
})

const email = (await p.text({
message: 'User email?',
})) as string

if (!email || typeof email !== 'string') {
console.log('No email provided')
return
}

const workspaces = await prisma.workspace.findMany({
where: {
members: { every: { user: { email } } },
},
include: {
members: { select: { user: { select: { email: true } }, role: true } },
typebots: {
select: {
results: {
select: { id: true },
},
},
},
},
})

console.log(`Found ${workspaces.length} workspaces`)

const proceed = await p.confirm({ message: 'Proceed?' })
if (!proceed || typeof proceed !== 'boolean') {
console.log('Aborting')
return
}

for (const workspace of workspaces) {
const hasResults = workspace.typebots.some((t) => t.results.length > 0)
if (hasResults) {
console.log(
`Workspace ${workspace.name} has results. Deleting results first...`,
workspace.typebots.filter((t) => t.results.length > 0)
)
console.log(JSON.stringify({ members: workspace.members }, null, 2))
const proceed = await p.confirm({ message: 'Proceed?' })
if (!proceed || typeof proceed !== 'boolean') {
console.log('Aborting')
return
}
}
for (const typebot of workspace.typebots.filter(
(t) => t.results.length > 0
)) {
for (const result of typebot.results) {
await prisma.result.deleteMany({ where: { id: result.id } })
}
}
await prisma.workspace.delete({ where: { id: workspace.id } })
}

const user = await prisma.user.delete({ where: { email } })

console.log(`Deleted user ${JSON.stringify(user, null, 2)}`)
}

destroyUser()
6 changes: 5 additions & 1 deletion packages/scripts/inspectUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const inspectUser = async () => {
select: {
workspace: {
select: {
id: true,
name: true,
plan: true,
members: {
Expand All @@ -49,6 +50,7 @@ const inspectUser = async () => {
name: true,
createdAt: true,
updatedAt: true,
riskLevel: true,
publishedTypebot: {
select: {
typebot: {
Expand Down Expand Up @@ -78,7 +80,8 @@ const inspectUser = async () => {
console.log('Workspaces:')

for (const workspace of user.workspaces) {
console.log(' - Name:', workspace.workspace.name)
console.log(' - ID:', workspace.workspace.id)
console.log(' Name:', workspace.workspace.name)
console.log(' Plan:', workspace.workspace.plan)
console.log(' Members:', workspace.workspace.members.length + 1)
console.log(
Expand All @@ -94,6 +97,7 @@ const inspectUser = async () => {
' Last updated:',
typebot.updatedAt.toLocaleDateString()
)
console.log(' Risk level:', typebot.riskLevel)
console.log(
' Public ID:',
typebot.publishedTypebot?.typebot.publicId
Expand Down
6 changes: 5 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"migrateSubscriptionsToUsageBased": "tsx migrateSubscriptionsToUsageBased.ts",
"importContactToBrevo": "tsx importContactToBrevo.ts",
"getUsage": "tsx getUsage.ts",
"suspendWorkspace": "SKIP_ENV_CHECK=true dotenv -e ./.env.production -- tsx suspendWorkspace.ts"
"suspendWorkspace": "tsx suspendWorkspace.ts",
"destroyUser": "tsx destroyUser.ts",
"updateTypebot": "tsx updateTypebot.ts",
"updateWorkspace": "tsx updateWorkspace.ts"
},
"devDependencies": {
"@typebot.io/emails": "workspace:*",
Expand All @@ -37,6 +40,7 @@
"zod": "3.22.4"
},
"dependencies": {
"@clack/prompts": "^0.7.0",
"@paralleldrive/cuid2": "2.2.1"
}
}
49 changes: 38 additions & 11 deletions packages/scripts/suspendWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { PrismaClient } from '@typebot.io/prisma'
import * as p from '@clack/prompts'
import { isEmpty } from '@typebot.io/lib'
import { promptAndSetEnvironment } from './utils'

const suspendWorkspace = async () => {
await promptAndSetEnvironment('production')
const prisma = new PrismaClient({
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'],
})
Expand All @@ -11,23 +15,46 @@ const suspendWorkspace = async () => {
console.log(e.duration, 'ms')
})

const typebot = await prisma.typebot.findUnique({
where: {
id: '',
},
select: {
workspaceId: true,
},
})
const type = (await p.select({
message: 'Select way',
options: [
{ label: 'Typebot public ID', value: 'typebotId' },
{ label: 'Workspace ID', value: 'workspaceId' },
],
})) as 'typebotId' | 'workspaceId'

const val = (await p.text({
message: 'Enter value',
})) as string

let workspaceId = type === 'workspaceId' ? val : undefined

if (!workspaceId) {
const typebot = await prisma.typebot.findUnique({
where: {
publicId: val,
},
select: {
workspaceId: true,
},
})

if (!typebot) {
console.log('Typebot not found')
return
}

workspaceId = typebot.workspaceId
}

if (!typebot) {
console.log('Typebot not found')
if (isEmpty(workspaceId)) {
console.log('Workspace not found')
return
}

const result = await prisma.workspace.update({
where: {
id: typebot.workspaceId,
id: workspaceId,
},
data: {
isSuspended: true,
Expand Down
34 changes: 34 additions & 0 deletions packages/scripts/updateTypebot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { PrismaClient } from '@typebot.io/prisma'
import { promptAndSetEnvironment } from './utils'
import * as p from '@clack/prompts'

const updateTypebot = async () => {
await promptAndSetEnvironment('production')

const prisma = new PrismaClient({
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'],
})

prisma.$on('query', (e) => {
console.log(e.query)
console.log(e.params)
console.log(e.duration, 'ms')
})

const typebotId = (await p.text({
message: 'Typebot ID?',
})) as string

const typebot = await prisma.typebot.update({
where: {
id: typebotId,
},
data: {
riskLevel: -1,
},
})

console.log(typebot)
}

updateTypebot()
34 changes: 34 additions & 0 deletions packages/scripts/updateWorkspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { PrismaClient } from '@typebot.io/prisma'
import { promptAndSetEnvironment } from './utils'
import * as p from '@clack/prompts'

const updateTypebot = async () => {
await promptAndSetEnvironment('production')

const prisma = new PrismaClient({
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'],
})

prisma.$on('query', (e) => {
console.log(e.query)
console.log(e.params)
console.log(e.duration, 'ms')
})

const workspaceId = (await p.text({
message: 'Workspace ID?',
})) as string

const workspace = await prisma.workspace.update({
where: {
id: workspaceId,
},
data: {
isVerified: true,
},
})

console.log(workspace)
}

updateTypebot()
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d6c964

Please sign in to comment.