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

Function applyOrgRefresh

apps/sim/lib/billing/calculations/usage-monitor.ts:176–210  ·  view source on GitHub ↗
(
  organizationId: string,
  sub: {
    plan: string | null
    seats: number | null
    periodStart: Date | null
    periodEnd: Date | null
  },
  currentUsage: number,
  preloadedMemberIds?: string[]
)

Source from the content-addressed store, hash-verified

174}
175
176async function applyOrgRefresh(
177 organizationId: string,
178 sub: {
179 plan: string | null
180 seats: number | null
181 periodStart: Date | null
182 periodEnd: Date | null
183 },
184 currentUsage: number,
185 preloadedMemberIds?: string[]
186): Promise<number> {
187 if (!isPaid(sub.plan) || !sub.periodStart) {
188 return currentUsage
189 }
190
191 const memberIds =
192 preloadedMemberIds ?? (await getPooledOrgCurrentPeriodCost(organizationId)).memberIds
193 if (memberIds.length === 0) return currentUsage
194
195 const planDollars = getPlanTierDollars(sub.plan)
196 if (planDollars <= 0) return currentUsage
197
198 const userBounds = await getOrgMemberRefreshBounds(organizationId, sub.periodStart)
199 const refresh = await computeDailyRefreshConsumed({
200 userIds: memberIds,
201 periodStart: sub.periodStart,
202 periodEnd: sub.periodEnd ?? null,
203 planDollars,
204 seats: sub.seats || 1,
205 userBounds: Object.keys(userBounds).length > 0 ? userBounds : undefined,
206 billingEntity: { type: 'organization', id: organizationId },
207 })
208
209 return Math.max(0, currentUsage - refresh)
210}
211
212function buildUsageData(params: {
213 currentUsage: number

Callers 1

computePooledOrgUsageFunction · 0.85

Calls 5

isPaidFunction · 0.90
getPlanTierDollarsFunction · 0.90

Tested by

no test coverage detected