* Get organization billing summary for admin dashboard
(organizationId: string)
| 362 | * Get organization billing summary for admin dashboard |
| 363 | */ |
| 364 | async function getOrganizationBillingSummary(organizationId: string) { |
| 365 | try { |
| 366 | const billingData = await getOrganizationBillingData(organizationId) |
| 367 | |
| 368 | if (!billingData) { |
| 369 | return null |
| 370 | } |
| 371 | |
| 372 | // Calculate additional metrics |
| 373 | const membersOverLimit = billingData.members.filter((m) => m.isOverLimit).length |
| 374 | const membersNearLimit = billingData.members.filter( |
| 375 | (m) => !m.isOverLimit && m.percentUsed >= 80 |
| 376 | ).length |
| 377 | |
| 378 | const topUsers = billingData.members.slice(0, 5).map((m) => ({ |
| 379 | name: m.userName, |
| 380 | usage: m.currentUsage, |
| 381 | limit: m.usageLimit, |
| 382 | percentUsed: m.percentUsed, |
| 383 | })) |
| 384 | |
| 385 | return { |
| 386 | organization: { |
| 387 | id: billingData.organizationId, |
| 388 | name: billingData.organizationName, |
| 389 | plan: billingData.subscriptionPlan, |
| 390 | status: billingData.subscriptionStatus, |
| 391 | }, |
| 392 | usage: { |
| 393 | total: billingData.totalCurrentUsage, |
| 394 | limit: billingData.totalUsageLimit, |
| 395 | average: billingData.averageUsagePerMember, |
| 396 | percentUsed: |
| 397 | billingData.totalUsageLimit > 0 |
| 398 | ? (billingData.totalCurrentUsage / billingData.totalUsageLimit) * 100 |
| 399 | : 0, |
| 400 | }, |
| 401 | seats: { |
| 402 | total: billingData.totalSeats, |
| 403 | used: billingData.usedSeats, |
| 404 | available: billingData.totalSeats - billingData.usedSeats, |
| 405 | }, |
| 406 | alerts: { |
| 407 | membersOverLimit, |
| 408 | membersNearLimit, |
| 409 | }, |
| 410 | billingPeriod: { |
| 411 | start: billingData.billingPeriodStart, |
| 412 | end: billingData.billingPeriodEnd, |
| 413 | }, |
| 414 | topUsers, |
| 415 | } |
| 416 | } catch (error) { |
| 417 | logger.error('Failed to get organization billing summary', { organizationId, error }) |
| 418 | throw error |
| 419 | } |
| 420 | } |
| 421 |
nothing calls this directly
no test coverage detected