(params: {
userId: string
grant: typeof schema.creditLedger.$inferSelect
consumed: number
newBalance: number
tx: DbConn
logger: Logger
})
| 165 | * Updates a single grant's balance and logs the change. |
| 166 | */ |
| 167 | export 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. |
no test coverage detected