black-gift.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Billing } from "../src/billing.js"
  2. import { and, Database, eq, isNull } from "../src/drizzle/index.js"
  3. import { UserTable } from "../src/schema/user.sql.js"
  4. import { BillingTable, SubscriptionTable } from "../src/schema/billing.sql.js"
  5. import { Identifier } from "../src/identifier.js"
  6. import { AuthTable } from "../src/schema/auth.sql.js"
  7. import { BlackData } from "../src/black.js"
  8. const plan = "200"
  9. const couponID = "JAIr0Pe1"
  10. const workspaceID = process.argv[2]
  11. const seats = parseInt(process.argv[3])
  12. console.log(`Gifting ${seats} seats of Black to workspace ${workspaceID}`)
  13. if (!workspaceID || !seats) throw new Error("Usage: bun foo.ts <workspaceID> <seats>")
  14. // Get workspace user
  15. const users = await Database.use((tx) =>
  16. tx
  17. .select({
  18. id: UserTable.id,
  19. role: UserTable.role,
  20. email: AuthTable.subject,
  21. })
  22. .from(UserTable)
  23. .leftJoin(AuthTable, and(eq(AuthTable.accountID, UserTable.accountID), eq(AuthTable.provider, "email")))
  24. .where(and(eq(UserTable.workspaceID, workspaceID), isNull(UserTable.timeDeleted))),
  25. )
  26. if (users.length === 0) throw new Error(`Error: No users found in workspace ${workspaceID}`)
  27. if (users.length !== seats)
  28. throw new Error(`Error: Workspace ${workspaceID} has ${users.length} users, expected ${seats}`)
  29. const adminUser = users.find((user) => user.role === "admin")
  30. if (!adminUser) throw new Error(`Error: No admin user found in workspace ${workspaceID}`)
  31. if (!adminUser.email) throw new Error(`Error: Admin user ${adminUser.id} has no email`)
  32. // Get Billing
  33. const billing = await Database.use((tx) =>
  34. tx
  35. .select({
  36. customerID: BillingTable.customerID,
  37. subscriptionID: BillingTable.subscriptionID,
  38. })
  39. .from(BillingTable)
  40. .where(eq(BillingTable.workspaceID, workspaceID))
  41. .then((rows) => rows[0]),
  42. )
  43. if (!billing) throw new Error(`Error: Workspace ${workspaceID} has no billing record`)
  44. if (billing.subscriptionID) throw new Error(`Error: Workspace ${workspaceID} already has a subscription`)
  45. // Look up the Stripe customer by email
  46. const customerID =
  47. billing.customerID ??
  48. (await (() =>
  49. Billing.stripe()
  50. .customers.create({
  51. email: adminUser.email,
  52. metadata: {
  53. workspaceID,
  54. },
  55. })
  56. .then((customer) => customer.id))())
  57. console.log(`Customer ID: ${customerID}`)
  58. const subscription = await Billing.stripe().subscriptions.create({
  59. customer: customerID!,
  60. items: [
  61. {
  62. price: BlackData.planToPriceID({ plan }),
  63. discounts: [{ coupon: couponID }],
  64. quantity: seats,
  65. },
  66. ],
  67. metadata: {
  68. workspaceID,
  69. },
  70. })
  71. console.log(`Subscription ID: ${subscription.id}`)
  72. await Database.transaction(async (tx) => {
  73. // Set customer id, subscription id, and payment method on workspace billing
  74. await tx
  75. .update(BillingTable)
  76. .set({
  77. customerID,
  78. subscriptionID: subscription.id,
  79. subscription: { status: "subscribed", coupon: couponID, seats, plan },
  80. })
  81. .where(eq(BillingTable.workspaceID, workspaceID))
  82. // Create a row in subscription table
  83. for (const user of users) {
  84. await tx.insert(SubscriptionTable).values({
  85. workspaceID,
  86. id: Identifier.create("subscription"),
  87. userID: user.id,
  88. })
  89. }
  90. //
  91. // // Create a row in payments table
  92. // await tx.insert(PaymentTable).values({
  93. // workspaceID,
  94. // id: Identifier.create("payment"),
  95. // amount: centsToMicroCents(amountInCents),
  96. // customerID,
  97. // invoiceID,
  98. // paymentID,
  99. // enrichment: {
  100. // type: "subscription",
  101. // couponID,
  102. // },
  103. // })
  104. })
  105. console.log(`done`)