Skip to content

Commit 02161e1

Browse files
committed
address comments
1 parent e561c3b commit 02161e1

2 files changed

Lines changed: 49 additions & 16 deletions

File tree

apps/sim/lib/billing/calculations/usage-monitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ export async function checkUsageStatus(
250250

251251
const percentUsed =
252252
effectiveLimit > 0 ? Math.min((currentUsage / effectiveLimit) * 100, 100) : 100
253-
const isExceeded = effectiveLimit > 0 && currentUsage >= effectiveLimit
253+
254+
const isExceeded = currentUsage >= effectiveLimit
254255
const isWarning = !isExceeded && percentUsed >= WARNING_THRESHOLD
255256

256257
logger.info('Final usage statistics', {

apps/sim/lib/billing/core/billing.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getHighestPrioritySubscription,
77
type SubscriptionMetadata,
88
} from '@/lib/billing/core/subscription'
9-
import { getUserUsageData } from '@/lib/billing/core/usage'
9+
import { getOrgUsageLimit, getUserUsageData } from '@/lib/billing/core/usage'
1010
import { getCreditBalance } from '@/lib/billing/credits/balance'
1111
import {
1212
computeDailyRefreshConsumed,
@@ -361,18 +361,50 @@ export async function getSimplifiedBillingSummary(
361361
// for org-scoped subs, which would N-times-count.
362362
const pooled = await aggregateOrgMemberStats(organizationId)
363363

364-
const totalCurrentUsage = pooled.currentPeriodCost
364+
const rawCurrentUsage = pooled.currentPeriodCost
365365
const totalCopilotCost = pooled.currentPeriodCopilotCost
366366
const totalLastPeriodCopilotCost = pooled.lastPeriodCopilotCost
367367

368+
// Deduct daily-refresh credits against this specific org's pool.
369+
// `usageData` is derived from the caller's priority subscription
370+
// and may not match the requested org (multi-org admins, personal
371+
// priority sub, etc.), so it cannot be reused here.
372+
let refreshDeduction = 0
373+
if (isPaid(plan) && subscription.periodStart) {
374+
const planDollars = getPlanTierDollars(plan)
375+
if (planDollars > 0) {
376+
const userBounds = await getOrgMemberRefreshBounds(
377+
organizationId,
378+
subscription.periodStart
379+
)
380+
refreshDeduction = await computeDailyRefreshConsumed({
381+
userIds: pooled.memberIds,
382+
periodStart: subscription.periodStart,
383+
periodEnd: subscription.periodEnd ?? null,
384+
planDollars,
385+
seats: subscription.seats || 1,
386+
userBounds: Object.keys(userBounds).length > 0 ? userBounds : undefined,
387+
})
388+
}
389+
}
390+
const effectiveCurrentUsage = Math.max(0, rawCurrentUsage - refreshDeduction)
391+
392+
const { limit: orgUsageLimit } = await getOrgUsageLimit(
393+
organizationId,
394+
plan,
395+
subscription.seats ?? null
396+
)
397+
368398
const percentUsed =
369-
usageData.limit > 0 ? Math.round((usageData.currentUsage / usageData.limit) * 100) : 0
399+
orgUsageLimit > 0 ? Math.round((effectiveCurrentUsage / orgUsageLimit) * 100) : 0
400+
const isExceeded = effectiveCurrentUsage >= orgUsageLimit
401+
const isWarning = !isExceeded && percentUsed >= 80
370402

371403
// Calculate days remaining in billing period
372-
const daysRemaining = usageData.billingPeriodEnd
404+
const daysRemaining = subscription.periodEnd
373405
? Math.max(
374406
0,
375-
Math.ceil((usageData.billingPeriodEnd.getTime() - Date.now()) / (1000 * 60 * 60 * 24))
407+
Math.ceil((subscription.periodEnd.getTime() - Date.now()) / (1000 * 60 * 60 * 24))
376408
)
377409
: 0
378410

@@ -382,11 +414,11 @@ export async function getSimplifiedBillingSummary(
382414
return {
383415
type: 'organization',
384416
plan: subscription.plan,
385-
currentUsage: totalCurrentUsage,
386-
usageLimit: usageData.limit,
417+
currentUsage: effectiveCurrentUsage,
418+
usageLimit: orgUsageLimit,
387419
percentUsed,
388-
isWarning: percentUsed >= 80 && percentUsed < 100,
389-
isExceeded: usageData.currentUsage >= usageData.limit,
420+
isWarning,
421+
isExceeded,
390422
daysRemaining,
391423
creditBalance: orgCredits.balance,
392424
billingInterval: orgBillingInterval,
@@ -405,13 +437,13 @@ export async function getSimplifiedBillingSummary(
405437
cancelAtPeriodEnd: subscription.cancelAtPeriodEnd || undefined,
406438
// Usage details
407439
usage: {
408-
current: usageData.currentUsage,
409-
limit: usageData.limit,
440+
current: effectiveCurrentUsage,
441+
limit: orgUsageLimit,
410442
percentUsed,
411-
isWarning: percentUsed >= 80 && percentUsed < 100,
412-
isExceeded: usageData.currentUsage >= usageData.limit,
413-
billingPeriodStart: usageData.billingPeriodStart,
414-
billingPeriodEnd: usageData.billingPeriodEnd,
443+
isWarning,
444+
isExceeded,
445+
billingPeriodStart: subscription.periodStart ?? null,
446+
billingPeriodEnd: subscription.periodEnd ?? null,
415447
lastPeriodCost: usageData.lastPeriodCost,
416448
lastPeriodCopilotCost: totalLastPeriodCopilotCost,
417449
daysRemaining,

0 commit comments

Comments
 (0)