Skip to content

Commit 1361894

Browse files
committed
pass in logger to org-billing
1 parent 24c3e41 commit 1361894

File tree

22 files changed

+248
-143
lines changed

22 files changed

+248
-143
lines changed

backend/src/api/usage.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ async function usageHandler(
7171
// If orgId is provided, return organization usage data
7272
if (orgId) {
7373
try {
74-
const orgUsageResponse = await getOrganizationUsageResponse(
75-
orgId,
74+
const orgUsageResponse = await getOrganizationUsageResponse({
75+
organizationId: orgId,
7676
userId,
77-
)
77+
logger,
78+
})
7879
return res.status(200).json(orgUsageResponse)
7980
} catch (error) {
8081
logger.error(

backend/src/llm-apis/message-cost-tracker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,11 @@ async function updateUserCycleUsage(
517517
if (orgId) {
518518
// TODO: use `consumeCreditsWithFallback` to handle organization delegation
519519
// Consume from organization credits
520-
const result = await consumeOrganizationCredits(orgId, creditsUsed)
520+
const result = await consumeOrganizationCredits({
521+
organizationId: orgId,
522+
creditsToConsume: creditsUsed,
523+
logger,
524+
})
521525

522526
if (VERBOSE) {
523527
logger.debug(

backend/src/websockets/middleware.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ protec.use(async (action, clientSessionId, ws, userInfo) => {
225225
// Using a far past date ensures all grants are considered for current balance.
226226
const orgQuotaResetDate = new Date(0)
227227
const { balance: orgBalance } =
228-
await calculateOrganizationUsageAndBalance(
229-
orgLookup.organizationId,
230-
orgQuotaResetDate,
228+
await calculateOrganizationUsageAndBalance({
229+
organizationId: orgLookup.organizationId,
230+
quotaResetDate: orgQuotaResetDate,
231231
now,
232-
)
232+
logger,
233+
})
233234

234235
if (orgBalance.totalRemaining <= 0) {
235236
const orgName = orgLookup.organizationName || 'Your organization'

packages/billing/src/__tests__/org-billing.test.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
validateAndNormalizeRepositoryUrl,
1313
} from '../org-billing'
1414

15+
import type { Logger } from '@codebuff/types/logger'
16+
1517
// Mock the database
1618
const mockGrants = [
1719
{
@@ -40,6 +42,13 @@ const mockGrants = [
4042
},
4143
]
4244

45+
const logger: Logger = {
46+
debug: () => {},
47+
error: () => {},
48+
info: () => {},
49+
warn: () => {},
50+
}
51+
4352
describe('Organization Billing', () => {
4453
beforeAll(() => {
4554
mockModule('@codebuff/common/db', () => ({
@@ -91,11 +100,12 @@ describe('Organization Billing', () => {
91100
const quotaResetDate = new Date('2024-01-01')
92101
const now = new Date('2024-06-01')
93102

94-
const result = await calculateOrganizationUsageAndBalance(
103+
const result = await calculateOrganizationUsageAndBalance({
95104
organizationId,
96105
quotaResetDate,
97106
now,
98-
)
107+
logger,
108+
})
99109

100110
// Total positive balance: 800
101111
// Total debt: 100
@@ -126,11 +136,12 @@ describe('Organization Billing', () => {
126136
const quotaResetDate = new Date('2024-01-01')
127137
const now = new Date('2024-06-01')
128138

129-
const result = await calculateOrganizationUsageAndBalance(
139+
const result = await calculateOrganizationUsageAndBalance({
130140
organizationId,
131141
quotaResetDate,
132142
now,
133-
)
143+
logger,
144+
})
134145

135146
expect(result.balance.totalRemaining).toBe(0)
136147
expect(result.balance.totalDebt).toBe(0)
@@ -211,10 +222,11 @@ describe('Organization Billing', () => {
211222
const organizationId = 'org-123'
212223
const creditsToConsume = 100
213224

214-
const result = await consumeOrganizationCredits(
225+
const result = await consumeOrganizationCredits({
215226
organizationId,
216227
creditsToConsume,
217-
)
228+
logger,
229+
})
218230

219231
expect(result.consumed).toBe(100)
220232
expect(result.fromPurchased).toBe(0) // Organization credits are not "purchased" type
@@ -231,13 +243,14 @@ describe('Organization Billing', () => {
231243

232244
// Should not throw
233245
await expect(
234-
grantOrganizationCredits(
246+
grantOrganizationCredits({
235247
organizationId,
236248
userId,
237249
amount,
238250
operationId,
239251
description,
240-
),
252+
logger,
253+
}),
241254
).resolves.toBeUndefined()
242255
})
243256

@@ -264,13 +277,14 @@ describe('Organization Billing', () => {
264277

265278
// Should not throw, should handle gracefully
266279
await expect(
267-
grantOrganizationCredits(
280+
grantOrganizationCredits({
268281
organizationId,
269282
userId,
270283
amount,
271284
operationId,
272285
description,
273-
),
286+
logger,
287+
}),
274288
).resolves.toBeUndefined()
275289
})
276290
})

packages/billing/src/auto-topup.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,13 @@ async function processOrgAutoTopupPayment(params: {
486486
throw new AutoTopupPaymentError('Payment failed or requires action')
487487
}
488488

489-
await grantOrganizationCredits(
490-
organizationId,
491-
userId,
492-
amountToTopUp,
489+
await grantOrganizationCredits({
490+
...params,
491+
amount: amountToTopUp,
493492
operationId,
494-
`Organization auto top-up of ${amountToTopUp.toLocaleString()} credits`,
495-
null,
496-
)
493+
description: `Organization auto top-up of ${amountToTopUp.toLocaleString()} credits`,
494+
expiresAt: null,
495+
})
497496

498497
logger.info(
499498
{
@@ -521,10 +520,10 @@ export async function checkAndTriggerOrgAutoTopup(params: {
521520
return
522521
}
523522

524-
const { balance } = await calculateOrganizationUsageAndBalance(
525-
organizationId,
526-
getNextQuotaReset(null),
527-
)
523+
const { balance } = await calculateOrganizationUsageAndBalance({
524+
...params,
525+
quotaResetDate: getNextQuotaReset(null),
526+
})
528527

529528
if (balance.netBalance > (org.auto_topup_threshold || 0)) {
530529
logger.info(

packages/billing/src/credit-delegation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ export async function consumeCreditsWithDelegation(params: {
172172

173173
// Consume credits from organization
174174
try {
175-
await consumeOrganizationCredits(
176-
orgLookup.organizationId,
177-
creditsToConsume,
178-
)
175+
await consumeOrganizationCredits({
176+
...params,
177+
organizationId: orgLookup.organizationId,
178+
})
179179

180180
logger.info(
181181
{

0 commit comments

Comments
 (0)