From 3cf4edb0379f349e80951394b669c05f9c154349 Mon Sep 17 00:00:00 2001 From: Dhruwang Date: Wed, 18 Mar 2026 17:43:40 +0530 Subject: [PATCH] refactor: update active customer check to use authenticated action and improve ChatwootWidget integration --- apps/web/app/chatwoot/ChatwootWidget.tsx | 8 +++++--- apps/web/app/chatwoot/actions.ts | 15 +++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/apps/web/app/chatwoot/ChatwootWidget.tsx b/apps/web/app/chatwoot/ChatwootWidget.tsx index 9dc9442cb6..5ce27956eb 100644 --- a/apps/web/app/chatwoot/ChatwootWidget.tsx +++ b/apps/web/app/chatwoot/ChatwootWidget.tsx @@ -1,7 +1,7 @@ "use client"; import { useCallback, useEffect, useRef } from "react"; -import { getIsActiveCustomer } from "./actions"; +import { getIsActiveCustomerAction } from "./actions"; interface ChatwootWidgetProps { chatwootBaseUrl: string; @@ -55,8 +55,10 @@ export const ChatwootWidget = ({ const $chatwoot = getChatwoot(); if (!$chatwoot) return; - const isActiveCustomer = await getIsActiveCustomer(); - $chatwoot.setCustomAttributes({ isActiveCustomer }); + const response = await getIsActiveCustomerAction(); + if (response?.data !== undefined) { + $chatwoot.setCustomAttributes({ isActiveCustomer: response.data }); + } customerStatusSetRef.current = true; }, [getChatwoot]); diff --git a/apps/web/app/chatwoot/actions.ts b/apps/web/app/chatwoot/actions.ts index 5d8705a8a8..fd33f04d39 100644 --- a/apps/web/app/chatwoot/actions.ts +++ b/apps/web/app/chatwoot/actions.ts @@ -1,19 +1,18 @@ "use server"; -import { getServerSession } from "next-auth"; +import { TCloudBillingPlan } from "@formbricks/types/organizations"; import { getOrganizationsByUserId } from "@/lib/organization/service"; -import { authOptions } from "@/modules/auth/lib/authOptions"; +import { authenticatedActionClient } from "@/lib/utils/action-client"; -export const getIsActiveCustomer = async (): Promise => { - const session = await getServerSession(authOptions); - if (!session?.user?.id) return false; +export const getIsActiveCustomerAction = authenticatedActionClient.action(async ({ ctx }) => { + const paidBillingPlans = new Set(["pro", "scale", "custom"]); - const organizations = await getOrganizationsByUserId(session.user.id); + const organizations = await getOrganizationsByUserId(ctx.user.id); return organizations.some((organization) => { const stripe = organization.billing.stripe; - const isPaidPlan = stripe?.plan === "pro" || stripe?.plan === "scale" || stripe?.plan === "custom"; + const isPaidPlan = stripe?.plan ? paidBillingPlans.has(stripe.plan) : false; const isActiveSubscription = stripe?.subscriptionStatus === "active" || stripe?.subscriptionStatus === "trialing"; return isPaidPlan && isActiveSubscription; }); -}; +});