Skip to content

Commit 2d9cbea

Browse files
committed
Refactor so only one ensureSubscriberBlockGrant function is injected
1 parent fba5e79 commit 2d9cbea

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

packages/billing/src/subscription.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,24 @@ export async function ensureActiveBlockGrant(params: {
422422
return result
423423
}
424424

425+
/**
426+
* Combined function that gets the active subscription and ensures a block grant exists.
427+
* Returns the block grant result if the user has an active subscription, null otherwise.
428+
*/
429+
export async function ensureSubscriberBlockGrant(params: {
430+
userId: string
431+
logger: Logger
432+
}): Promise<BlockGrantResult | null> {
433+
const { userId, logger } = params
434+
435+
const subscription = await getActiveSubscription({ userId, logger })
436+
if (!subscription) {
437+
return null
438+
}
439+
440+
return ensureActiveBlockGrant({ userId, subscription, logger })
441+
}
442+
425443
// ---------------------------------------------------------------------------
426444
// Rate limiting
427445
// ---------------------------------------------------------------------------

web/src/app/api/v1/chat/completions/_post.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import type {
1818
LoggerWithContextFn,
1919
} from '@codebuff/common/types/contracts/logger'
2020

21-
import type {
22-
BlockGrantResult,
23-
SubscriptionRow,
24-
} from '@codebuff/billing/subscription'
21+
import type { BlockGrantResult } from '@codebuff/billing/subscription'
2522
import type { NextRequest } from 'next/server'
2623

2724
import type { ChatCompletionRequestBody } from '@/llm-api/types'
@@ -83,8 +80,7 @@ export async function postChatCompletions(params: {
8380
getAgentRunFromId: GetAgentRunFromIdFn
8481
fetch: typeof globalThis.fetch
8582
insertMessageBigquery: InsertMessageBigqueryFn
86-
getActiveSubscription?: (params: { userId: string; logger: Logger }) => Promise<SubscriptionRow | null>
87-
ensureActiveBlockGrant?: (params: { userId: string; subscription: SubscriptionRow; logger: Logger }) => Promise<BlockGrantResult>
83+
ensureSubscriberBlockGrant?: (params: { userId: string; logger: Logger }) => Promise<BlockGrantResult | null>
8884
}) {
8985
const {
9086
req,
@@ -95,8 +91,7 @@ export async function postChatCompletions(params: {
9591
getAgentRunFromId,
9692
fetch,
9793
insertMessageBigquery,
98-
getActiveSubscription,
99-
ensureActiveBlockGrant,
94+
ensureSubscriberBlockGrant,
10095
} = params
10196
let { logger } = params
10297

@@ -192,13 +187,10 @@ export async function postChatCompletions(params: {
192187
})
193188

194189
// For subscribers, ensure a block grant exists before checking balance.
195-
// This is done here block grants should only start when the user begins working.
196-
if (getActiveSubscription && ensureActiveBlockGrant) {
190+
// This is done here because block grants should only start when the user begins working.
191+
if (ensureSubscriberBlockGrant) {
197192
try {
198-
const activeSub = await getActiveSubscription({ userId, logger })
199-
if (activeSub) {
200-
await ensureActiveBlockGrant({ userId, subscription: activeSub, logger })
201-
}
193+
await ensureSubscriberBlockGrant({ userId, logger })
202194
} catch (error) {
203195
logger.error(
204196
{ error: getErrorObject(error), userId },

web/src/app/api/v1/chat/completions/route.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { insertMessageBigquery } from '@codebuff/bigquery'
2-
import {
3-
ensureActiveBlockGrant,
4-
getActiveSubscription,
5-
} from '@codebuff/billing/subscription'
2+
import { ensureSubscriberBlockGrant } from '@codebuff/billing/subscription'
63
import { getUserUsageData } from '@codebuff/billing/usage-service'
74
import { trackEvent } from '@codebuff/common/analytics'
85

@@ -25,7 +22,6 @@ export async function POST(req: NextRequest) {
2522
getAgentRunFromId,
2623
fetch,
2724
insertMessageBigquery,
28-
getActiveSubscription,
29-
ensureActiveBlockGrant,
25+
ensureSubscriberBlockGrant,
3026
})
3127
}

0 commit comments

Comments
 (0)