Skip to content

The open alternative to Salesforce, designed for AI.

Let usage limit defaults override the plan while trialing (#28703)

Etienne06a5c6bc4c7c
diff --git a/packages/twenty-server/src/engine/core-modules/billing/services/billing-credit-allowance.service.ts b/packages/twenty-server/src/engine/core-modules/billing/services/billing-credit-allowance.service.ts
index e5419ea083a..a96adfbf539 100644
--- a/packages/twenty-server/src/engine/core-modules/billing/services/billing-credit-allowance.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/billing/services/billing-credit-allowance.service.ts
@@ -5,6 +5,7 @@ import { Injectable } from '@nestjs/common';
import { isDefined } from 'twenty-shared/utils';
import { NO_BILLING_SUBSCRIPTION } from 'src/engine/core-modules/billing/constants/no-billing-subscription.constant';
+import { SubscriptionStatus } from 'src/engine/core-modules/billing/enums/billing-subscription-status.enum';
import { UserCreditsFallback } from 'src/engine/core-modules/billing/enums/billing-user-credits-fallback.enum';
import { type FlatBillingSubscription } from 'src/engine/core-modules/billing/types/flat-billing-subscription.type';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
@@ -58,10 +59,19 @@ export class BillingCreditAllowanceService {
};
}
- async getPlanKey(workspaceId: string): Promise<string | null> {
+ async getSubscriptionPlan(
+ workspaceId: string,
+ ): Promise<{ planKey: string | null; isTrialing: boolean } | null> {
const subscription = await this.findCurrentBillingSubscription(workspaceId);
- return subscription?.planKey ?? null;
+ if (!isDefined(subscription)) {
+ return null;
+ }
+
+ return {
+ planKey: subscription.planKey,
+ isTrialing: subscription.status === SubscriptionStatus.Trialing,
+ };
}
async getUserCreditsFallback(
diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
index 6dee587acab..d92520650b2 100644
--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
@@ -1060,7 +1060,7 @@ export class ConfigVariables {
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.BILLING_CONFIG,
description:
- 'Quota and stock usage limit default values per billing plan, keyed by plan key then by the default\'s config variable, as {"PRO":{"WORKSPACE_RECORD_LIMIT":1000000},"ENTERPRISE":{"WORKSPACE_RECORD_LIMIT":null}}. null means no limit, and a variable left out keeps its own value',
+ 'Quota and stock usage limit default values per billing plan, keyed by plan key then by the default\'s config variable, as {"PRO":{"WORKSPACE_RECORD_LIMIT":1000000},"ENTERPRISE":{"WORKSPACE_RECORD_LIMIT":null},"TRIAL":{"AI_USER_CREDITS_LIMIT":null}}. TRIAL applies to every trialing subscription and wins over its plan. null means no limit, and a variable left out keeps its own value',
isHiddenInAdminPanel: true,
type: ConfigVariableType.JSON,
})
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/trial-usage-limit-defaults-key.constant.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/trial-usage-limit-defaults-key.constant.ts
new file mode 100644
index 00000000000..f66085ae20d
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/trial-usage-limit-defaults-key.constant.ts
@@ -0,0 +1 @@
+export const TRIAL_USAGE_LIMIT_DEFAULTS_KEY = 'TRIAL';
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-default-value.service.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-default-value.service.ts
index 78395005330..875ba06c0ae 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-default-value.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-default-value.service.ts
@@ -38,16 +38,17 @@ export class UsageLimitDefaultValueService {
return instanceValue;
}
- const planKey =
- await this.billingCreditAllowanceService.getPlanKey(workspaceId);
+ const subscriptionPlan =
+ await this.billingCreditAllowanceService.getSubscriptionPlan(workspaceId);
- if (!isDefined(planKey)) {
+ if (!isDefined(subscriptionPlan)) {
return instanceValue;
}
return getPlanLimitDefaultValue({
usageLimitDefaultsByPlan,
- planKey,
+ planKey: subscriptionPlan.planKey,
+ isTrialing: subscriptionPlan.isTrialing,
limitValueConfigVariable,
instanceValue,
});
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/get-plan-limit-default-value.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/get-plan-limit-default-value.util.spec.ts
index d3abd863802..fb0e2a3930d 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/get-plan-limit-default-value.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/get-plan-limit-default-value.util.spec.ts
@@ -8,23 +8,41 @@ const usageLimitDefaultsByPlan: Record<string, PlanUsageLimitDefaults> = {
PRO: {
WORKSPACE_RECORD_LIMIT: 1_000,
EMAIL_SEND_WORKSPACE_DAILY_LIMIT: null,
+ AI_USER_CREDITS_LIMIT: 5_000,
+ },
+ TRIAL: {
+ AI_USER_CREDITS_LIMIT: null,
+ WORKSPACE_STORAGE_LIMIT_BYTES: 100,
},
};
describe('getPlanLimitDefaultValue', () => {
- it.each<[string, OptionalNumericConfigVariableKey, number | null]>([
- ['PRO', 'WORKSPACE_RECORD_LIMIT', 1_000],
- ['PRO', 'EMAIL_SEND_WORKSPACE_DAILY_LIMIT', null],
- ['PRO', 'WORKSPACE_STORAGE_LIMIT_BYTES', INSTANCE_VALUE],
- ['UNKNOWN', 'WORKSPACE_RECORD_LIMIT', INSTANCE_VALUE],
- ])('on %s, %s is %p', (planKey, limitValueConfigVariable, expected) => {
- expect(
- getPlanLimitDefaultValue({
- usageLimitDefaultsByPlan,
- planKey,
- limitValueConfigVariable,
- instanceValue: INSTANCE_VALUE,
- }),
- ).toBe(expected);
- });
+ it.each<
+ [string | null, boolean, OptionalNumericConfigVariableKey, number | null]
+ >([
+ ['PRO', false, 'WORKSPACE_RECORD_LIMIT', 1_000],
+ ['PRO', false, 'EMAIL_SEND_WORKSPACE_DAILY_LIMIT', null],
+ ['PRO', false, 'WORKSPACE_STORAGE_LIMIT_BYTES', INSTANCE_VALUE],
+ ['PRO', false, 'AI_USER_CREDITS_LIMIT', 5_000],
+ ['UNKNOWN', false, 'WORKSPACE_RECORD_LIMIT', INSTANCE_VALUE],
+ [null, false, 'WORKSPACE_RECORD_LIMIT', INSTANCE_VALUE],
+ ['PRO', true, 'AI_USER_CREDITS_LIMIT', null],
+ ['PRO', true, 'WORKSPACE_STORAGE_LIMIT_BYTES', 100],
+ ['PRO', true, 'WORKSPACE_RECORD_LIMIT', 1_000],
+ ['UNKNOWN', true, 'EMAIL_SEND_WORKSPACE_DAILY_LIMIT', INSTANCE_VALUE],
+ [null, true, 'AI_USER_CREDITS_LIMIT', null],
+ ])(
+ 'on %s with isTrialing %s, %s is %p',
+ (planKey, isTrialing, limitValueConfigVariable, expected) => {
+ expect(
+ getPlanLimitDefaultValue({
+ usageLimitDefaultsByPlan,
+ planKey,
+ isTrialing,
+ limitValueConfigVariable,
+ instanceValue: INSTANCE_VALUE,
+ }),
+ ).toBe(expected);
+ },
+ );
});
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/get-plan-limit-default-value.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/get-plan-limit-default-value.util.ts
index 8f139c17455..4df69744f3d 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/get-plan-limit-default-value.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/get-plan-limit-default-value.util.ts
@@ -1,21 +1,36 @@
import { isUndefined } from '@sniptt/guards';
+import { isDefined } from 'twenty-shared/utils';
import { type OptionalNumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type';
+import { TRIAL_USAGE_LIMIT_DEFAULTS_KEY } from 'src/engine/core-modules/usage-limit/constants/trial-usage-limit-defaults-key.constant';
import { type PlanUsageLimitDefaults } from 'src/engine/core-modules/usage-limit/types/plan-usage-limit-defaults.type';
export const getPlanLimitDefaultValue = ({
usageLimitDefaultsByPlan,
planKey,
+ isTrialing,
limitValueConfigVariable,
instanceValue,
}: {
usageLimitDefaultsByPlan: Record<string, PlanUsageLimitDefaults>;
- planKey: string;
+ planKey: string | null;
+ isTrialing: boolean;
limitValueConfigVariable: OptionalNumericConfigVariableKey;
instanceValue: number | null;
}): number | null => {
- const planValue =
- usageLimitDefaultsByPlan[planKey]?.[limitValueConfigVariable];
+ const trialValue = isTrialing
+ ? usageLimitDefaultsByPlan[TRIAL_USAGE_LIMIT_DEFAULTS_KEY]?.[
+ limitValueConfigVariable
+ ]
+ : undefined;
+
+ if (!isUndefined(trialValue)) {
+ return trialValue;
+ }
+
+ const planValue = isDefined(planKey)
+ ? usageLimitDefaultsByPlan[planKey]?.[limitValueConfigVariable]
+ : undefined;
return isUndefined(planValue) ? instanceValue : planValue;
};