(params: {
organizationId: string
currentBalance: number
recentUsage: number
organizationName?: string
logger: Logger
})
| 258 | * Monitors organization credit consumption and sends alerts when needed |
| 259 | */ |
| 260 | export async function monitorOrganizationCredits(params: { |
| 261 | organizationId: string |
| 262 | currentBalance: number |
| 263 | recentUsage: number |
| 264 | organizationName?: string |
| 265 | logger: Logger |
| 266 | }): Promise<void> { |
| 267 | const { |
| 268 | organizationId, |
| 269 | currentBalance, |
| 270 | recentUsage, |
| 271 | organizationName: _organizationName, |
| 272 | logger, |
| 273 | } = params |
| 274 | |
| 275 | const LOW_BALANCE_THRESHOLD = 100 // Credits |
| 276 | const HIGH_USAGE_THRESHOLD = 1000 // Credits per day |
| 277 | |
| 278 | try { |
| 279 | // Check for low balance |
| 280 | if (currentBalance < LOW_BALANCE_THRESHOLD) { |
| 281 | await sendOrganizationAlert({ |
| 282 | ...params, |
| 283 | alertType: 'low_balance', |
| 284 | currentBalance, |
| 285 | threshold: LOW_BALANCE_THRESHOLD, |
| 286 | }) |
| 287 | } |
| 288 | |
| 289 | // Check for high usage |
| 290 | if (recentUsage > HIGH_USAGE_THRESHOLD) { |
| 291 | await sendOrganizationAlert({ |
| 292 | ...params, |
| 293 | alertType: 'high_usage', |
| 294 | usageAmount: recentUsage, |
| 295 | threshold: HIGH_USAGE_THRESHOLD, |
| 296 | }) |
| 297 | } |
| 298 | |
| 299 | // Check for negative balance (debt) |
| 300 | if (currentBalance < 0) { |
| 301 | await sendOrganizationAlert({ |
| 302 | ...params, |
| 303 | alertType: 'failed_consumption', |
| 304 | currentBalance, |
| 305 | error: 'Organization has negative credit balance', |
| 306 | }) |
| 307 | } |
| 308 | } catch (error) { |
| 309 | logger.error( |
| 310 | { organizationId, error }, |
| 311 | 'Error monitoring organization credits', |
| 312 | ) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Tracks organization usage metrics for analytics |
nothing calls this directly
no test coverage detected