1- import { TEST_USER_ID } from '@codebuff/common/old-constants'
21import { withRetry, withTimeout } from '@codebuff/common/util/promise'
32import db from '@codebuff/internal/db'
43import * as schema from '@codebuff/internal/db/schema'
@@ -9,6 +8,8 @@ import type { Logger } from '@codebuff/common/types/contracts/logger'
98
109const STRIPE_METER_EVENT_NAME = 'credits'
1110const STRIPE_METER_REQUEST_TIMEOUT_MS = 10_000
11+ const STRIPE_METERING_SKIP_USER_IDS_ENV_VAR =
12+ 'CODEBUFF_STRIPE_METERING_SKIP_USER_IDS'
1213
1314function shouldAttemptStripeMetering(): boolean {
1415 // Avoid sending Stripe metering events in CI/tests, and when Stripe isn't configured.
@@ -17,6 +18,16 @@ function shouldAttemptStripeMetering(): boolean {
1718 return Boolean(process.env.STRIPE_SECRET_KEY)
1819}
1920
21+ function shouldSkipStripeMeteringForUser(userId: string): boolean {
22+ const rawIds = process.env[STRIPE_METERING_SKIP_USER_IDS_ENV_VAR]
23+ if (!rawIds) return false
24+ return rawIds
25+ .split(',')
26+ .map((value) => value.trim())
27+ .filter(Boolean)
28+ .includes(userId)
29+ }
30+
2031export async function reportPurchasedCreditsToStripe(params: {
2132 userId: string
2233 stripeCustomerId?: string | null
@@ -48,7 +59,7 @@ export async function reportPurchasedCreditsToStripe(params: {
4859 } = params
4960
5061 if (purchasedCredits <= 0) return
51- if (userId === TEST_USER_ID ) return
62+ if (shouldSkipStripeMeteringForUser( userId) ) return
5263 if (!shouldAttemptStripeMetering()) return
5364
5465 const logContext = { userId, purchasedCredits, eventId }
0 commit comments