Skip to content

Commit 66463e9

Browse files
committed
Extract getUserByStripeCustomerId helper
1 parent 8e31469 commit 66463e9

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

packages/billing/src/subscription-webhooks.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { trackEvent } from '@codebuff/common/analytics'
22
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
33
import db from '@codebuff/internal/db'
44
import * as schema from '@codebuff/internal/db/schema'
5-
import { stripeServer } from '@codebuff/internal/util/stripe'
5+
import {
6+
getUserByStripeCustomerId,
7+
stripeServer,
8+
} from '@codebuff/internal/util/stripe'
69
import { eq } from 'drizzle-orm'
710

811
import { handleSubscribe } from './subscription'
@@ -21,20 +24,6 @@ function mapStripeStatus(status: Stripe.Subscription.Status): SubscriptionStatus
2124
return 'active'
2225
}
2326

24-
/**
25-
* Looks up a user ID by Stripe customer ID.
26-
*/
27-
async function getUserIdByCustomerId(
28-
customerId: string,
29-
): Promise<string | null> {
30-
const userRecord = await db
31-
.select({ id: schema.user.id })
32-
.from(schema.user)
33-
.where(eq(schema.user.stripe_customer_id, customerId))
34-
.limit(1)
35-
return userRecord[0]?.id ?? null
36-
}
37-
3827
// ---------------------------------------------------------------------------
3928
// invoice.paid
4029
// ---------------------------------------------------------------------------
@@ -82,7 +71,7 @@ export async function handleSubscriptionInvoicePaid(params: {
8271
}
8372

8473
// Look up the user for this customer
85-
const userId = await getUserIdByCustomerId(customerId)
74+
const userId = (await getUserByStripeCustomerId(customerId))?.id ?? null
8675

8776
// On first invoice, migrate renewal date & credits (Option B)
8877
if (invoice.billing_reason === 'subscription_create') {
@@ -163,7 +152,7 @@ export async function handleSubscriptionInvoicePaymentFailed(params: {
163152
? invoice.customer
164153
: invoice.customer?.id
165154
const userId = customerId
166-
? await getUserIdByCustomerId(customerId)
155+
? (await getUserByStripeCustomerId(customerId))?.id ?? null
167156
: null
168157

169158
await db
@@ -214,7 +203,7 @@ export async function handleSubscriptionUpdated(params: {
214203
typeof stripeSubscription.customer === 'string'
215204
? stripeSubscription.customer
216205
: stripeSubscription.customer.id
217-
const userId = await getUserIdByCustomerId(customerId)
206+
const userId = (await getUserByStripeCustomerId(customerId))?.id ?? null
218207

219208
const status = mapStripeStatus(stripeSubscription.status)
220209

@@ -280,7 +269,7 @@ export async function handleSubscriptionDeleted(params: {
280269
typeof stripeSubscription.customer === 'string'
281270
? stripeSubscription.customer
282271
: stripeSubscription.customer.id
283-
const userId = await getUserIdByCustomerId(customerId)
272+
const userId = (await getUserByStripeCustomerId(customerId))?.id ?? null
284273

285274
await db
286275
.update(schema.subscription)

packages/internal/src/util/stripe.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import Stripe from 'stripe'
2-
1+
import db from '@codebuff/internal/db'
2+
import * as schema from '@codebuff/internal/db/schema'
33
import { env } from '@codebuff/internal/env'
4+
import { eq } from 'drizzle-orm'
5+
import Stripe from 'stripe'
46

57
export const stripeServer = new Stripe(env.STRIPE_SECRET_KEY, {
68
apiVersion: '2024-06-20',
@@ -15,3 +17,28 @@ export async function getCurrentSubscription(customerId: string) {
1517
})
1618
return subscriptions.data[0]
1719
}
20+
21+
/**
22+
* Look up a user by their Stripe customer ID.
23+
*/
24+
export async function getUserByStripeCustomerId(
25+
stripeCustomerId: string,
26+
): Promise<{
27+
id: string
28+
banned: boolean
29+
email: string
30+
name: string | null
31+
} | null> {
32+
const users = await db
33+
.select({
34+
id: schema.user.id,
35+
banned: schema.user.banned,
36+
email: schema.user.email,
37+
name: schema.user.name,
38+
})
39+
.from(schema.user)
40+
.where(eq(schema.user.stripe_customer_id, stripeCustomerId))
41+
.limit(1)
42+
43+
return users[0] ?? null
44+
}

web/src/lib/ban-conditions.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { eq } from 'drizzle-orm'
55

66
import type { Logger } from '@codebuff/common/types/contracts/logger'
77

8+
export { getUserByStripeCustomerId } from '@codebuff/internal/util/stripe'
9+
810
// =============================================================================
911
// CONFIGURATION - Edit these values to adjust ban thresholds
1012
// =============================================================================
@@ -102,31 +104,6 @@ const BAN_CONDITIONS: BanCondition[] = [
102104
// PUBLIC API
103105
// =============================================================================
104106

105-
/**
106-
* Look up a user by their Stripe customer ID
107-
*/
108-
export async function getUserByStripeCustomerId(
109-
stripeCustomerId: string,
110-
): Promise<{
111-
id: string
112-
banned: boolean
113-
email: string
114-
name: string | null
115-
} | null> {
116-
const users = await db
117-
.select({
118-
id: schema.user.id,
119-
banned: schema.user.banned,
120-
email: schema.user.email,
121-
name: schema.user.name,
122-
})
123-
.from(schema.user)
124-
.where(eq(schema.user.stripe_customer_id, stripeCustomerId))
125-
.limit(1)
126-
127-
return users[0] ?? null
128-
}
129-
130107
/**
131108
* Ban a user and log the action
132109
*/

0 commit comments

Comments
 (0)