remove-black.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Billing } from "../src/billing.js"
  2. import { and, Database, eq } from "../src/drizzle/index.js"
  3. import { BillingTable, PaymentTable, SubscriptionTable } from "../src/schema/billing.sql.js"
  4. const workspaceID = process.argv[2]
  5. if (!workspaceID) {
  6. console.error("Usage: bun remove-black.ts <workspaceID>")
  7. process.exit(1)
  8. }
  9. console.log(`Removing subscription from workspace ${workspaceID}`)
  10. // Look up the workspace billing
  11. const billing = await Database.use((tx) =>
  12. tx
  13. .select({
  14. customerID: BillingTable.customerID,
  15. subscriptionID: BillingTable.subscriptionID,
  16. })
  17. .from(BillingTable)
  18. .where(eq(BillingTable.workspaceID, workspaceID))
  19. .then((rows) => rows[0]),
  20. )
  21. if (!billing) {
  22. console.error(`Error: No billing record found for workspace ${workspaceID}`)
  23. process.exit(1)
  24. }
  25. if (!billing.subscriptionID) {
  26. console.error(`Error: Workspace ${workspaceID} does not have a subscription`)
  27. process.exit(1)
  28. }
  29. console.log(` Customer ID: ${billing.customerID}`)
  30. console.log(` Subscription ID: ${billing.subscriptionID}`)
  31. // Clear workspaceID from Stripe customer metadata
  32. if (billing.customerID) {
  33. //await Billing.stripe().customers.update(billing.customerID, {
  34. // metadata: {
  35. // workspaceID: "",
  36. // },
  37. //})
  38. //console.log(`Cleared workspaceID from Stripe customer metadata`)
  39. }
  40. await Database.transaction(async (tx) => {
  41. // Clear subscription-related fields from billing table
  42. await tx
  43. .update(BillingTable)
  44. .set({
  45. // customerID: null,
  46. subscriptionID: null,
  47. subscriptionCouponID: null,
  48. // paymentMethodID: null,
  49. // paymentMethodLast4: null,
  50. // paymentMethodType: null,
  51. })
  52. .where(eq(BillingTable.workspaceID, workspaceID))
  53. // Delete from subscription table
  54. await tx.delete(SubscriptionTable).where(eq(SubscriptionTable.workspaceID, workspaceID))
  55. // Delete from payments table
  56. await tx
  57. .delete(PaymentTable)
  58. .where(
  59. and(
  60. eq(PaymentTable.workspaceID, workspaceID),
  61. eq(PaymentTable.enrichment, { type: "subscription" }),
  62. eq(PaymentTable.amount, 20000000000),
  63. ),
  64. )
  65. })
  66. console.log(`Successfully removed subscription from workspace ${workspaceID}`)