Skip to content

The open alternative to Salesforce, designed for AI.

Share one billing entitlement check across settings and logs (#26972)

Thomas des Francs667d35b79a7a
diff --git a/packages/twenty-front/src/modules/log-console/components/LogConsole.tsx b/packages/twenty-front/src/modules/log-console/components/LogConsole.tsx
index 934e3026a0c..9a1609a516f 100644
--- a/packages/twenty-front/src/modules/log-console/components/LogConsole.tsx
+++ b/packages/twenty-front/src/modules/log-console/components/LogConsole.tsx
@@ -51,6 +51,7 @@ import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/use
import { useAtomState } from '@/ui/utilities/state/jotai/hooks/useAtomState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
+import { checkIfBillingEntitlementIsEnabledOnWorkspace } from '@/workspace/utils/checkIfBillingEntitlementIsEnabledOnWorkspace';
import { BillingEntitlementKey } from '~/generated-metadata/graphql';
const LOG_CONSOLE_HEIGHT_CSS_VARIABLE = '--log-console-height';
@@ -380,12 +381,10 @@ export const LogConsole = () => {
const panelHeight =
!isExiting && displayedLayout.isFullScreen ? '100%' : spacerHeight;
- const hasAuditLogsEntitlement =
- currentWorkspace?.billingEntitlements?.some(
- (entitlement) =>
- entitlement.key === BillingEntitlementKey.AUDIT_LOGS &&
- entitlement.value,
- ) ?? false;
+ const hasAuditLogsEntitlement = checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.AUDIT_LOGS,
+ currentWorkspace,
+ );
const isSourceLocked = (source: LogConsoleSource) =>
source.requiresAuditLogs && !hasAuditLogsEntitlement;
diff --git a/packages/twenty-front/src/modules/settings/event-logs/components/SettingsLogs.tsx b/packages/twenty-front/src/modules/settings/event-logs/components/SettingsLogs.tsx
index f2b3bf2f278..7482f69f25e 100644
--- a/packages/twenty-front/src/modules/settings/event-logs/components/SettingsLogs.tsx
+++ b/packages/twenty-front/src/modules/settings/event-logs/components/SettingsLogs.tsx
@@ -15,6 +15,7 @@ import { useEventLogsLiveStream } from '@/settings/event-logs/hooks/useEventLogs
import { useEventLogs } from '@/settings/event-logs/hooks/useQueryEventLogs';
import { type EventLogFiltersState } from '@/settings/event-logs/types/EventLogFiltersState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
+import { checkIfBillingEntitlementIsEnabledOnWorkspace } from '@/workspace/utils/checkIfBillingEntitlementIsEnabledOnWorkspace';
import { SettingsPath } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import {
@@ -99,12 +100,10 @@ export const SettingsLogs = () => {
const navigateSettings = useNavigateSettings();
const isBillingEnabled = billing?.isBillingEnabled ?? false;
- const hasAuditLogsEntitlement =
- currentWorkspace?.billingEntitlements?.some(
- (entitlement) =>
- entitlement.key === BillingEntitlementKey.AUDIT_LOGS &&
- entitlement.value,
- ) === true;
+ const hasAuditLogsEntitlement = checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.AUDIT_LOGS,
+ currentWorkspace,
+ );
const [selectedTable, setSelectedTable] = useState<EventLogTable>(
EventLogTable.PAGEVIEW,
diff --git a/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/object-form/components/SettingsRolePermissionsObjectLevelObjectForm.tsx b/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/object-form/components/SettingsRolePermissionsObjectLevelObjectForm.tsx
index 3c48eabb574..c694bd7dc8e 100644
--- a/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/object-form/components/SettingsRolePermissionsObjectLevelObjectForm.tsx
+++ b/packages/twenty-front/src/modules/settings/roles/role-permissions/object-level-permissions/object-form/components/SettingsRolePermissionsObjectLevelObjectForm.tsx
@@ -21,8 +21,8 @@ import {
} from 'twenty-shared/utils';
import { themeCssVariables } from 'twenty-ui/theme';
import { useQuery } from '@apollo/client/react';
+import { checkIfBillingEntitlementIsEnabledOnWorkspace } from '@/workspace/utils/checkIfBillingEntitlementIsEnabledOnWorkspace';
import {
- type BillingEntitlement,
BillingEntitlementKey,
FindOneAgentDocument,
} from '~/generated-metadata/graphql';
@@ -56,14 +56,11 @@ export const SettingsRolePermissionsObjectLevelObjectForm = ({
objectId: objectMetadataId,
});
- const workspaceBillingEntitlements = currentWorkspace?.billingEntitlements;
-
const isRLSBillingEntitlementEnabled =
- workspaceBillingEntitlements?.some(
- (entitlement: BillingEntitlement) =>
- entitlement.key === BillingEntitlementKey.RLS &&
- entitlement.value === true,
- ) ?? false;
+ checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.RLS,
+ currentWorkspace,
+ );
const objectMetadataItem = objectMetadata.objectMetadataItem;
diff --git a/packages/twenty-front/src/modules/workspace/utils/__tests__/checkIfBillingEntitlementIsEnabledOnWorkspace.test.ts b/packages/twenty-front/src/modules/workspace/utils/__tests__/checkIfBillingEntitlementIsEnabledOnWorkspace.test.ts
new file mode 100644
index 00000000000..13aaf64442e
--- /dev/null
+++ b/packages/twenty-front/src/modules/workspace/utils/__tests__/checkIfBillingEntitlementIsEnabledOnWorkspace.test.ts
@@ -0,0 +1,61 @@
+import { type CurrentWorkspace } from '@/auth/states/currentWorkspaceState';
+import { checkIfBillingEntitlementIsEnabledOnWorkspace } from '@/workspace/utils/checkIfBillingEntitlementIsEnabledOnWorkspace';
+import { BillingEntitlementKey } from '~/generated-metadata/graphql';
+
+const makeWorkspace = (
+ billingEntitlements?: Array<{ key: BillingEntitlementKey; value: boolean }>,
+): CurrentWorkspace =>
+ ({
+ id: 'workspace-1',
+ billingEntitlements,
+ }) as unknown as CurrentWorkspace;
+
+describe('checkIfBillingEntitlementIsEnabledOnWorkspace', () => {
+ it('should return true when the entitlement is enabled', () => {
+ const workspace = makeWorkspace([
+ { key: BillingEntitlementKey.SSO, value: false },
+ { key: BillingEntitlementKey.AUDIT_LOGS, value: true },
+ ]);
+
+ expect(
+ checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.AUDIT_LOGS,
+ workspace,
+ ),
+ ).toBe(true);
+ });
+
+ it('should return false when the entitlement is disabled', () => {
+ const workspace = makeWorkspace([
+ { key: BillingEntitlementKey.AUDIT_LOGS, value: false },
+ ]);
+
+ expect(
+ checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.AUDIT_LOGS,
+ workspace,
+ ),
+ ).toBe(false);
+ });
+
+ it('should return false when the entitlement is missing', () => {
+ expect(
+ checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.RLS,
+ makeWorkspace([{ key: BillingEntitlementKey.SSO, value: true }]),
+ ),
+ ).toBe(false);
+ expect(
+ checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.RLS,
+ makeWorkspace(),
+ ),
+ ).toBe(false);
+ expect(
+ checkIfBillingEntitlementIsEnabledOnWorkspace(
+ BillingEntitlementKey.RLS,
+ null,
+ ),
+ ).toBe(false);
+ });
+});
diff --git a/packages/twenty-front/src/modules/workspace/utils/checkIfBillingEntitlementIsEnabledOnWorkspace.ts b/packages/twenty-front/src/modules/workspace/utils/checkIfBillingEntitlementIsEnabledOnWorkspace.ts
new file mode 100644
index 00000000000..6110c60c5c5
--- /dev/null
+++ b/packages/twenty-front/src/modules/workspace/utils/checkIfBillingEntitlementIsEnabledOnWorkspace.ts
@@ -0,0 +1,11 @@
+import { type CurrentWorkspace } from '@/auth/states/currentWorkspaceState';
+import { type BillingEntitlementKey } from '~/generated-metadata/graphql';
+
+export const checkIfBillingEntitlementIsEnabledOnWorkspace = (
+ entitlementKey: BillingEntitlementKey,
+ workspace: CurrentWorkspace | null | undefined,
+) =>
+ workspace?.billingEntitlements?.some(
+ (entitlement) =>
+ entitlement.key === entitlementKey && entitlement.value === true,
+ ) === true;