Skip to content

Commit 2eb1bd9

Browse files
committed
fix: replace typeof db with BillingDbConnection in all billing dependency interfaces
Addresses Gemini CLI review feedback: - balance-calculator.ts: CalculateUsageThisCycleDeps now uses BillingDbConnection - credit-delegation.ts: FindOrganizationForRepositoryDeps now uses BillingDbConnection - grant-credits.ts: GetPreviousFreeGrantAmountDeps and CalculateTotalReferralBonusDeps now use BillingDbConnection - org-billing.ts: SyncOrganizationBillingCycleDeps now uses BillingDbConnection Added type casts to resolve union type incompatibilities between real Drizzle db and mock BillingDbConnection interface.
1 parent c28dcd1 commit 2eb1bd9

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

packages/billing/src/balance-calculator.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { GrantType } from '@codebuff/internal/db/schema'
2020
import type { withSerializableTransaction as withSerializableTransactionFn } from '@codebuff/internal/db/transaction'
2121
import type { trackEvent as trackEventFn } from '@codebuff/common/analytics'
2222
import type { reportPurchasedCreditsToStripe as reportPurchasedCreditsToStripeFn } from './stripe-metering'
23+
import type { BillingDbConnection } from '@codebuff/common/types/contracts/billing'
2324

2425
export interface CreditBalance {
2526
totalRemaining: number
@@ -48,7 +49,7 @@ type DbConn = Pick<typeof db, 'select' | 'update'>
4849
* Dependencies for calculateUsageThisCycle (for testing)
4950
*/
5051
export interface CalculateUsageThisCycleDeps {
51-
db?: typeof db
52+
db?: BillingDbConnection
5253
}
5354

5455
/**
@@ -692,7 +693,8 @@ export async function calculateUsageThisCycle(params: {
692693
deps?: CalculateUsageThisCycleDeps
693694
}): Promise<number> {
694695
const { userId, quotaResetDate, deps = {} } = params
695-
const dbClient = deps.db ?? db
696+
// Cast to BillingDbConnection to allow either real db or mock to be used
697+
const dbClient = (deps.db ?? db) as BillingDbConnection
696698

697699
const usageResult = await dbClient
698700
.select({
@@ -713,7 +715,7 @@ export async function calculateUsageThisCycle(params: {
713715
),
714716
),
715717
),
716-
)
718+
) as unknown as { totalUsed: number }[]
717719

718720
return usageResult[0].totalUsed
719721
}

packages/billing/src/credit-delegation.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
extractOwnerAndRepo,
1111
} from './org-billing'
1212

13-
import type { ConsumeCreditsWithFallbackFn } from '@codebuff/common/types/contracts/billing'
13+
import type { ConsumeCreditsWithFallbackFn, BillingDbConnection } from '@codebuff/common/types/contracts/billing'
1414
import type { Logger } from '@codebuff/common/types/contracts/logger'
1515
import type { ParamsOf } from '@codebuff/common/types/function-params'
1616

@@ -33,7 +33,7 @@ export interface CreditDelegationResult {
3333
* Dependencies for findOrganizationForRepository (for testing)
3434
*/
3535
export interface FindOrganizationForRepositoryDeps {
36-
db?: typeof db
36+
db?: BillingDbConnection
3737
}
3838

3939
/**
@@ -47,7 +47,8 @@ export async function findOrganizationForRepository(params: {
4747
deps?: FindOrganizationForRepositoryDeps
4848
}): Promise<OrganizationLookupResult> {
4949
const { userId, repositoryUrl, logger, deps = {} } = params
50-
const dbClient = deps.db ?? db
50+
// Cast to BillingDbConnection to allow either real db or mock to be used
51+
const dbClient = (deps.db ?? db) as BillingDbConnection
5152

5253
try {
5354
const normalizedUrl = normalizeRepositoryUrl(repositoryUrl)
@@ -70,7 +71,7 @@ export async function findOrganizationForRepository(params: {
7071
})
7172
.from(schema.orgMember)
7273
.innerJoin(schema.org, eq(schema.orgMember.org_id, schema.org.id))
73-
.where(eq(schema.orgMember.user_id, userId))
74+
.where(eq(schema.orgMember.user_id, userId)) as unknown as { orgId: string; orgName: string; orgSlug: string }[]
7475

7576
if (userOrganizations.length === 0) {
7677
logger.debug(
@@ -94,7 +95,7 @@ export async function findOrganizationForRepository(params: {
9495
eq(schema.orgRepo.org_id, userOrg.orgId),
9596
eq(schema.orgRepo.is_active, true),
9697
),
97-
)
98+
) as unknown as { repoUrl: string; repoName: string; isActive: boolean }[]
9899

99100
// Check if any repository in this organization matches
100101
for (const orgRepo of orgRepos) {

packages/billing/src/grant-credits.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { Logger } from '@codebuff/common/types/contracts/logger'
1515
import type {
1616
TriggerMonthlyResetAndGrantDeps,
1717
BillingTransactionFn,
18+
BillingDbConnection,
1819
} from '@codebuff/common/types/contracts/billing'
1920
import type { GrantType } from '@codebuff/internal/db/schema'
2021

@@ -24,7 +25,7 @@ import type { CodebuffTransaction } from '@codebuff/internal/db'
2425
* Dependencies for getPreviousFreeGrantAmount (for testing)
2526
*/
2627
export interface GetPreviousFreeGrantAmountDeps {
27-
db?: typeof db
28+
db?: BillingDbConnection
2829
}
2930

3031
/**
@@ -37,7 +38,8 @@ export async function getPreviousFreeGrantAmount(params: {
3738
deps?: GetPreviousFreeGrantAmountDeps
3839
}): Promise<number> {
3940
const { userId, logger, deps = {} } = params
40-
const dbClient = deps.db ?? db
41+
// Cast to BillingDbConnection to allow either real db or mock to be used
42+
const dbClient = (deps.db ?? db) as BillingDbConnection
4143

4244
const now = new Date()
4345
const lastExpiredFreeGrant = await dbClient
@@ -53,7 +55,7 @@ export async function getPreviousFreeGrantAmount(params: {
5355
),
5456
)
5557
.orderBy(desc(schema.creditLedger.expires_at)) // Most recent expiry first
56-
.limit(1)
58+
.limit(1) as unknown as { principal: number }[]
5759

5860
if (lastExpiredFreeGrant.length > 0) {
5961
// TODO: remove this once it's past May 22nd, after all users have been migrated over
@@ -76,7 +78,7 @@ export async function getPreviousFreeGrantAmount(params: {
7678
* Dependencies for calculateTotalReferralBonus (for testing)
7779
*/
7880
export interface CalculateTotalReferralBonusDeps {
79-
db?: typeof db
81+
db?: BillingDbConnection
8082
}
8183

8284
/**
@@ -91,7 +93,8 @@ export async function calculateTotalReferralBonus(params: {
9193
deps?: CalculateTotalReferralBonusDeps
9294
}): Promise<number> {
9395
const { userId, logger, deps = {} } = params
94-
const dbClient = deps.db ?? db
96+
// Cast to BillingDbConnection to allow either real db or mock to be used
97+
const dbClient = (deps.db ?? db) as BillingDbConnection
9598

9699
try {
97100
const result = await dbClient
@@ -104,7 +107,7 @@ export async function calculateTotalReferralBonus(params: {
104107
eq(schema.referral.referrer_id, userId),
105108
eq(schema.referral.referred_id, userId),
106109
),
107-
)
110+
) as unknown as { totalCredits: string }[]
108111

109112
const totalBonus = parseInt(result[0]?.totalCredits ?? '0')
110113
logger.debug({ userId, totalBonus }, 'Calculated total referral bonus.')

packages/billing/src/org-billing.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
CreditConsumptionResult,
1616
} from './balance-calculator'
1717
import type { Logger } from '@codebuff/common/types/contracts/logger'
18-
import type { BillingTransactionFn } from '@codebuff/common/types/contracts/billing'
18+
import type { BillingTransactionFn, BillingDbConnection } from '@codebuff/common/types/contracts/billing'
1919
import type { OptionalFields } from '@codebuff/common/types/function-params'
2020
import type { GrantType } from '@codebuff/internal/db/schema'
2121

@@ -28,7 +28,7 @@ type DbConn = Pick<typeof db, 'select' | 'update'>
2828
* Dependencies for syncOrganizationBillingCycle (for testing)
2929
*/
3030
export interface SyncOrganizationBillingCycleDeps {
31-
db?: typeof db
31+
db?: BillingDbConnection
3232
stripeServer?: typeof stripeServer
3333
}
3434

@@ -42,7 +42,8 @@ export async function syncOrganizationBillingCycle(params: {
4242
deps?: SyncOrganizationBillingCycleDeps
4343
}): Promise<Date> {
4444
const { organizationId, logger, deps = {} } = params
45-
const dbClient = deps.db ?? db
45+
// Cast to BillingDbConnection to allow either real db or mock to be used
46+
const dbClient = (deps.db ?? db) as BillingDbConnection
4647
const stripe = deps.stripeServer ?? stripeServer
4748

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

0 commit comments

Comments
 (0)