Skip to content

Commit a2d40d2

Browse files
committed
feat: add DI support and tests for remaining billing functions
Auto-topup functions: - validateAutoTopupStatus: DI for db and stripeServer - checkAndTriggerAutoTopup: DI for db, stripe, calculateUsageAndBalance, etc. - checkAndTriggerOrgAutoTopup: DI for db, stripe, org billing functions Stripe metering: - reportPurchasedCreditsToStripe: DI for db, stripe, shouldAttemptStripeMetering Grant credits helpers: - getPreviousFreeGrantAmount: DI for db - calculateTotalReferralBonus: DI for db - processAndGrantCredit: DI for grantCreditFn and logSyncFailure New test files: - auto-topup.test.ts: 19 tests for auto-topup DI - stripe-metering.test.ts: 12 tests for stripe metering DI - Updated grant-credits.test.ts with helper function tests Type improvements: - Added BillingOrganization type to contracts/billing.ts - Added org query to BillingDbConnection interface - Added org support to mock-db.ts
1 parent b02392f commit a2d40d2

File tree

8 files changed

+1589
-53
lines changed

8 files changed

+1589
-53
lines changed

common/src/testing/mock-db.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import type { GrantType } from '../types/grant'
2222
import type {
2323
BillingDbConnection,
24+
BillingOrganization,
2425
BillingUser,
2526
CreditGrant,
2627
FindFirstParams,
@@ -425,6 +426,7 @@ export function createMockDb(config: MockDbConfig = {}): BillingDbConnection {
425426
query: {
426427
user: createTableQuery<BillingUser>(users as BillingUser[]),
427428
creditLedger: createTableQuery<CreditGrant>(creditGrants as CreditGrant[]),
429+
org: createTableQuery<BillingOrganization>(organizations as BillingOrganization[]),
428430
},
429431
}
430432
}

common/src/types/contracts/billing.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ export type Referral = {
4343
credits: number
4444
}
4545

46+
/**
47+
* Organization record fields relevant to billing
48+
*/
49+
export type BillingOrganization = {
50+
id: string
51+
auto_topup_enabled: boolean | null
52+
auto_topup_threshold: number | null
53+
auto_topup_amount: number | null
54+
stripe_customer_id: string | null
55+
current_period_start: Date | null
56+
current_period_end: Date | null
57+
}
58+
4659
// ============================================================================
4760
// Query Builder Types for Type-Safe Database Operations
4861
// ============================================================================
@@ -208,6 +221,8 @@ export type BillingDbConnection = {
208221
user: TableQuery<BillingUser>
209222
/** Query the creditLedger table */
210223
creditLedger: TableQuery<CreditGrant>
224+
/** Query the org table (for organization billing) */
225+
org: TableQuery<BillingOrganization>
211226
}
212227
}
213228

0 commit comments

Comments
 (0)