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