MCPcopy Index your code
hub / github.com/simstudioai/sim / getPooledOrgCurrentPeriodCost

Function getPooledOrgCurrentPeriodCost

apps/sim/lib/billing/core/usage.ts:62–90  ·  view source on GitHub ↗
(
  organizationId: string,
  executor: DbClient = db
)

Source from the content-addressed store, hash-verified

60 * downstream refresh / bounds computations.
61 */
62export async function getPooledOrgCurrentPeriodCost(
63 organizationId: string,
64 executor: DbClient = db
65): Promise<{ memberIds: string[]; currentPeriodCost: number; lastPeriodCost: number }> {
66 const rows = await executor
67 .select({
68 userId: member.userId,
69 currentPeriodCost: userStats.currentPeriodCost,
70 lastPeriodCost: userStats.lastPeriodCost,
71 })
72 .from(member)
73 .leftJoin(userStats, eq(member.userId, userStats.userId))
74 .where(eq(member.organizationId, organizationId))
75
76 let pooled = new Decimal(0)
77 let lastPeriodCost = new Decimal(0)
78 const memberIds: string[] = []
79 for (const row of rows) {
80 memberIds.push(row.userId)
81 pooled = pooled.plus(toDecimal(row.currentPeriodCost))
82 lastPeriodCost = lastPeriodCost.plus(toDecimal(row.lastPeriodCost))
83 }
84
85 return {
86 memberIds,
87 currentPeriodCost: toNumber(pooled),
88 lastPeriodCost: toNumber(lastPeriodCost),
89 }
90}
91
92/**
93 * Calculates the effective usage limit for an organization-scoped plan.

Callers 4

computePooledOrgUsageFunction · 0.90
applyOrgRefreshFunction · 0.90
getUserUsageDataFunction · 0.85

Calls 4

toDecimalFunction · 0.90
toNumberFunction · 0.90
eqFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected