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

Function decrementStorageUsage

apps/sim/lib/billing/storage/tracking.ts:125–167  ·  view source on GitHub ↗
(
  userId: string,
  bytes: number,
  workspaceId?: string
)

Source from the content-addressed store, hash-verified

123 * after a shrink. Best-effort; never blocks the caller.
124 */
125export async function decrementStorageUsage(
126 userId: string,
127 bytes: number,
128 workspaceId?: string
129): Promise<void> {
130 if (!isBillingEnabled) {
131 logger.debug('Billing disabled, skipping storage decrement')
132 return
133 }
134
135 let sub: HighestPrioritySubscription | null = null
136 try {
137 const { getHighestPrioritySubscription } = await import('@/lib/billing/core/subscription')
138 sub = await getHighestPrioritySubscription(userId)
139
140 if (isOrgScopedSubscription(sub, userId) && sub) {
141 await db
142 .update(organization)
143 .set({
144 storageUsedBytes: sql`GREATEST(0, ${organization.storageUsedBytes} - ${bytes})`,
145 })
146 .where(eq(organization.id, sub.referenceId))
147
148 logger.info(`Decremented org storage: ${bytes} bytes for org ${sub.referenceId}`)
149 } else {
150 await db
151 .update(userStats)
152 .set({
153 storageUsedBytes: sql`GREATEST(0, ${userStats.storageUsedBytes} - ${bytes})`,
154 })
155 .where(eq(userStats.userId, userId))
156
157 logger.info(`Decremented user storage: ${bytes} bytes for user ${userId}`)
158 }
159 } catch (error) {
160 logger.error('Error decrementing storage usage:', error)
161 throw error
162 }
163
164 if (workspaceId) {
165 void maybeNotifyStorageLimit(userId, workspaceId, sub, true)
166 }
167}

Callers 1

Calls 8

isOrgScopedSubscriptionFunction · 0.90
maybeNotifyStorageLimitFunction · 0.85
debugMethod · 0.80
infoMethod · 0.80
errorMethod · 0.80
setMethod · 0.65
eqFunction · 0.50

Tested by

no test coverage detected