|
1 | | -import { Database, eq } from "../src/drizzle/index.js" |
2 | | -import { AuthTable } from "../src/schema/auth.sql" |
| 1 | +import { Database, eq, sql, inArray } from "../src/drizzle/index.js" |
| 2 | +import { AuthTable } from "../src/schema/auth.sql.js" |
| 3 | +import { UserTable } from "../src/schema/user.sql.js" |
| 4 | +import { BillingTable, PaymentTable } from "../src/schema/billing.sql.js" |
| 5 | +import { WorkspaceTable } from "../src/schema/workspace.sql.js" |
3 | 6 |
|
4 | 7 | // get input from command line |
5 | | -const email = process.argv[2] |
6 | | -if (!email) { |
7 | | - console.error("Usage: bun lookup-user.ts <email>") |
| 8 | +const identifier = process.argv[2] |
| 9 | +if (!identifier) { |
| 10 | + console.error("Usage: bun lookup-user.ts <email|workspaceID>") |
8 | 11 | process.exit(1) |
9 | 12 | } |
10 | 13 |
|
11 | | -const authData = await printTable("Auth", (tx) => tx.select().from(AuthTable).where(eq(AuthTable.subject, email))) |
12 | | -if (authData.length === 0) { |
13 | | - console.error("User not found") |
14 | | - process.exit(1) |
| 14 | +if (identifier.startsWith("wrk_")) { |
| 15 | + await printWorkspace(identifier) |
| 16 | +} else { |
| 17 | + const authData = await printTable("Email", (tx) => |
| 18 | + tx.select().from(AuthTable).where(eq(AuthTable.subject, identifier)), |
| 19 | + ) |
| 20 | + if (authData.length === 0) { |
| 21 | + console.error("Email not found") |
| 22 | + process.exit(1) |
| 23 | + } |
| 24 | + if (authData.length > 1) console.warn("Multiple users found for email", identifier) |
| 25 | + |
| 26 | + // Get all auth records for email |
| 27 | + const accountID = authData[0].accountID |
| 28 | + await printTable("Auth Records", (tx) => tx.select().from(AuthTable).where(eq(AuthTable.accountID, accountID))) |
| 29 | + |
| 30 | + // Get all workspaces for this account |
| 31 | + const users = await printTable("Workspaces", (tx) => |
| 32 | + tx |
| 33 | + .select({ |
| 34 | + userID: UserTable.id, |
| 35 | + workspaceID: UserTable.workspaceID, |
| 36 | + workspaceName: WorkspaceTable.name, |
| 37 | + role: UserTable.role, |
| 38 | + }) |
| 39 | + .from(UserTable) |
| 40 | + .innerJoin(WorkspaceTable, eq(WorkspaceTable.id, UserTable.workspaceID)) |
| 41 | + .where(eq(UserTable.accountID, accountID)), |
| 42 | + ) |
| 43 | + |
| 44 | + // Get all payments for these workspaces |
| 45 | + await Promise.all(users.map((u: { workspaceID: string }) => printWorkspace(u.workspaceID))) |
| 46 | +} |
| 47 | + |
| 48 | +async function printWorkspace(workspaceID: string) { |
| 49 | + const workspace = await Database.use((tx) => |
| 50 | + tx |
| 51 | + .select() |
| 52 | + .from(WorkspaceTable) |
| 53 | + .where(eq(WorkspaceTable.id, workspaceID)) |
| 54 | + .then((rows) => rows[0]), |
| 55 | + ) |
| 56 | + |
| 57 | + printHeader(`Workspace "${workspace.name}" (${workspace.id})`) |
| 58 | + |
| 59 | + await printTable("Billing", (tx) => |
| 60 | + tx |
| 61 | + .select({ |
| 62 | + balance: BillingTable.balance, |
| 63 | + }) |
| 64 | + .from(BillingTable) |
| 65 | + .where(eq(BillingTable.workspaceID, workspace.id)) |
| 66 | + .then( |
| 67 | + (rows) => |
| 68 | + rows.map((row) => ({ |
| 69 | + ...row, |
| 70 | + balance: `$${(row.balance / 100000000).toFixed(2)}`, |
| 71 | + }))[0], |
| 72 | + ), |
| 73 | + ) |
| 74 | + |
| 75 | + await printTable("Payments", (tx) => |
| 76 | + tx |
| 77 | + .select({ |
| 78 | + amount: PaymentTable.amount, |
| 79 | + paymentID: PaymentTable.paymentID, |
| 80 | + invoiceID: PaymentTable.invoiceID, |
| 81 | + timeCreated: PaymentTable.timeCreated, |
| 82 | + timeRefunded: PaymentTable.timeRefunded, |
| 83 | + }) |
| 84 | + .from(PaymentTable) |
| 85 | + .where(eq(PaymentTable.workspaceID, workspace.id)) |
| 86 | + .orderBy(sql`${PaymentTable.timeCreated} DESC`) |
| 87 | + .limit(100) |
| 88 | + .then((rows) => |
| 89 | + rows.map((row) => ({ |
| 90 | + ...row, |
| 91 | + amount: `$${(row.amount / 100000000).toFixed(2)}`, |
| 92 | + paymentID: row.paymentID |
| 93 | + ? `https://dashboard.stripe.com/acct_1RszBH2StuRr0lbX/payments/${row.paymentID}` |
| 94 | + : null, |
| 95 | + })), |
| 96 | + ), |
| 97 | + ) |
15 | 98 | } |
16 | 99 |
|
17 | | -await printTable("Auth", (tx) => tx.select().from(AuthTable).where(eq(AuthTable.accountID, authData[0].accountID))) |
| 100 | +function printHeader(title: string) { |
| 101 | + console.log() |
| 102 | + console.log("─".repeat(title.length)) |
| 103 | + console.log(`${title}`) |
| 104 | + console.log("─".repeat(title.length)) |
| 105 | +} |
18 | 106 |
|
19 | | -function printTable(title: string, callback: (tx: Database.TxOrDb) => Promise<any[]>): Promise<any[]> { |
| 107 | +function printTable(title: string, callback: (tx: Database.TxOrDb) => Promise<any>): Promise<any> { |
20 | 108 | return Database.use(async (tx) => { |
21 | 109 | const data = await callback(tx) |
22 | | - console.log(`== ${title} ==`) |
23 | | - console.table(data) |
| 110 | + console.log(`\n== ${title} ==`) |
| 111 | + if (data.length === 0) { |
| 112 | + console.log("(no data)") |
| 113 | + } else { |
| 114 | + console.table(data) |
| 115 | + } |
24 | 116 | return data |
25 | 117 | }) |
26 | 118 | } |