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

Function updateGrantBalance

packages/billing/src/balance-calculator.ts:167–200  ·  view source on GitHub ↗
(params: {
  userId: string
  grant: typeof schema.creditLedger.$inferSelect
  consumed: number
  newBalance: number
  tx: DbConn
  logger: Logger
})

Source from the content-addressed store, hash-verified

165 * Updates a single grant's balance and logs the change.
166 */
167export async function updateGrantBalance(params: {
168 userId: string
169 grant: typeof schema.creditLedger.$inferSelect
170 consumed: number
171 newBalance: number
172 tx: DbConn
173 logger: Logger
174}) {
175 const {
176 userId: _userId,
177 grant,
178 consumed: _consumed,
179 newBalance,
180 tx,
181 logger: _logger,
182 } = params
183 await tx
184 .update(schema.creditLedger)
185 .set({ balance: newBalance })
186 .where(eq(schema.creditLedger.operation_id, grant.operation_id))
187
188 // Note (James): This log was too noisy. Reenable it as you need to test something.
189 // logger.debug(
190 // {
191 // userId,
192 // grantId: grant.operation_id,
193 // grantType: grant.type,
194 // consumed,
195 // remaining: newBalance,
196 // expiresAt: grant.expires_at,
197 // },
198 // 'Updated grant remaining amount after consumption',
199 // )
200}
201
202/**
203 * Consumes credits from a list of ordered grants.

Callers 1

consumeFromOrderedGrantsFunction · 0.70

Calls 1

setMethod · 0.80

Tested by

no test coverage detected