MCPcopy Create free account
hub / github.com/FlowiseAI/Flowise / getQuotas

Method getQuotas

packages/server/src/UsageCacheManager.ts:113–162  ·  view source on GitHub ↗
(subscriptionId: string, withoutCache: boolean = false)

Source from the content-addressed store, hash-verified

111 }
112
113 public async getQuotas(subscriptionId: string, withoutCache: boolean = false): Promise<Record<string, number>> {
114 const stripeManager = await StripeManager.getInstance()
115 if (!stripeManager || !subscriptionId) {
116 return UNLIMITED_QUOTAS
117 }
118
119 // Skip cache if withoutCache is true
120 if (!withoutCache) {
121 const subscriptionData = await this.getSubscriptionDataFromCache(subscriptionId)
122 if (subscriptionData?.quotas) {
123 return subscriptionData.quotas
124 }
125 }
126
127 // If not in cache, retrieve from Stripe
128 const subscription = await stripeManager.getStripe().subscriptions.retrieve(subscriptionId)
129 if (subscription.status === 'canceled') throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, 'Subscription is canceled')
130 const items = subscription.items.data
131 if (items.length === 0) {
132 return DISABLED_QUOTAS
133 }
134
135 const productId = items[0].price.product as string
136 const product = await stripeManager.getStripe().products.retrieve(productId)
137 const productMetadata = product.metadata
138
139 if (!productMetadata || Object.keys(productMetadata).length === 0) {
140 return DISABLED_QUOTAS
141 }
142
143 const quotas: Record<string, number> = {}
144 for (const key in productMetadata) {
145 if (key.startsWith('quota:')) {
146 quotas[key] = parseInt(productMetadata[key])
147 }
148 }
149
150 const additionalSeatsItem = subscription.items.data.find(
151 (item) => (item.price.product as string) === process.env.ADDITIONAL_SEAT_ID
152 )
153 quotas[LICENSE_QUOTAS.ADDITIONAL_SEATS_LIMIT] = additionalSeatsItem?.quantity || 0
154
155 // Update subscription data cache with quotas
156 await this.updateSubscriptionDataToCache(subscriptionId, {
157 quotas,
158 subsriptionDetails: stripeManager.getSubscriptionObject(subscription)
159 })
160
161 return quotas
162 }
163
164 public async getSubscriptionDataFromCache(subscriptionId: string) {
165 const cacheKey = `subscription:${subscriptionId}`

Callers 6

getCurrentUsageFunction · 0.80
checkUsageLimitFunction · 0.80
updatePredictionsUsageFunction · 0.80
checkPredictionsFunction · 0.80
checkStorageFunction · 0.80

Calls 5

getStripeMethod · 0.80
getSubscriptionObjectMethod · 0.80
getInstanceMethod · 0.45

Tested by

no test coverage detected