(params: {
organizationId: string
logger: Logger
})
| 43 | * Gets organization alerts for UI display |
| 44 | */ |
| 45 | export async function getOrganizationAlerts(params: { |
| 46 | organizationId: string |
| 47 | logger: Logger |
| 48 | }): Promise<OrganizationAlert[]> { |
| 49 | const { organizationId, logger } = params |
| 50 | |
| 51 | const alerts: OrganizationAlert[] = [] |
| 52 | |
| 53 | try { |
| 54 | // Get organization settings |
| 55 | const organization = await db.query.org.findFirst({ |
| 56 | where: eq(schema.org.id, organizationId), |
| 57 | }) |
| 58 | |
| 59 | if (!organization) { |
| 60 | return alerts |
| 61 | } |
| 62 | |
| 63 | // Check current balance |
| 64 | const now = new Date() |
| 65 | const quotaResetDate = getNextQuotaReset(now) |
| 66 | const { balance, usageThisCycle } = |
| 67 | await calculateOrganizationUsageAndBalance({ |
| 68 | ...params, |
| 69 | quotaResetDate, |
| 70 | now, |
| 71 | }) |
| 72 | |
| 73 | // Low balance alert |
| 74 | if (organization.billing_alerts && balance.netBalance < 500) { |
| 75 | alerts.push({ |
| 76 | id: `low-balance-${organizationId}`, |
| 77 | type: 'low_balance', |
| 78 | severity: balance.netBalance < 100 ? 'critical' : 'warning', |
| 79 | title: 'Low Credit Balance', |
| 80 | message: `Organization has ${balance.netBalance} credits remaining`, |
| 81 | timestamp: new Date(), |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | // High usage alert |
| 86 | if (organization.usage_alerts && usageThisCycle > 5000) { |
| 87 | alerts.push({ |
| 88 | id: `high-usage-${organizationId}`, |
| 89 | type: 'high_usage', |
| 90 | severity: 'info', |
| 91 | title: 'High Usage This Cycle', |
| 92 | message: `Organization has used ${usageThisCycle} credits this billing cycle`, |
| 93 | timestamp: new Date(), |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | // Credit limit alert |
| 98 | if ( |
| 99 | organization.credit_limit && |
| 100 | usageThisCycle >= organization.credit_limit * 0.9 |
| 101 | ) { |
| 102 | alerts.push({ |
no test coverage detected