MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / getOrderedActiveGrants

Function getOrderedActiveGrants

packages/billing/src/balance-calculator.ts:85–102  ·  view source on GitHub ↗
(params: {
  userId: string
  now: Date
  conn?: DbConn
})

Source from the content-addressed store, hash-verified

83 * Added optional `conn` param so callers inside a transaction can supply their TX object.
84 */
85export async function getOrderedActiveGrants(params: {
86 userId: string
87 now: Date
88 conn?: DbConn
89}) {
90 const { userId, now, conn = db } = params
91 const activeGrantsFilter = buildActiveGrantsFilter(userId, now)
92 return conn
93 .select()
94 .from(schema.creditLedger)
95 .where(activeGrantsFilter)
96 .orderBy(
97 // Use grants based on priority, then expiration date, then creation date
98 asc(schema.creditLedger.priority),
99 asc(schema.creditLedger.expires_at),
100 asc(schema.creditLedger.created_at),
101 )
102}
103
104/**
105 * Gets active grants ordered for credit consumption, ensuring the "last grant" is always

Callers 1

calculateUsageAndBalanceFunction · 0.85

Calls 2

fromMethod · 0.80
buildActiveGrantsFilterFunction · 0.70

Tested by

no test coverage detected