The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Allow a zero usage limit (#27627)
Mariebb995e2eba56
diff --git a/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitForm.tsx b/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitForm.tsxindex ce43f727c6b..94c65b385f7 100644--- a/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitForm.tsx+++ b/packages/twenty-front/src/modules/settings/billing/components/SettingsBillingLimitForm.tsx@@ -25,6 +25,7 @@ import { getUsageLimitLabel } from '@/settings/billing/utils/getUsageLimitLabel'import { getUsageLimitAmountInput } from '@/settings/billing/utils/getUsageLimitAmountInput';import { getUsageLimitRingColor } from '@/settings/billing/utils/getUsageLimitRingColor';import { getUsageLimitUnitLabel } from '@/settings/billing/utils/getUsageLimitUnitLabel';+import { hasUsageLimitValue } from '@/settings/billing/utils/hasUsageLimitValue';import { Select } from '@/ui/input/components/Select';import { SettingsTextInput } from '@/ui/input/components/SettingsTextInput';import { TooltipDelay } from '@/ui/layout/tooltip/constants/TooltipDelay';@@ -139,11 +140,13 @@ export const SettingsBillingLimitForm = ({};const consumedValue = scopeConsumption?.consumedValue ?? null;- const progress = computeUsageLimitProgress({- limitValue:- Number(values.limitValue) * getUsageLimitInputScale(values.unit),- consumedValue,- });+ const progress = hasUsageLimitValue(values)+ ? computeUsageLimitProgress({+ limitValue:+ Number(values.limitValue) * getUsageLimitInputScale(values.unit),+ consumedValue,+ })+ : null;const consumedPercentage = progress?.consumedPercentage ?? 0;const isExhausted = progress?.remainingValue === 0;const hasConsumption = isDefined(consumedValue) && consumedValue > 0;diff --git a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/buildCreateUsageLimitInput.test.ts b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/buildCreateUsageLimitInput.test.tsindex 4f661271a01..2220ab364be 100644--- a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/buildCreateUsageLimitInput.test.ts+++ b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/buildCreateUsageLimitInput.test.ts@@ -67,8 +67,14 @@ describe('buildCreateUsageLimitInput', () => {).toBe(2);});- it('rejects an amount that is not a positive number', () => {- for (const limitValue of ['', 'abc', '-5', '0', '1e30']) {+ it('accepts a zero amount to block the usage entirely', () => {+ expect(+ buildCreateUsageLimitInput(buildValues({ limitValue: '0' }))?.limitValue,+ ).toBe(0);+ });++ it('rejects an amount that is not a positive number or zero', () => {+ for (const limitValue of ['', ' ', 'abc', '-5', '1e30']) {expect(buildCreateUsageLimitInput(buildValues({ limitValue })),).toBeNull();diff --git a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/computeUsageLimitProgress.test.ts b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/computeUsageLimitProgress.test.tsindex 9d779bc202e..1ea819a5272 100644--- a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/computeUsageLimitProgress.test.ts+++ b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/computeUsageLimitProgress.test.ts@@ -37,9 +37,21 @@ describe('computeUsageLimitProgress', () => {).toBeNull();});- it('reports nothing when the limit cannot be divided by', () => {+ it('treats a zero limit as exhausted, even before consumption loads', () => {+ for (const consumedValue of [0, null]) {+ expect(+ computeUsageLimitProgress({ limitValue: 0, consumedValue }),+ ).toEqual({+ remainingValue: 0,+ consumedPercentage: 100,+ remainingPercentage: 0,+ });+ }+ });++ it('reports nothing for a negative limit', () => {expect(- computeUsageLimitProgress({ limitValue: 0, consumedValue: 10 }),+ computeUsageLimitProgress({ limitValue: -1, consumedValue: 10 }),).toBeNull();});});diff --git a/packages/twenty-front/src/modules/settings/billing/utils/__tests__/hasUsageLimitValue.test.ts b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/hasUsageLimitValue.test.tsnew file mode 100644index 00000000000..6f0c191549c--- /dev/null+++ b/packages/twenty-front/src/modules/settings/billing/utils/__tests__/hasUsageLimitValue.test.ts@@ -0,0 +1,12 @@+import { hasUsageLimitValue } from '@/settings/billing/utils/hasUsageLimitValue';++describe('hasUsageLimitValue', () => {+ it('counts zero as an entered amount', () => {+ expect(hasUsageLimitValue({ limitValue: '0' })).toBe(true);+ });++ it('ignores a blank amount', () => {+ expect(hasUsageLimitValue({ limitValue: '' })).toBe(false);+ expect(hasUsageLimitValue({ limitValue: ' ' })).toBe(false);+ });+});diff --git a/packages/twenty-front/src/modules/settings/billing/utils/buildCreateUsageLimitInput.ts b/packages/twenty-front/src/modules/settings/billing/utils/buildCreateUsageLimitInput.tsindex 17f1100d51c..1309d016411 100644--- a/packages/twenty-front/src/modules/settings/billing/utils/buildCreateUsageLimitInput.ts+++ b/packages/twenty-front/src/modules/settings/billing/utils/buildCreateUsageLimitInput.ts@@ -3,6 +3,7 @@ import { isDefined } from 'twenty-shared/utils';import { type UsageLimitFormValues } from '@/settings/billing/types/UsageLimitFormValues';import { buildUsageQuotaScopeInput } from '@/settings/billing/utils/buildUsageQuotaScopeInput';import { getUsageLimitInputScale } from '@/settings/billing/utils/getUsageLimitInputScale';+import { hasUsageLimitValue } from '@/settings/billing/utils/hasUsageLimitValue';import { type CreateUsageLimitInput } from '~/generated-metadata/graphql';export const buildCreateUsageLimitInput = (@@ -10,15 +11,20 @@ export const buildCreateUsageLimitInput = (): CreateUsageLimitInput | null => {const scope = buildUsageQuotaScopeInput(values);- if (!isDefined(scope)) {+ if (!isDefined(scope) || !hasUsageLimitValue(values)) {return null;}+ const inputValue = Number(values.limitValue);const limitValue = Math.round(- Number(values.limitValue) * getUsageLimitInputScale(scope.unit),+ inputValue * getUsageLimitInputScale(scope.unit),);- if (!Number.isSafeInteger(limitValue) || limitValue < 1) {+ if (+ !Number.isSafeInteger(limitValue) ||+ limitValue < 0 ||+ (limitValue === 0 && inputValue !== 0)+ ) {return null;}diff --git a/packages/twenty-front/src/modules/settings/billing/utils/computeUsageLimitProgress.ts b/packages/twenty-front/src/modules/settings/billing/utils/computeUsageLimitProgress.tsindex c3b198d8018..7d9fc68388f 100644--- a/packages/twenty-front/src/modules/settings/billing/utils/computeUsageLimitProgress.ts+++ b/packages/twenty-front/src/modules/settings/billing/utils/computeUsageLimitProgress.ts@@ -13,7 +13,15 @@ export const computeUsageLimitProgress = ({limitValue: number;consumedValue: number | null;}): UsageLimitProgress | null => {- if (!isDefined(consumedValue) || limitValue <= 0) {+ if (limitValue === 0) {+ return {+ remainingValue: 0,+ consumedPercentage: 100,+ remainingPercentage: 0,+ };+ }++ if (!isDefined(consumedValue) || limitValue < 0) {return null;}diff --git a/packages/twenty-front/src/modules/settings/billing/utils/hasUsageLimitValue.ts b/packages/twenty-front/src/modules/settings/billing/utils/hasUsageLimitValue.tsnew file mode 100644index 00000000000..52cecd1e6a3--- /dev/null+++ b/packages/twenty-front/src/modules/settings/billing/utils/hasUsageLimitValue.ts@@ -0,0 +1,7 @@+import { isNonEmptyString } from '@sniptt/guards';++import { type UsageLimitFormValues } from '@/settings/billing/types/UsageLimitFormValues';++export const hasUsageLimitValue = (+ values: Pick<UsageLimitFormValues, 'limitValue'>,+): boolean => isNonEmptyString(values.limitValue.trim());diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/limit-kind-rules.constant.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/limit-kind-rules.constant.tsindex 58db161c5b8..b9a41fdd9c8 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/constants/limit-kind-rules.constant.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/constants/limit-kind-rules.constant.ts@@ -8,17 +8,20 @@ export const LIMIT_KIND_RULES: Record<LimitKind, LimitKindRule> = {allowedPeriodUnits: ['second'],requiredPeriodCount: null,isBurstValueAllowed: true,+ isZeroLimitValueAllowed: false,},quota: {isAllOperationTypeAllowed: true,allowedPeriodUnits: ANCHORED_PERIOD_UNITS,requiredPeriodCount: 1,isBurstValueAllowed: false,+ isZeroLimitValueAllowed: true,},stock: {isAllOperationTypeAllowed: false,allowedPeriodUnits: ['lifetime'],requiredPeriodCount: 1,isBurstValueAllowed: false,+ isZeroLimitValueAllowed: false,},};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/dtos/create-usage-limit.input.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/dtos/create-usage-limit.input.tsindex 43f8a74097d..2ed40255545 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/dtos/create-usage-limit.input.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/dtos/create-usage-limit.input.ts@@ -59,7 +59,7 @@ export class CreateUsageLimitInput {@Field(() => GraphQLBigInt)@IsInt()- @Min(1)+ @Min(0)limitValue: number;@Field(() => GraphQLBigInt, { nullable: true })diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/types/limit-kind-rule.type.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/types/limit-kind-rule.type.tsindex 6e0157909eb..67bdcaf85a8 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/types/limit-kind-rule.type.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/types/limit-kind-rule.type.ts@@ -5,4 +5,5 @@ export type LimitKindRule = {allowedPeriodUnits: readonly PeriodUnit[];requiredPeriodCount: number | null;isBurstValueAllowed: boolean;+ isZeroLimitValueAllowed: boolean;};diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/validate-usage-limit-against-kind-rule.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/validate-usage-limit-against-kind-rule.util.spec.tsindex 2c338533840..0240bd1fd73 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/validate-usage-limit-against-kind-rule.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/validate-usage-limit-against-kind-rule.util.spec.ts@@ -65,6 +65,10 @@ describe('validateUsageLimitAgainstKindRule', () => {).not.toThrow();});+ it('rejects a zero speed limit', () => {+ rejects({ ...validSpeedLimit, limitValue: 0 });+ });+it('rejects a speed limit without a rolling window', () => {rejects({ ...validSpeedLimit, periodUnit: 'month' });});@@ -91,6 +95,15 @@ describe('validateUsageLimitAgainstKindRule', () => {).not.toThrow();});+ it('accepts a zero quota that blocks the usage', () => {+ expect(() =>+ validateUsageLimitAgainstKindRule({+ ...validQuotaLimit,+ limitValue: 0,+ }),+ ).not.toThrow();+ });+it('accepts a weekly quota', () => {expect(() =>validateUsageLimitAgainstKindRule({@@ -127,6 +140,10 @@ describe('validateUsageLimitAgainstKindRule', () => {});describe('stock', () => {+ it('rejects a zero stock limit', () => {+ rejects({ ...validStockLimit, limitValue: 0 });+ });+it('accepts a workspace-wide stock on the operation that fills it', () => {expect(() =>validateUsageLimitAgainstKindRule(validStockLimit),diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/validate-usage-limit-against-kind-rule.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/validate-usage-limit-against-kind-rule.util.tsindex 7838cd01a56..f12ec279ca7 100644--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/validate-usage-limit-against-kind-rule.util.ts+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/validate-usage-limit-against-kind-rule.util.ts@@ -47,6 +47,13 @@ export const validateUsageLimitAgainstKindRule = ();}+ if (!rule.isZeroLimitValueAllowed && input.limitValue === 0) {+ throw new UsageLimitException(+ `A ${input.limitKind} limit needs a limit value above zero`,+ UsageLimitExceptionCode.LIMIT_INVALID,+ );+ }+if (!rule.isBurstValueAllowed && isDefined(input.burstValue)) {throw new UsageLimitException(`A ${input.limitKind} limit cannot hold a burst value`,