diff --git a/packages/ee/billing/handlers/subscriptionDeleted.ts b/packages/ee/billing/handlers/subscriptionDeleted.ts index b77a405f24..ab4246895a 100644 --- a/packages/ee/billing/handlers/subscriptionDeleted.ts +++ b/packages/ee/billing/handlers/subscriptionDeleted.ts @@ -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 = diff --git a/packages/ee/billing/lib/downgradePlan.ts b/packages/ee/billing/lib/downgradePlan.ts new file mode 100644 index 0000000000..d3f620dc16 --- /dev/null +++ b/packages/ee/billing/lib/downgradePlan.ts @@ -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, + }); + } + } +};