Skip to content

The open alternative to Salesforce, designed for AI.

fix(usage-limit): no workspace record or storage limit when the config variable is unset (#27803)

Etienneae69c8a34d72
diff --git a/packages/twenty-server/src/engine/core-modules/admin-panel/services/admin-panel-usage-limit.service.ts b/packages/twenty-server/src/engine/core-modules/admin-panel/services/admin-panel-usage-limit.service.ts
index 49ae35b34b5..25e47d84208 100644
--- a/packages/twenty-server/src/engine/core-modules/admin-panel/services/admin-panel-usage-limit.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/admin-panel/services/admin-panel-usage-limit.service.ts
@@ -4,7 +4,7 @@ import {
type AdminPanelUsageLimitDefaultDTO,
type AdminPanelWorkspaceUsageLimitsDTO,
} from 'src/engine/core-modules/admin-panel/dtos/admin-panel-workspace-usage-limits.dto';
-import { type NumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/numeric-config-variable-key.type';
+import { type OptionalNumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { UsageLimitService } from 'src/engine/core-modules/usage-limit/services/usage-limit.service';
import { type UsageLimitEntity } from 'src/engine/core-modules/usage-limit/usage-limit.entity';
@@ -34,8 +34,9 @@ export class AdminPanelUsageLimitService {
private buildDefaults(
usageLimits: UsageLimitEntity[],
): AdminPanelUsageLimitDefaultDTO[] {
- const getConfigValue = (key: NumericConfigVariableKey) =>
- this.twentyConfigService.get(key);
+ const getConfigValue = <TKey extends OptionalNumericConfigVariableKey>(
+ key: TKey,
+ ) => this.twentyConfigService.get(key);
return buildUsageLimitDefaultScopes({ getConfigValue }).map(
(usageLimitDefault) => {
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 878725c888d..b17dd5b58ce 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
@@ -711,22 +711,22 @@ export class ConfigVariables {
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.STORAGE_CONFIG,
description:
- 'Maximum bytes of file storage a single workspace may hold, all its files counted. An upload that would cross it is refused',
+ 'Maximum bytes of file storage a single workspace may hold, all its files counted. An upload that would cross it is refused. No limit when unset',
type: ConfigVariableType.NUMBER,
})
@CastToPositiveNumber()
@IsOptional()
- WORKSPACE_STORAGE_LIMIT_BYTES: number = 100 * 1024 * 1024 * 1024;
+ WORKSPACE_STORAGE_LIMIT_BYTES?: number;
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.STORAGE_CONFIG,
description:
- 'Maximum number of records a single workspace may hold across its objects, system objects other than messages and calendar events left out, soft-deleted records included. A write that would cross it is refused',
+ 'Maximum number of records a single workspace may hold across its objects, system objects other than messages and calendar events left out, soft-deleted records included. A write that would cross it is refused. No limit when unset',
type: ConfigVariableType.NUMBER,
})
@CastToPositiveNumber()
@IsOptional()
- WORKSPACE_RECORD_LIMIT: number = 10_000_000;
+ WORKSPACE_RECORD_LIMIT?: number;
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.LOGIC_FUNCTION_CONFIG,
diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type.ts
new file mode 100644
index 00000000000..8c60a2feda2
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type.ts
@@ -0,0 +1,9 @@
+import { type ConfigVariables } from 'src/engine/core-modules/twenty-config/config-variables';
+
+export type OptionalNumericConfigVariableKey = {
+ [Key in keyof ConfigVariables]-?: NonNullable<
+ ConfigVariables[Key]
+ > extends number
+ ? Key
+ : never;
+}[keyof ConfigVariables];
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.ts
index 0800f18fa34..aa1d6e53d30 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/services/usage-limit-stock.service.ts
@@ -322,14 +322,22 @@ export class UsageLimitStockService {
(stockLimitDefaultDefinition) =>
stockLimitDefaultDefinition.operationType === operationType,
)
- .map((stockLimitDefaultDefinition) => ({
- spenderType: stockLimitDefaultDefinition.spenderType,
- unit: stockLimitDefaultDefinition.unit,
- isOverridable: stockLimitDefaultDefinition.isOverridable,
- limitValue: this.twentyConfigService.get(
+ .flatMap((stockLimitDefaultDefinition) => {
+ const limitValue = this.twentyConfigService.get(
stockLimitDefaultDefinition.limitValueConfigVariable,
- ),
- }));
+ );
+
+ return isDefined(limitValue)
+ ? [
+ {
+ spenderType: stockLimitDefaultDefinition.spenderType,
+ unit: stockLimitDefaultDefinition.unit,
+ isOverridable: stockLimitDefaultDefinition.isOverridable,
+ limitValue,
+ },
+ ]
+ : [];
+ });
}
private async readRemainings({
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/types/stock-limit-default-definition.type.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/types/stock-limit-default-definition.type.ts
index ac1db7e1fb0..44bc85b38a2 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/types/stock-limit-default-definition.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/types/stock-limit-default-definition.type.ts
@@ -1,4 +1,4 @@
-import { type NumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/numeric-config-variable-key.type';
+import { type OptionalNumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type';
import { type SpenderType } from 'src/engine/core-modules/usage-limit/types/spender-type.type';
import { type UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';
import { type UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';
@@ -15,6 +15,6 @@ export type StockLimitDefaultDefinition<
unit: UsageUnit;
periodUnit: 'lifetime';
periodCount: 1;
- limitValueConfigVariable: NumericConfigVariableKey;
+ limitValueConfigVariable: OptionalNumericConfigVariableKey;
isOverridable: boolean;
};
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-usage-limit-default-scopes.util.spec.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-usage-limit-default-scopes.util.spec.ts
new file mode 100644
index 00000000000..5a2c6da111d
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/__tests__/build-usage-limit-default-scopes.util.spec.ts
@@ -0,0 +1,46 @@
+import { type ConfigVariables } from 'src/engine/core-modules/twenty-config/config-variables';
+import { type OptionalNumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type';
+import { buildUsageLimitDefaultScopes } from 'src/engine/core-modules/usage-limit/utils/build-usage-limit-default-scopes.util';
+import { UsageResourceType } from 'src/engine/core-modules/usage/enums/usage-resource-type.enum';
+
+const buildStockDefaultScopes = (
+ stockConfigValues: Pick<
+ ConfigVariables,
+ 'WORKSPACE_RECORD_LIMIT' | 'WORKSPACE_STORAGE_LIMIT_BYTES'
+ >,
+) =>
+ buildUsageLimitDefaultScopes({
+ getConfigValue: <TKey extends OptionalNumericConfigVariableKey>(
+ key: TKey,
+ ) =>
+ (key in stockConfigValues
+ ? stockConfigValues[key as keyof typeof stockConfigValues]
+ : 1000) as ConfigVariables[TKey],
+ })
+ .filter((scope) => scope.limitKind === 'stock')
+ .map(({ resourceType, limitValue }) => ({ resourceType, limitValue }));
+
+describe('buildUsageLimitDefaultScopes', () => {
+ it('leaves out stock defaults whose config variable is unset', () => {
+ expect(
+ buildStockDefaultScopes({
+ WORKSPACE_RECORD_LIMIT: undefined,
+ WORKSPACE_STORAGE_LIMIT_BYTES: undefined,
+ }),
+ ).toEqual([]);
+ });
+
+ it('keeps stock defaults whose config variable is set', () => {
+ expect(
+ buildStockDefaultScopes({
+ WORKSPACE_RECORD_LIMIT: 5,
+ WORKSPACE_STORAGE_LIMIT_BYTES: 10,
+ }),
+ ).toEqual(
+ expect.arrayContaining([
+ { resourceType: UsageResourceType.RECORD, limitValue: 5 },
+ { resourceType: UsageResourceType.STORAGE, limitValue: 10 },
+ ]),
+ );
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-usage-limit-default-scopes.util.ts b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-usage-limit-default-scopes.util.ts
index d5348b12190..8c47ea8d090 100644
--- a/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-usage-limit-default-scopes.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/usage-limit/utils/build-usage-limit-default-scopes.util.ts
@@ -1,4 +1,7 @@
-import { type NumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/numeric-config-variable-key.type';
+import { isDefined } from 'twenty-shared/utils';
+
+import { type ConfigVariables } from 'src/engine/core-modules/twenty-config/config-variables';
+import { type OptionalNumericConfigVariableKey } from 'src/engine/core-modules/twenty-config/types/optional-numeric-config-variable-key.type';
import { type UsageLimitDefaultDefinition } from 'src/engine/core-modules/usage-limit/types/usage-limit-default-definition.type';
import { type UsageLimitScope } from 'src/engine/core-modules/usage-limit/utils/build-usage-limit-scope.util';
import { findUsageLimitDefaults } from 'src/engine/core-modules/usage-limit/utils/find-usage-limit-defaults.util';
@@ -9,12 +12,16 @@ export type UsageLimitDefaultScope = UsageLimitScope & {
limitValue: number;
};
+type GetConfigValue = <TKey extends OptionalNumericConfigVariableKey>(
+ key: TKey,
+) => ConfigVariables[TKey];
+
const buildPeriodCount = ({
usageLimitDefault,
getConfigValue,
}: {
usageLimitDefault: UsageLimitDefaultDefinition;
- getConfigValue: (key: NumericConfigVariableKey) => number;
+ getConfigValue: GetConfigValue;
}): number =>
usageLimitDefault.limitKind === 'speed'
? Math.ceil(getConfigValue(usageLimitDefault.windowMsConfigVariable) / 1000)
@@ -23,19 +30,32 @@ const buildPeriodCount = ({
export const buildUsageLimitDefaultScopes = ({
getConfigValue,
}: {
- getConfigValue: (key: NumericConfigVariableKey) => number;
+ getConfigValue: GetConfigValue;
}): UsageLimitDefaultScope[] =>
Object.values(UsageResourceType).flatMap((resourceType) =>
- findUsageLimitDefaults({ resourceType }).map((usageLimitDefault) => ({
- resourceType: usageLimitDefault.resourceType,
- operationType: usageLimitDefault.operationType,
- spenderType: usageLimitDefault.spenderType,
- spenderId: usageLimitDefault.spenderId,
- limitKind: usageLimitDefault.limitKind,
- unit: usageLimitDefault.unit,
- periodUnit: usageLimitDefault.periodUnit,
- periodCount: buildPeriodCount({ usageLimitDefault, getConfigValue }),
- limitValue: getConfigValue(usageLimitDefault.limitValueConfigVariable),
- isOverridable: usageLimitDefault.isOverridable,
- })),
+ findUsageLimitDefaults({ resourceType }).flatMap((usageLimitDefault) => {
+ const limitValue = getConfigValue(
+ usageLimitDefault.limitValueConfigVariable,
+ );
+
+ return isDefined(limitValue)
+ ? [
+ {
+ resourceType: usageLimitDefault.resourceType,
+ operationType: usageLimitDefault.operationType,
+ spenderType: usageLimitDefault.spenderType,
+ spenderId: usageLimitDefault.spenderId,
+ limitKind: usageLimitDefault.limitKind,
+ unit: usageLimitDefault.unit,
+ periodUnit: usageLimitDefault.periodUnit,
+ periodCount: buildPeriodCount({
+ usageLimitDefault,
+ getConfigValue,
+ }),
+ limitValue,
+ isOverridable: usageLimitDefault.isOverridable,
+ },
+ ]
+ : [];
+ }),
);
diff --git a/packages/twenty-server/test/integration/graphql/suites/admin-panel/workspace-usage-limits.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/admin-panel/workspace-usage-limits.integration-spec.ts
index 6e98b93e26c..6cb9cadf21a 100644
--- a/packages/twenty-server/test/integration/graphql/suites/admin-panel/workspace-usage-limits.integration-spec.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/admin-panel/workspace-usage-limits.integration-spec.ts
@@ -8,6 +8,7 @@ import { gql } from 'graphql-tag';
import { type Repository } from 'typeorm';
import { type CoreEntityCacheService } from 'src/engine/core-entity-cache/services/core-entity-cache.service';
+import { type TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { UsageLimitEntity } from 'src/engine/core-modules/usage-limit/usage-limit.entity';
import { UserEntity } from 'src/engine/core-modules/user/user.entity';
import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';
@@ -86,6 +87,11 @@ const STORAGE_STOCK_PAYLOAD = {
};
describe('Workspace usage limits from the admin panel', () => {
+ const twentyConfigService = getAppProviderByClassName<TwentyConfigService>(
+ 'TwentyConfigService',
+ );
+ const readConfig = twentyConfigService.get.bind(twentyConfigService);
+
let usageLimitRepository: Repository<UsageLimitEntity>;
const findWorkspaceUsageLimits = async () => {
@@ -100,6 +106,16 @@ describe('Workspace usage limits from the admin panel', () => {
beforeAll(() => {
usageLimitRepository =
getCoreRepository<UsageLimitEntity>(UsageLimitEntity);
+
+ jest
+ .spyOn(twentyConfigService, 'get')
+ .mockImplementation((key) =>
+ key === 'WORKSPACE_STORAGE_LIMIT_BYTES' ? 10_000_000 : readConfig(key),
+ );
+ });
+
+ afterAll(() => {
+ jest.restoreAllMocks();
});
afterEach(async () => {