( userId: string, organizationId?: string, executor: DbClient = db )
| 392 | * Get comprehensive billing and subscription summary |
| 393 | */ |
| 394 | export async function getSimplifiedBillingSummary( |
| 395 | userId: string, |
| 396 | organizationId?: string, |
| 397 | executor: DbClient = db |
| 398 | ): Promise<{ |
| 399 | type: 'individual' | 'organization' |
| 400 | plan: string |
| 401 | currentUsage: number |
| 402 | usageLimit: number |
| 403 | percentUsed: number |
| 404 | isWarning: boolean |
| 405 | isExceeded: boolean |
| 406 | daysRemaining: number |
| 407 | creditBalance: number |
| 408 | billingInterval: 'month' | 'year' |
| 409 | // Subscription details |
| 410 | isPaid: boolean |
| 411 | isPro: boolean |
| 412 | isTeam: boolean |
| 413 | isEnterprise: boolean |
| 414 | /** True when the subscription's `referenceId` is an organization id. */ |
| 415 | isOrgScoped: boolean |
| 416 | /** Present when `isOrgScoped` is true. */ |
| 417 | organizationId: string | null |
| 418 | status: string | null |
| 419 | seats: number | null |
| 420 | metadata: any |
| 421 | stripeSubscriptionId: string | null |
| 422 | periodEnd: Date | string | null |
| 423 | cancelAtPeriodEnd?: boolean |
| 424 | // Usage details |
| 425 | usage: { |
| 426 | current: number |
| 427 | limit: number |
| 428 | percentUsed: number |
| 429 | isWarning: boolean |
| 430 | isExceeded: boolean |
| 431 | billingPeriodStart: Date | null |
| 432 | billingPeriodEnd: Date | null |
| 433 | lastPeriodCost: number |
| 434 | lastPeriodCopilotCost: number |
| 435 | daysRemaining: number |
| 436 | copilotCost: number |
| 437 | } |
| 438 | }> { |
| 439 | try { |
| 440 | // Get subscription and usage data upfront |
| 441 | const [subscription, usageData] = await Promise.all([ |
| 442 | organizationId |
| 443 | ? getOrganizationSubscription(organizationId, { executor }) |
| 444 | : getHighestPrioritySubscription(userId, { executor }), |
| 445 | getUserUsageData(userId, executor), |
| 446 | ]) |
| 447 | |
| 448 | const plan = subscription?.plan || 'free' |
| 449 | const hasPaidEntitlement = hasPaidSubscriptionStatus(subscription?.status) |
| 450 | const planIsPaid = hasPaidEntitlement && isPaid(plan) |
| 451 | const planIsPro = hasPaidEntitlement && isPro(plan) |
no test coverage detected