black-gift.ts 3.5 KB

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