11import { trackEvent } from '@codebuff/common/analytics'
22import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
3- import { PLANS } from '@codebuff/common/constants/subscription-plans'
43import db from '@codebuff/internal/db'
54import * as schema from '@codebuff/internal/db/schema'
6- import { env } from '@codebuff/internal/env'
75import { stripeServer } from '@codebuff/internal/util/stripe'
86import { eq } from 'drizzle-orm'
97
108import { handleSubscribe } from './subscription'
119
1210import type { Logger } from '@codebuff/common/types/contracts/logger'
13- import type { PlanConfig } from '@codebuff/common/constants/subscription-plans'
1411import type Stripe from 'stripe'
1512
1613type SubscriptionStatus = ( typeof schema . subscriptionStatusEnum . enumValues ) [ number ]
@@ -38,22 +35,6 @@ async function getUserIdByCustomerId(
3835 return userRecord [ 0 ] ?. id ?? null
3936}
4037
41- /**
42- * Resolves a PlanConfig from a Stripe price ID.
43- * Compares against the configured env var for each plan.
44- */
45- function getPlanFromPriceId ( priceId : string ) : PlanConfig {
46- if ( ! env . STRIPE_SUBSCRIPTION_200_PRICE_ID ) {
47- throw new Error (
48- 'STRIPE_SUBSCRIPTION_200_PRICE_ID env var is not configured' ,
49- )
50- }
51- if ( env . STRIPE_SUBSCRIPTION_200_PRICE_ID === priceId ) {
52- return PLANS . pro
53- }
54- throw new Error ( `Unknown subscription price ID: ${ priceId } ` )
55- }
56-
5738// ---------------------------------------------------------------------------
5839// invoice.paid
5940// ---------------------------------------------------------------------------
@@ -100,17 +81,6 @@ export async function handleSubscriptionInvoicePaid(params: {
10081 return
10182 }
10283
103- let plan : PlanConfig
104- try {
105- plan = getPlanFromPriceId ( priceId )
106- } catch {
107- logger . warn (
108- { subscriptionId, priceId } ,
109- 'Subscription invoice for unrecognised price — skipping' ,
110- )
111- return
112- }
113-
11484 // Look up the user for this customer
11585 const userId = await getUserIdByCustomerId ( customerId )
11686
@@ -138,7 +108,6 @@ export async function handleSubscriptionInvoicePaid(params: {
138108 stripe_customer_id : customerId ,
139109 user_id : userId ,
140110 stripe_price_id : priceId ,
141- plan_name : plan . name ,
142111 status : 'active' ,
143112 billing_period_start : new Date ( stripeSub . current_period_start * 1000 ) ,
144113 billing_period_end : new Date ( stripeSub . current_period_end * 1000 ) ,
@@ -150,7 +119,6 @@ export async function handleSubscriptionInvoicePaid(params: {
150119 status : 'active' ,
151120 ...( userId ? { user_id : userId } : { } ) ,
152121 stripe_price_id : priceId ,
153- plan_name : plan . name ,
154122 billing_period_start : new Date (
155123 stripeSub . current_period_start * 1000 ,
156124 ) ,
@@ -164,7 +132,6 @@ export async function handleSubscriptionInvoicePaid(params: {
164132 {
165133 subscriptionId,
166134 customerId,
167- planName : plan . name ,
168135 billingReason : invoice . billing_reason ,
169136 } ,
170137 'Processed subscription invoice.paid' ,
@@ -243,18 +210,6 @@ export async function handleSubscriptionUpdated(params: {
243210 return
244211 }
245212
246- let planName : string
247- try {
248- const plan = getPlanFromPriceId ( priceId )
249- planName = plan . name
250- } catch {
251- logger . warn (
252- { subscriptionId, priceId } ,
253- 'Subscription updated with unrecognised price — skipping' ,
254- )
255- return
256- }
257-
258213 const customerId =
259214 typeof stripeSubscription . customer === 'string'
260215 ? stripeSubscription . customer
@@ -272,7 +227,6 @@ export async function handleSubscriptionUpdated(params: {
272227 stripe_customer_id : customerId ,
273228 user_id : userId ,
274229 stripe_price_id : priceId ,
275- plan_name : planName ,
276230 status,
277231 cancel_at_period_end : stripeSubscription . cancel_at_period_end ,
278232 billing_period_start : new Date (
@@ -287,7 +241,6 @@ export async function handleSubscriptionUpdated(params: {
287241 set : {
288242 ...( userId ? { user_id : userId } : { } ) ,
289243 stripe_price_id : priceId ,
290- plan_name : planName ,
291244 status,
292245 cancel_at_period_end : stripeSubscription . cancel_at_period_end ,
293246 billing_period_start : new Date (
@@ -303,7 +256,6 @@ export async function handleSubscriptionUpdated(params: {
303256 logger . info (
304257 {
305258 subscriptionId,
306- planName,
307259 cancelAtPeriodEnd : stripeSubscription . cancel_at_period_end ,
308260 } ,
309261 'Processed subscription update' ,
0 commit comments