@@ -2,14 +2,17 @@ import { trackEvent } from '@codebuff/common/analytics'
22import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
33import db from '@codebuff/internal/db'
44import * as schema from '@codebuff/internal/db/schema'
5+ import { env } from '@codebuff/internal/env'
56import {
7+ getStripeId ,
68 getUserByStripeCustomerId ,
79 stripeServer ,
810} from '@codebuff/internal/util/stripe'
911import { eq } from 'drizzle-orm'
1012
1113import { handleSubscribe } from './subscription'
1214
15+ import type { SubscriptionTierPrice } from '@codebuff/common/constants/subscription-plans'
1316import type { Logger } from '@codebuff/common/types/contracts/logger'
1417import type Stripe from 'stripe'
1518
@@ -24,6 +27,16 @@ function mapStripeStatus(status: Stripe.Subscription.Status): SubscriptionStatus
2427 return 'incomplete'
2528}
2629
30+ const priceToTier : Record < string , SubscriptionTierPrice > = {
31+ ...( env . STRIPE_SUBSCRIPTION_100_PRICE_ID && { [ env . STRIPE_SUBSCRIPTION_100_PRICE_ID ] : 100 as const } ) ,
32+ ...( env . STRIPE_SUBSCRIPTION_200_PRICE_ID && { [ env . STRIPE_SUBSCRIPTION_200_PRICE_ID ] : 200 as const } ) ,
33+ ...( env . STRIPE_SUBSCRIPTION_500_PRICE_ID && { [ env . STRIPE_SUBSCRIPTION_500_PRICE_ID ] : 500 as const } ) ,
34+ }
35+
36+ function getTierFromPriceId ( priceId : string ) : SubscriptionTierPrice | null {
37+ return priceToTier [ priceId ] ?? null
38+ }
39+
2740// ---------------------------------------------------------------------------
2841// invoice.paid
2942// ---------------------------------------------------------------------------
@@ -43,14 +56,8 @@ export async function handleSubscriptionInvoicePaid(params: {
4356 const { invoice, logger } = params
4457
4558 if ( ! invoice . subscription ) return
46- const subscriptionId =
47- typeof invoice . subscription === 'string'
48- ? invoice . subscription
49- : invoice . subscription . id
50- const customerId =
51- typeof invoice . customer === 'string'
52- ? invoice . customer
53- : invoice . customer ?. id
59+ const subscriptionId = getStripeId ( invoice . subscription )
60+ const customerId = getStripeId ( invoice . customer )
5461
5562 if ( ! customerId ) {
5663 logger . warn (
@@ -97,6 +104,7 @@ export async function handleSubscriptionInvoicePaid(params: {
97104 stripe_customer_id : customerId ,
98105 user_id : userId ,
99106 stripe_price_id : priceId ,
107+ tier : getTierFromPriceId ( priceId ) ,
100108 status : 'active' ,
101109 billing_period_start : new Date ( stripeSub . current_period_start * 1000 ) ,
102110 billing_period_end : new Date ( stripeSub . current_period_end * 1000 ) ,
@@ -108,6 +116,7 @@ export async function handleSubscriptionInvoicePaid(params: {
108116 status : 'active' ,
109117 ...( userId ? { user_id : userId } : { } ) ,
110118 stripe_price_id : priceId ,
119+ tier : getTierFromPriceId ( priceId ) ,
111120 billing_period_start : new Date (
112121 stripeSub . current_period_start * 1000 ,
113122 ) ,
@@ -142,15 +151,8 @@ export async function handleSubscriptionInvoicePaymentFailed(params: {
142151 const { invoice, logger } = params
143152
144153 if ( ! invoice . subscription ) return
145- const subscriptionId =
146- typeof invoice . subscription === 'string'
147- ? invoice . subscription
148- : invoice . subscription . id
149-
150- const customerId =
151- typeof invoice . customer === 'string'
152- ? invoice . customer
153- : invoice . customer ?. id
154+ const subscriptionId = getStripeId ( invoice . subscription )
155+ const customerId = getStripeId ( invoice . customer )
154156 const userId = customerId
155157 ? ( await getUserByStripeCustomerId ( customerId ) ) ?. id ?? null
156158 : null
@@ -199,10 +201,7 @@ export async function handleSubscriptionUpdated(params: {
199201 return
200202 }
201203
202- const customerId =
203- typeof stripeSubscription . customer === 'string'
204- ? stripeSubscription . customer
205- : stripeSubscription . customer . id
204+ const customerId = getStripeId ( stripeSubscription . customer )
206205 const userId = ( await getUserByStripeCustomerId ( customerId ) ) ?. id ?? null
207206
208207 const status = mapStripeStatus ( stripeSubscription . status )
@@ -216,6 +215,7 @@ export async function handleSubscriptionUpdated(params: {
216215 stripe_customer_id : customerId ,
217216 user_id : userId ,
218217 stripe_price_id : priceId ,
218+ tier : getTierFromPriceId ( priceId ) ,
219219 status,
220220 cancel_at_period_end : stripeSubscription . cancel_at_period_end ,
221221 billing_period_start : new Date (
@@ -230,6 +230,7 @@ export async function handleSubscriptionUpdated(params: {
230230 set : {
231231 ...( userId ? { user_id : userId } : { } ) ,
232232 stripe_price_id : priceId ,
233+ tier : getTierFromPriceId ( priceId ) ,
233234 status,
234235 cancel_at_period_end : stripeSubscription . cancel_at_period_end ,
235236 billing_period_start : new Date (
@@ -265,10 +266,7 @@ export async function handleSubscriptionDeleted(params: {
265266 const { stripeSubscription, logger } = params
266267 const subscriptionId = stripeSubscription . id
267268
268- const customerId =
269- typeof stripeSubscription . customer === 'string'
270- ? stripeSubscription . customer
271- : stripeSubscription . customer . id
269+ const customerId = getStripeId ( stripeSubscription . customer )
272270 const userId = ( await getUserByStripeCustomerId ( customerId ) ) ?. id ?? null
273271
274272 await db
0 commit comments