Skip to content

Commit 2b3fa2e

Browse files
committed
pass in logger to db/transaction.ts
1 parent fb2c99d commit 2b3fa2e

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

common/src/db/transaction.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import db from './index'
2-
import { logger } from '../util/logger'
32
import { withRetry } from '../util/promise'
43

4+
import type { Logger } from '@codebuff/types/logger'
5+
56
type TransactionCallback<T> = Parameters<typeof db.transaction<T>>[0]
67

78
/**
@@ -12,10 +13,15 @@ type TransactionCallback<T> = Parameters<typeof db.transaction<T>>[0]
1213
* @param context Additional context for logging (e.g., userId, operationId)
1314
* @returns The result of the transaction
1415
*/
15-
export async function withSerializableTransaction<T>(
16-
callback: TransactionCallback<T>,
17-
context: Record<string, any> = {},
18-
): Promise<T> {
16+
export async function withSerializableTransaction<T>({
17+
callback,
18+
context = {},
19+
logger,
20+
}: {
21+
callback: TransactionCallback<T>
22+
context: Record<string, any>
23+
logger: Logger
24+
}): Promise<T> {
1925
return withRetry(
2026
async () => {
2127
return await db.transaction(callback, { isolationLevel: 'serializable' })

packages/billing/src/balance-calculator.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ export async function consumeCredits(
298298
userId: string,
299299
creditsToConsume: number,
300300
): Promise<CreditConsumptionResult> {
301-
return await withSerializableTransaction(
302-
async (tx) => {
301+
return await withSerializableTransaction({
302+
callback: async (tx) => {
303303
const now = new Date()
304304
const activeGrants = await getOrderedActiveGrants(userId, now, tx)
305305

@@ -320,8 +320,9 @@ export async function consumeCredits(
320320

321321
return result
322322
},
323-
{ userId, creditsToConsume },
324-
)
323+
context: { userId, creditsToConsume },
324+
logger,
325+
})
325326
}
326327

327328
export async function consumeCreditsAndAddAgentStep(options: {
@@ -374,8 +375,8 @@ export async function consumeCreditsAndAddAgentStep(options: {
374375

375376
try {
376377
return success(
377-
await withSerializableTransaction(
378-
async (tx) => {
378+
await withSerializableTransaction({
379+
callback: async (tx) => {
379380
const now = new Date()
380381
const activeGrants = await getOrderedActiveGrants(userId, now, tx)
381382

@@ -426,8 +427,9 @@ export async function consumeCreditsAndAddAgentStep(options: {
426427

427428
return { ...result, agentStepId: stepId }
428429
},
429-
{ userId, credits },
430-
),
430+
context: { userId, credits },
431+
logger,
432+
}),
431433
)
432434
} catch (error) {
433435
return failure(error)

packages/billing/src/org-billing.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ export async function consumeOrganizationCredits(
259259
organizationId: string,
260260
creditsToConsume: number,
261261
): Promise<CreditConsumptionResult> {
262-
return await withSerializableTransaction(
263-
async (tx) => {
262+
return await withSerializableTransaction({
263+
callback: async (tx) => {
264264
const now = new Date()
265265
const activeGrants = await getOrderedActiveOrganizationGrants(
266266
organizationId,
@@ -285,8 +285,9 @@ export async function consumeOrganizationCredits(
285285

286286
return result
287287
},
288-
{ organizationId, creditsToConsume },
289-
)
288+
context: { organizationId, creditsToConsume },
289+
logger,
290+
})
290291
}
291292

292293
/**

0 commit comments

Comments
 (0)