feat: downgrade team functionality when payment subscription has expired (#2092)

This commit is contained in:
Shubham Palriwala
2024-02-19 16:20:24 +05:30
committed by GitHub
parent eadf63b47e
commit 6ae782b984
2 changed files with 26 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ import Stripe from "stripe";
import { getTeam, updateTeam } from "@formbricks/lib/team/service";
import { ProductFeatureKeys, StripeProductNames } from "../lib/constants";
import { unsubscribeCoreAndAppSurveyFeatures, unsubscribeLinkSurveyProFeatures } from "../lib/downgradePlan";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
// https://github.com/stripe/stripe-node#configuration
@@ -31,12 +32,14 @@ export const handleSubscriptionDeleted = async (event: Stripe.Event) => {
"inactive";
updatedFeatures[ProductFeatureKeys.inAppSurvey as keyof typeof team.billing.features].unlimited =
false;
await unsubscribeCoreAndAppSurveyFeatures(teamId);
break;
case StripeProductNames.linkSurvey:
updatedFeatures[ProductFeatureKeys.linkSurvey as keyof typeof team.billing.features].status =
"inactive";
updatedFeatures[ProductFeatureKeys.linkSurvey as keyof typeof team.billing.features].unlimited =
false;
await unsubscribeLinkSurveyProFeatures(teamId);
break;
case StripeProductNames.userTargeting:
updatedFeatures[ProductFeatureKeys.userTargeting as keyof typeof team.billing.features].status =

View File

@@ -0,0 +1,23 @@
import { getProducts, updateProduct } from "@formbricks/lib/product/service";
export const unsubscribeLinkSurveyProFeatures = async (teamId: string) => {
const productsOfTeam = await getProducts(teamId);
for (const product of productsOfTeam) {
if (!product.linkSurveyBranding) {
await updateProduct(product.id, {
linkSurveyBranding: true,
});
}
}
};
export const unsubscribeCoreAndAppSurveyFeatures = async (teamId: string) => {
const productsOfTeam = await getProducts(teamId);
for (const product of productsOfTeam) {
if (!product.inAppSurveyBranding) {
await updateProduct(product.id, {
inAppSurveyBranding: true,
});
}
}
};