refactor: update active customer check to use authenticated action and improve ChatwootWidget integration

This commit is contained in:
Dhruwang
2026-03-18 17:43:40 +05:30
parent 731c4373c3
commit 3cf4edb037
2 changed files with 12 additions and 11 deletions

View File

@@ -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]);

View File

@@ -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<boolean> => {
const session = await getServerSession(authOptions);
if (!session?.user?.id) return false;
export const getIsActiveCustomerAction = authenticatedActionClient.action(async ({ ctx }) => {
const paidBillingPlans = new Set<TCloudBillingPlan>(["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;
});
};
});