Skip to content

Commit 1bca1eb

Browse files
committed
refactor: standardize getBillingDbClient helper across all billing files
Apply the same getBillingDbClient helper pattern from grant-credits.ts to: - balance-calculator.ts (calculateUsageThisCycle) - credit-delegation.ts (findOrganizationForRepository) - org-billing.ts (syncOrganizationBillingCycle) This centralizes the DB client casting logic and removes redundant comments.
1 parent 0af83cf commit 1bca1eb

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

packages/billing/src/balance-calculator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import type { trackEvent as trackEventFn } from '@codebuff/common/analytics'
2222
import type { reportPurchasedCreditsToStripe as reportPurchasedCreditsToStripeFn } from './stripe-metering'
2323
import type { BillingDbConnection } from '@codebuff/common/types/contracts/billing'
2424

25+
const getBillingDbClient = (
26+
dbOverride?: BillingDbConnection,
27+
): BillingDbConnection => (dbOverride ?? db) as BillingDbConnection
28+
2529
export interface CreditBalance {
2630
totalRemaining: number
2731
totalDebt: number
@@ -693,8 +697,7 @@ export async function calculateUsageThisCycle(params: {
693697
deps?: CalculateUsageThisCycleDeps
694698
}): Promise<number> {
695699
const { userId, quotaResetDate, deps = {} } = params
696-
// Cast to BillingDbConnection to allow either real db or mock to be used
697-
const dbClient = (deps.db ?? db) as BillingDbConnection
700+
const dbClient = getBillingDbClient(deps.db)
698701

699702
const usageResult = await dbClient
700703
.select({

packages/billing/src/credit-delegation.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import type { ConsumeCreditsWithFallbackFn, BillingDbConnection } from '@codebuf
1414
import type { Logger } from '@codebuff/common/types/contracts/logger'
1515
import type { ParamsOf } from '@codebuff/common/types/function-params'
1616

17+
const getBillingDbClient = (
18+
dbOverride?: BillingDbConnection,
19+
): BillingDbConnection => (dbOverride ?? db) as BillingDbConnection
20+
1721
export interface OrganizationLookupResult {
1822
found: boolean
1923
organizationId?: string
@@ -47,8 +51,7 @@ export async function findOrganizationForRepository(params: {
4751
deps?: FindOrganizationForRepositoryDeps
4852
}): Promise<OrganizationLookupResult> {
4953
const { userId, repositoryUrl, logger, deps = {} } = params
50-
// Cast to BillingDbConnection to allow either real db or mock to be used
51-
const dbClient = (deps.db ?? db) as BillingDbConnection
54+
const dbClient = getBillingDbClient(deps.db)
5255

5356
try {
5457
const normalizedUrl = normalizeRepositoryUrl(repositoryUrl)

packages/billing/src/org-billing.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import type { BillingTransactionFn, BillingDbConnection } from '@codebuff/common
1919
import type { OptionalFields } from '@codebuff/common/types/function-params'
2020
import type { GrantType } from '@codebuff/internal/db/schema'
2121

22+
const getBillingDbClient = (
23+
dbOverride?: BillingDbConnection,
24+
): BillingDbConnection => (dbOverride ?? db) as BillingDbConnection
25+
2226
// Minimal structural type that both `db` and `tx` satisfy
2327
// Note: This is intentionally defined locally rather than imported from billing.ts
2428
// because it needs to be compatible with real Drizzle types (PgTransaction, etc.)
@@ -42,8 +46,7 @@ export async function syncOrganizationBillingCycle(params: {
4246
deps?: SyncOrganizationBillingCycleDeps
4347
}): Promise<Date> {
4448
const { organizationId, logger, deps = {} } = params
45-
// Cast to BillingDbConnection to allow either real db or mock to be used
46-
const dbClient = (deps.db ?? db) as BillingDbConnection
49+
const dbClient = getBillingDbClient(deps.db)
4750
const stripe = deps.stripeServer ?? stripeServer
4851

4952
const organization = await dbClient.query.org.findFirst({

0 commit comments

Comments
 (0)