Skip to content

Commit

Permalink
Use upsert and move method to service
Browse files Browse the repository at this point in the history
  • Loading branch information
ijreilly committed Oct 7, 2024
1 parent db3ded5 commit e0f93f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,17 @@ export class FeatureFlagService {

return workspaceFeatureFlagsMap;
}

public async enableFeatureFlags(
keys: FeatureFlagKey[],
workspaceId: string,
): Promise<void> {
await this.featureFlagRepository.upsert(
keys.map((key) => ({ workspaceId, key, value: true })),
{
conflictPaths: ['workspaceId', 'key'],
skipUpdateIfNoValuesChanged: true,
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,11 +28,10 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
private readonly workspaceRepository: Repository<Workspace>,
@InjectRepository(User, 'core')
private readonly userRepository: Repository<User>,
@InjectRepository(FeatureFlagEntity, 'core')
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
@InjectRepository(UserWorkspace, 'core')
private readonly userWorkspaceRepository: Repository<UserWorkspace>,
private readonly workspaceManagerService: WorkspaceManagerService,
private readonly featureFlagService: FeatureFlagService,
private readonly billingSubscriptionService: BillingSubscriptionService,
private moduleRef: ModuleRef,
) {
Expand Down Expand Up @@ -73,7 +72,10 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
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(
Expand Down Expand Up @@ -158,16 +160,4 @@ export class WorkspaceService extends TypeOrmQueryService<Workspace> {
);
}
}

private async enableDefaultFeatureFlags(workspaceId: string) {
await this.featureFlagRepository.save(
DEFAULT_FEATURE_FLAGS.map((featureFlagKey) => {
return {
key: featureFlagKey,
value: true,
workspaceId,
};
}),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand All @@ -182,15 +179,4 @@ export class WorkspaceSyncMetadataService {
storage,
};
}

private async enableFeatureFlag(
featureFlagKey: FeatureFlagKey,
workspaceId: string,
) {
await this.featureFlagRepository.save({
key: featureFlagKey,
workspaceId,
value: true,
});
}
}

0 comments on commit e0f93f0

Please sign in to comment.