11import db from '@codebuff/common/db'
22import * as schema from '@codebuff/common/db/schema'
3- import { logger } from '@codebuff/common/util/logger'
43import { eq , and } from 'drizzle-orm'
54
65import { consumeCredits } from './balance-calculator'
@@ -10,6 +9,8 @@ import {
109 extractOwnerAndRepo ,
1110} from './org-billing'
1211
12+ import type { Logger } from '@codebuff/types/logger'
13+
1314export interface OrganizationLookupResult {
1415 found : boolean
1516 organizationId ?: string
@@ -29,10 +30,13 @@ export interface CreditDelegationResult {
2930 * Finds the organization associated with a repository for a given user.
3031 * Uses owner/repo comparison for better matching.
3132 */
32- export async function findOrganizationForRepository (
33- userId : string ,
34- repositoryUrl : string ,
35- ) : Promise < OrganizationLookupResult > {
33+ export async function findOrganizationForRepository ( params : {
34+ userId : string
35+ repositoryUrl : string
36+ logger : Logger
37+ } ) : Promise < OrganizationLookupResult > {
38+ const { userId, repositoryUrl, logger } = params
39+
3640 try {
3741 const normalizedUrl = normalizeRepositoryUrl ( repositoryUrl )
3842 const ownerRepo = extractOwnerAndRepo ( normalizedUrl )
@@ -135,23 +139,28 @@ export async function findOrganizationForRepository(
135139/**
136140 * Consumes credits with organization delegation if applicable.
137141 */
138- export async function consumeCreditsWithDelegation (
139- userId : string ,
140- repositoryUrl : string | null ,
141- creditsToConsume : number ,
142- ) : Promise < CreditDelegationResult > {
143- try {
144- // If no repository URL, fall back to personal credits
145- if ( ! repositoryUrl ) {
146- logger . debug (
147- { userId, creditsToConsume } ,
148- 'No repository URL provided, falling back to personal credits' ,
149- )
150- return { success : false , error : 'No repository URL provided' }
151- }
142+ export async function consumeCreditsWithDelegation ( params : {
143+ userId : string
144+ repositoryUrl : string | null
145+ creditsToConsume : number
146+ logger : Logger
147+ } ) : Promise < CreditDelegationResult > {
148+ const { userId, repositoryUrl, creditsToConsume, logger } = params
152149
150+ // If no repository URL, fall back to personal credits
151+ if ( ! repositoryUrl ) {
152+ logger . debug (
153+ { userId, creditsToConsume } ,
154+ 'No repository URL provided, falling back to personal credits' ,
155+ )
156+ return { success : false , error : 'No repository URL provided' }
157+ }
158+
159+ const withRepoUrl = { ...params , repositoryUrl }
160+
161+ try {
153162 // Find organization for this repository
154- const orgLookup = await findOrganizationForRepository ( userId , repositoryUrl )
163+ const orgLookup = await findOrganizationForRepository ( withRepoUrl )
155164
156165 if ( ! orgLookup . found || ! orgLookup . organizationId ) {
157166 logger . debug (
@@ -216,13 +225,6 @@ export async function consumeCreditsWithDelegation(
216225 }
217226}
218227
219- export interface CreditConsumptionOptions {
220- userId : string
221- creditsToCharge : number
222- repoUrl ?: string | null
223- context : string // Description of what the credits are for (e.g., 'web search', 'documentation lookup')
224- }
225-
226228export interface CreditFallbackResult {
227229 success : boolean
228230 organizationId ?: string
@@ -235,19 +237,23 @@ export interface CreditFallbackResult {
235237 * Helper function that decides whether to charge credits to an organization or user directly.
236238 * Tries organization delegation first if a repo URL is available, falls back to personal credits.
237239 */
238- export async function consumeCreditsWithFallback (
239- options : CreditConsumptionOptions ,
240- ) : Promise < CreditFallbackResult > {
241- const { userId, creditsToCharge, repoUrl, context } = options
240+ export async function consumeCreditsWithFallback ( params : {
241+ userId : string
242+ creditsToCharge : number
243+ repoUrl ?: string | null
244+ context : string // Description of what the credits are for (e.g., 'web search', 'documentation lookup')
245+ logger : Logger
246+ } ) : Promise < CreditFallbackResult > {
247+ const { userId, creditsToCharge, repoUrl, context, logger } = params
242248
243249 try {
244250 // Try organization delegation first if repo URL is available
245251 if ( repoUrl ) {
246- const delegationResult = await consumeCreditsWithDelegation (
247- userId ,
248- repoUrl ,
249- creditsToCharge ,
250- )
252+ const delegationResult = await consumeCreditsWithDelegation ( {
253+ ... params ,
254+ repositoryUrl : repoUrl ,
255+ creditsToConsume : creditsToCharge ,
256+ } )
251257
252258 if ( delegationResult . success ) {
253259 logger . debug (
0 commit comments