diff --git a/packages/twenty-server/src/engine/core-modules/feature-flag/services/feature-flag.service.ts b/packages/twenty-server/src/engine/core-modules/feature-flag/services/feature-flag.service.ts index fc9c5038a2bd..cd2e4ca4d255 100644 --- a/packages/twenty-server/src/engine/core-modules/feature-flag/services/feature-flag.service.ts +++ b/packages/twenty-server/src/engine/core-modules/feature-flag/services/feature-flag.service.ts @@ -46,4 +46,17 @@ export class FeatureFlagService { return workspaceFeatureFlagsMap; } + + public async enableFeatureFlags( + keys: FeatureFlagKey[], + workspaceId: string, + ): Promise { + await this.featureFlagRepository.upsert( + keys.map((key) => ({ workspaceId, key, value: true })), + { + conflictPaths: ['workspaceId', 'key'], + skipUpdateIfNoValuesChanged: true, + }, + ); + } } diff --git a/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts b/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts index baf71345bb82..38befbf6e12d 100644 --- a/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts +++ b/packages/twenty-server/src/engine/core-modules/workspace/services/workspace.service.ts @@ -8,7 +8,7 @@ import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm'; import { Repository } from 'typeorm'; import { BillingSubscriptionService } from 'src/engine/core-modules/billing/services/billing-subscription.service'; -import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; +import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service'; import { UserWorkspace } from 'src/engine/core-modules/user-workspace/user-workspace.entity'; import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service'; import { User } from 'src/engine/core-modules/user/user.entity'; @@ -28,11 +28,10 @@ export class WorkspaceService extends TypeOrmQueryService { private readonly workspaceRepository: Repository, @InjectRepository(User, 'core') private readonly userRepository: Repository, - @InjectRepository(FeatureFlagEntity, 'core') - private readonly featureFlagRepository: Repository, @InjectRepository(UserWorkspace, 'core') private readonly userWorkspaceRepository: Repository, private readonly workspaceManagerService: WorkspaceManagerService, + private readonly featureFlagService: FeatureFlagService, private readonly billingSubscriptionService: BillingSubscriptionService, private moduleRef: ModuleRef, ) { @@ -73,7 +72,10 @@ export class WorkspaceService extends TypeOrmQueryService { activationStatus: WorkspaceActivationStatus.ONGOING_CREATION, }); - await this.enableDefaultFeatureFlags(user.defaultWorkspaceId); + await this.featureFlagService.enableFeatureFlags( + DEFAULT_FEATURE_FLAGS, + user.defaultWorkspaceId, + ); await this.workspaceManagerService.init(user.defaultWorkspaceId); await this.userWorkspaceService.createWorkspaceMember( @@ -158,16 +160,4 @@ export class WorkspaceService extends TypeOrmQueryService { ); } } - - private async enableDefaultFeatureFlags(workspaceId: string) { - await this.featureFlagRepository.save( - DEFAULT_FEATURE_FLAGS.map((featureFlagKey) => { - return { - key: featureFlagKey, - value: true, - workspaceId, - }; - }), - ); - } } diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.service.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.service.ts index e6f99cc5c703..c090d413aa18 100644 --- a/packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.service.ts +++ b/packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.service.ts @@ -154,12 +154,9 @@ export class WorkspaceSyncMetadataService { context.workspaceId, ); - if ( - workspaceFeatureFlagsMap.IS_SEARCH_ENABLED && - !workspaceFeatureFlagsMap.IS_WORKSPACE_MIGRATED_FOR_SEARCH - ) { - await this.enableFeatureFlag( - FeatureFlagKey.IsWorkspaceMigratedForSearch, + if (workspaceFeatureFlagsMap.IS_SEARCH_ENABLED) { + await this.featureFlagService.enableFeatureFlags( + [FeatureFlagKey.IsWorkspaceMigratedForSearch], context.workspaceId, ); } @@ -182,15 +179,4 @@ export class WorkspaceSyncMetadataService { storage, }; } - - private async enableFeatureFlag( - featureFlagKey: FeatureFlagKey, - workspaceId: string, - ) { - await this.featureFlagRepository.save({ - key: featureFlagKey, - workspaceId, - value: true, - }); - } }