summaryrefslogtreecommitdiffhomepage
path: root/packages/console/core/script/remove-black.ts
blob: 0803c8f8330742812fbec94355aac0ec282223c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Billing } from "../src/billing.js"
import { and, Database, eq } from "../src/drizzle/index.js"
import { BillingTable, PaymentTable, SubscriptionTable } from "../src/schema/billing.sql.js"

const workspaceID = process.argv[2]

if (!workspaceID) {
  console.error("Usage: bun remove-black.ts <workspaceID>")
  process.exit(1)
}

console.log(`Removing subscription from workspace ${workspaceID}`)

// Look up the workspace billing
const billing = await Database.use((tx) =>
  tx
    .select({
      customerID: BillingTable.customerID,
      subscriptionID: BillingTable.subscriptionID,
    })
    .from(BillingTable)
    .where(eq(BillingTable.workspaceID, workspaceID))
    .then((rows) => rows[0]),
)

if (!billing) {
  console.error(`Error: No billing record found for workspace ${workspaceID}`)
  process.exit(1)
}

if (!billing.subscriptionID) {
  console.error(`Error: Workspace ${workspaceID} does not have a subscription`)
  process.exit(1)
}

console.log(`  Customer ID: ${billing.customerID}`)
console.log(`  Subscription ID: ${billing.subscriptionID}`)

// Clear workspaceID from Stripe customer metadata
if (billing.customerID) {
  //await Billing.stripe().customers.update(billing.customerID, {
  //  metadata: {
  //    workspaceID: "",
  //  },
  //})
  //console.log(`Cleared workspaceID from Stripe customer metadata`)
}

await Database.transaction(async (tx) => {
  // Clear subscription-related fields from billing table
  await tx
    .update(BillingTable)
    .set({
      //      customerID: null,
      subscriptionID: null,
      subscriptionCouponID: null,
      //     paymentMethodID: null,
      //     paymentMethodLast4: null,
      //     paymentMethodType: null,
    })
    .where(eq(BillingTable.workspaceID, workspaceID))

  // Delete from subscription table
  await tx.delete(SubscriptionTable).where(eq(SubscriptionTable.workspaceID, workspaceID))

  // Delete from payments table
  await tx
    .delete(PaymentTable)
    .where(
      and(
        eq(PaymentTable.workspaceID, workspaceID),
        eq(PaymentTable.enrichment, { type: "subscription" }),
        eq(PaymentTable.amount, 20000000000),
      ),
    )
})

console.log(`Successfully removed subscription from workspace ${workspaceID}`)