The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Add feature-flag:update workspace command (#29098)
martmull4a4a4479deb0
diff --git a/packages/twenty-server/src/database/commands/__tests__/update-feature-flag.command.spec.ts b/packages/twenty-server/src/database/commands/__tests__/update-feature-flag.command.spec.tsnew file mode 100644index 00000000000..d25f970f978--- /dev/null+++ b/packages/twenty-server/src/database/commands/__tests__/update-feature-flag.command.spec.ts@@ -0,0 +1,63 @@+import { Logger } from '@nestjs/common';+import { FeatureFlagKey } from 'twenty-shared/types';++import { type WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';+import { UpdateFeatureFlagCommand } from 'src/database/commands/update-feature-flag.command';+import { type FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';++describe('UpdateFeatureFlagCommand', () => {+ const featureFlagKey = Object.values(FeatureFlagKey)[0];+ const upsertWorkspaceFeatureFlag = jest.fn();++ const command = new UpdateFeatureFlagCommand(+ {} as WorkspaceIteratorService,+ { upsertWorkspaceFeatureFlag } as unknown as FeatureFlagService,+ );++ beforeEach(() => {+ jest.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined);+ upsertWorkspaceFeatureFlag.mockReset();+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ it('rejects unknown feature flag keys', () => {+ expect(() => command.parseFlag('NOT_A_FLAG')).toThrow();+ expect(command.parseFlag(featureFlagKey)).toBe(featureFlagKey);+ });++ it('only accepts true or false as value', () => {+ expect(command.parseValue('true')).toBe(true);+ expect(command.parseValue('false')).toBe(false);+ expect(() => command.parseValue('yes')).toThrow();+ });++ it('upserts the feature flag on the workspace', async () => {+ await command.runOnWorkspace({+ workspaceId: 'workspace-id',+ options: { flag: featureFlagKey, value: false },+ index: 0,+ total: 1,+ });++ expect(upsertWorkspaceFeatureFlag).toHaveBeenCalledTimes(1);+ expect(upsertWorkspaceFeatureFlag).toHaveBeenCalledWith({+ workspaceId: 'workspace-id',+ featureFlag: featureFlagKey,+ value: false,+ });+ });++ it('does not write on dry run', async () => {+ await command.runOnWorkspace({+ workspaceId: 'workspace-id',+ options: { flag: featureFlagKey, value: true, dryRun: true },+ index: 0,+ total: 1,+ });++ expect(upsertWorkspaceFeatureFlag).not.toHaveBeenCalled();+ });+});diff --git a/packages/twenty-server/src/database/commands/database-command.module.ts b/packages/twenty-server/src/database/commands/database-command.module.tsindex 04f1c57b014..885711799fb 100644--- a/packages/twenty-server/src/database/commands/database-command.module.ts+++ b/packages/twenty-server/src/database/commands/database-command.module.ts@@ -13,6 +13,7 @@ import { ListOrphanedWorkspaceEntitiesCommand } from 'src/database/commands/listimport { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question';import { RebuildApplicationDefaultDepsCommand } from 'src/database/commands/rebuild-application-default-deps.command';import { RunInstanceCommandsCommand } from 'src/database/commands/run-instance-commands.command';+import { UpdateFeatureFlagCommand } from 'src/database/commands/update-feature-flag.command';import { UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/upgrade-version-command.module';import { WorkspaceExportModule } from 'src/database/commands/workspace-export/workspace-export.module';import { TypeORMModule } from 'src/database/typeorm/typeorm.module';@@ -126,6 +127,7 @@ import { AutomatedTriggerModule } from 'src/modules/workflow/workflow-trigger/auUpgradeStatusCommand,RebuildApplicationDefaultDepsCommand,InstallPreInstalledAppsCommand,+ UpdateFeatureFlagCommand,provideWorkspaceScopedRepository(RoleEntity),],})diff --git a/packages/twenty-server/src/database/commands/update-feature-flag.command.ts b/packages/twenty-server/src/database/commands/update-feature-flag.command.tsnew file mode 100644index 00000000000..111e2f1e696--- /dev/null+++ b/packages/twenty-server/src/database/commands/update-feature-flag.command.ts@@ -0,0 +1,91 @@+import { Command, Option } from 'nest-commander';+import { type FeatureFlagKey } from 'twenty-shared/types';+import { WorkspaceActivationStatus } from 'twenty-shared/workspace';++import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';+import {+ WorkspaceCommandRunner,+ type RunOnWorkspaceArgs,+ type WorkspaceCommandOptions,+} from 'src/database/commands/command-runners/workspace.command-runner';+import {+ FeatureFlagException,+ FeatureFlagExceptionCode,+} from 'src/engine/core-modules/feature-flag/feature-flag.exception';+import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';+import { featureFlagValidator } from 'src/engine/core-modules/feature-flag/validates/feature-flag.validate';++type UpdateFeatureFlagCommandOptions = WorkspaceCommandOptions & {+ flag: FeatureFlagKey;+ value: boolean;+};++@Command({+ name: 'feature-flag:update',+ description:+ 'Set a feature flag to a value on one workspace (-w), a batch (--workspace-count-limit, --start-from-workspace-id) or all workspaces',+})+export class UpdateFeatureFlagCommand extends WorkspaceCommandRunner<UpdateFeatureFlagCommandOptions> {+ constructor(+ protected readonly workspaceIteratorService: WorkspaceIteratorService,+ private readonly featureFlagService: FeatureFlagService,+ ) {+ // Feature flags live in core, so they can be set regardless of workspace provisioning state+ super(workspaceIteratorService, Object.values(WorkspaceActivationStatus));+ }++ @Option({+ flags: '-f, --flag <flag>',+ description: 'Feature flag key, e.g. IS_AI_ENABLED',+ required: true,+ })+ parseFlag(val: string): FeatureFlagKey {+ featureFlagValidator.assertIsFeatureFlagKey(+ val,+ new FeatureFlagException(+ `Invalid feature flag key: ${val}`,+ FeatureFlagExceptionCode.INVALID_FEATURE_FLAG_KEY,+ ),+ );++ return val;+ }++ @Option({+ flags: '--value <value>',+ description: 'Feature flag value: true or false',+ required: true,+ })+ parseValue(val: string): boolean {+ if (val !== 'true' && val !== 'false') {+ throw new Error(+ `Feature flag value must be "true" or "false", got ${val}`,+ );+ }++ return val === 'true';+ }++ override async runOnWorkspace({+ workspaceId,+ options,+ index,+ total,+ }: RunOnWorkspaceArgs<UpdateFeatureFlagCommandOptions>): Promise<void> {+ const { flag, value, dryRun } = options;++ this.logger.log(+ `${dryRun ? '[DRY RUN] ' : ''}Setting ${flag} to ${value} on workspace ${workspaceId} (${index + 1}/${total})`,+ );++ if (dryRun) {+ return;+ }++ await this.featureFlagService.upsertWorkspaceFeatureFlag({+ workspaceId,+ featureFlag: flag,+ value,+ });+ }+}