(subscriptionId: string, withoutCache: boolean = false)
| 74 | } |
| 75 | |
| 76 | public async getFeaturesByPlan(subscriptionId: string, withoutCache: boolean = false) { |
| 77 | if (!this.stripe || !subscriptionId) { |
| 78 | return {} |
| 79 | } |
| 80 | |
| 81 | if (!withoutCache) { |
| 82 | const subscriptionData = await this.cacheManager.getSubscriptionDataFromCache(subscriptionId) |
| 83 | if (subscriptionData?.features) { |
| 84 | return subscriptionData.features |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | const subscription = await this.stripe.subscriptions.retrieve(subscriptionId, { |
| 89 | timeout: 5000 |
| 90 | }) |
| 91 | if (subscription.status === 'canceled') throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, 'Subscription is canceled') |
| 92 | const items = subscription.items.data |
| 93 | if (items.length === 0) { |
| 94 | return {} |
| 95 | } |
| 96 | |
| 97 | const productId = items[0].price.product as string |
| 98 | const product = await this.stripe.products.retrieve(productId, { |
| 99 | timeout: 5000 |
| 100 | }) |
| 101 | const productMetadata = product.metadata |
| 102 | |
| 103 | if (!productMetadata || Object.keys(productMetadata).length === 0) { |
| 104 | return {} |
| 105 | } |
| 106 | |
| 107 | const features: Record<string, string> = {} |
| 108 | for (const key in productMetadata) { |
| 109 | if (key.startsWith('feat:')) { |
| 110 | features[key] = productMetadata[key] |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | await this.cacheManager.updateSubscriptionDataToCache(subscriptionId, { |
| 115 | features, |
| 116 | subsriptionDetails: this.getSubscriptionObject(subscription) |
| 117 | }) |
| 118 | |
| 119 | return features |
| 120 | } |
| 121 | |
| 122 | public async createStripeCustomerPortalSession(req: Request) { |
| 123 | if (!this.stripe) { |
no test coverage detected