11import { trackEvent } from '@codebuff/common/analytics'
22import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
3+ import { createSubscriptionPriceMappings } from '@codebuff/common/constants/subscription-plans'
34import db from '@codebuff/internal/db'
45import * as schema from '@codebuff/internal/db/schema'
56import { env } from '@codebuff/internal/env'
@@ -12,7 +13,6 @@ import { eq } from 'drizzle-orm'
1213
1314import { expireActiveBlockGrants , handleSubscribe } from './subscription'
1415
15- import type { SubscriptionTierPrice } from '@codebuff/common/constants/subscription-plans'
1616import type { Logger } from '@codebuff/common/types/contracts/logger'
1717import type Stripe from 'stripe'
1818
@@ -27,23 +27,11 @@ function mapStripeStatus(status: Stripe.Subscription.Status): SubscriptionStatus
2727 return 'incomplete'
2828}
2929
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-
40- const tierToPrice = Object . fromEntries (
41- Object . entries ( priceToTier ) . map ( ( [ priceId , tier ] ) => [ tier , priceId ] ) ,
42- ) as Partial < Record < SubscriptionTierPrice , string > >
43-
44- export function getTierPriceId ( tier : SubscriptionTierPrice ) : string | null {
45- return tierToPrice [ tier ] ?? null
46- }
30+ export const { getTierFromPriceId, getPriceIdFromTier } = createSubscriptionPriceMappings ( {
31+ 100 : env . STRIPE_SUBSCRIPTION_100_PRICE_ID ,
32+ 200 : env . STRIPE_SUBSCRIPTION_200_PRICE_ID ,
33+ 500 : env . STRIPE_SUBSCRIPTION_500_PRICE_ID ,
34+ } )
4735
4836// ---------------------------------------------------------------------------
4937// invoice.paid
0 commit comments