From 931540b91bf0b1bcb1658de93faecd34811a3e95 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Wed, 21 Jun 2023 10:43:42 +0200 Subject: [PATCH] :alembic: Add inspect user script --- packages/scripts/inspectUser.ts | 130 ++++++++++++++++++++++++++++++++ packages/scripts/package.json | 3 +- 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 packages/scripts/inspectUser.ts diff --git a/packages/scripts/inspectUser.ts b/packages/scripts/inspectUser.ts new file mode 100644 index 000000000..281c12f62 --- /dev/null +++ b/packages/scripts/inspectUser.ts @@ -0,0 +1,130 @@ +import { PrismaClient } from '@typebot.io/prisma' +import { promptAndSetEnvironment } from './utils' +import prompts from 'prompts' +import { isEmpty } from '@typebot.io/lib' + +const inspectUser = async () => { + await promptAndSetEnvironment('production') + const response = await prompts({ + type: 'text', + name: 'email', + message: 'User email', + }) + + if (isEmpty(response.email)) process.exit() + const prisma = new PrismaClient({ + log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'], + }) + + const user = await prisma.user.findFirst({ + where: { + email: response.email, + }, + select: { + name: true, + lastActivityAt: true, + company: true, + onboardingCategories: true, + workspaces: { + where: { + role: 'ADMIN', + }, + select: { + workspace: { + select: { + name: true, + plan: true, + members: { + where: { + user: { email: { not: response.email } }, + }, + }, + additionalChatsIndex: true, + additionalStorageIndex: true, + typebots: { + orderBy: { + updatedAt: 'desc', + }, + select: { + id: true, + name: true, + createdAt: true, + updatedAt: true, + publishedTypebot: { + select: { + typebot: { + select: { publicId: true }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }) + + if (!user) { + console.log('User not found') + process.exit() + } + + console.log('Name:', user.name) + console.log('Last activity:', user.lastActivityAt.toLocaleDateString()) + console.log('Company:', user.company) + console.log('Onboarding categories:', user.onboardingCategories) + console.log('Total workspaces:', user.workspaces.length) + console.log('Workspaces:') + + for (const workspace of user.workspaces) { + console.log(' - Name:', workspace.workspace.name) + console.log(' Plan:', workspace.workspace.plan) + console.log(' Members:', workspace.workspace.members.length + 1) + console.log( + ' Additional chats:', + workspace.workspace.additionalChatsIndex + ) + console.log( + ' Additional storage:', + workspace.workspace.additionalStorageIndex + ) + console.log(' Typebots:', workspace.workspace.typebots.length) + + for (const typebot of workspace.workspace.typebots) { + console.log(' - Name:', typebot.name) + console.log(' Created:', typebot.createdAt.toLocaleDateString()) + console.log( + ' Last updated:', + typebot.updatedAt.toLocaleDateString() + ) + console.log( + ' Public ID:', + typebot.publishedTypebot?.typebot.publicId + ) + console.log( + ' URL:', + `https://app.typebot.io/typebots/${typebot.id}/edit` + ) + + if (!typebot.publishedTypebot) continue + + const totalTraffic = await prisma.result.count({ + where: { + typebotId: typebot.id, + isArchived: false, + }, + select: { + _all: true, + hasStarted: true, + }, + }) + + console.log(' Total traffic:', totalTraffic._all) + console.log(' Started:', totalTraffic.hasStarted) + } + } +} + +inspectUser() diff --git a/packages/scripts/package.json b/packages/scripts/package.json index 2769f9971..efd706dbe 100644 --- a/packages/scripts/package.json +++ b/packages/scripts/package.json @@ -13,7 +13,8 @@ "db:bulkUpdate": "tsx bulkUpdate.ts", "db:fixTypebots": "tsx fixTypebots.ts", "telemetry:sendTotalResultsDigest": "tsx sendTotalResultsDigest.ts", - "sendAlertEmails": "tsx sendAlertEmails.ts" + "sendAlertEmails": "tsx sendAlertEmails.ts", + "inspectUser": "tsx inspectUser.ts" }, "devDependencies": { "@typebot.io/emails": "workspace:*",