From 3f29fa5ec82b0af45cd06a9d906b607682da6f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Fri, 24 Mar 2023 10:43:38 +0100 Subject: [PATCH] refactor: fix compilation issues in features service (#3323) ## About the changes This PR fixes a bunch of null check errors: https://github.com/Unleash/unleash/actions/runs/4509279284/jobs/7938853559#step:5:39 --- src/lib/proxy/proxy-repository.ts | 2 +- src/lib/services/feature-toggle-service.ts | 53 +++++++++++----------- src/lib/services/index.ts | 2 +- src/lib/services/instance-stats-service.ts | 2 +- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/lib/proxy/proxy-repository.ts b/src/lib/proxy/proxy-repository.ts index 7555cd483f..a3c185c5e1 100644 --- a/src/lib/proxy/proxy-repository.ts +++ b/src/lib/proxy/proxy-repository.ts @@ -139,7 +139,7 @@ export class ProxyRepository private async segmentsForToken(): Promise { return mapSegmentsForClient( - await this.services.segmentService.getAll(), // TODO coupled with enterprise feature + await this.services.segmentService.getAll(), ); } diff --git a/src/lib/services/feature-toggle-service.ts b/src/lib/services/feature-toggle-service.ts index c3ae7a08dc..48fa442b5b 100644 --- a/src/lib/services/feature-toggle-service.ts +++ b/src/lib/services/feature-toggle-service.ts @@ -281,24 +281,20 @@ class FeatureToggleService { } if ( - oneOf( + contextDefinition && + contextDefinition.legalValues && + contextDefinition.legalValues.length > 0 + ) { + const valuesToValidate = oneOf( [...DATE_OPERATORS, ...SEMVER_OPERATORS, ...NUM_OPERATORS], operator, ) - ) { - if (contextDefinition?.legalValues?.length > 0) { - validateLegalValues( - contextDefinition.legalValues, - constraint.value, - ); - } - } else { - if (contextDefinition?.legalValues?.length > 0) { - validateLegalValues( - contextDefinition.legalValues, - constraint.values, - ); - } + ? constraint.value + : constraint.values; + validateLegalValues( + contextDefinition.legalValues, + valuesToValidate, + ); } return constraint; @@ -414,8 +410,8 @@ class FeatureToggleService { const newFeatureStrategy = await this.featureStrategiesStore.createStrategyFeatureEnv({ strategyName: strategyConfig.name, - constraints: strategyConfig.constraints, - parameters: strategyConfig.parameters, + constraints: strategyConfig.constraints || [], + parameters: strategyConfig.parameters || {}, sortOrder: strategyConfig.sortOrder, projectId, featureName, @@ -435,7 +431,7 @@ class FeatureToggleService { const tags = await this.tagStore.getAllTagsForFeature(featureName); const segments = await this.segmentService.getByStrategy( newFeatureStrategy.id, - ); // TODO coupled with enterprise feature + ); const strategy = this.featureStrategyToPublic( newFeatureStrategy, segments, @@ -521,7 +517,7 @@ class FeatureToggleService { const segments = await this.segmentService.getByStrategy( strategy.id, - ); // TODO coupled with enterprise feature + ); // Store event! const tags = await this.tagStore.getAllTagsForFeature(featureName); @@ -567,7 +563,7 @@ class FeatureToggleService { const tags = await this.tagStore.getAllTagsForFeature(featureName); const segments = await this.segmentService.getByStrategy( strategy.id, - ); // TODO coupled with enterprise feature + ); const data = this.featureStrategyToPublic(strategy, segments); const preData = this.featureStrategyToPublic( existingStrategy, @@ -661,12 +657,12 @@ class FeatureToggleService { featureName, environment, ); - const result = []; + const result: Saved[] = []; for (const strat of featureStrategies) { const segments = (await this.segmentService.getByStrategy(strat.id)).map( (segment) => segment.id, - ) ?? []; // TODO coupled with enterprise feature + ) ?? []; result.push({ id: strat.id, name: strat.strategyName, @@ -748,7 +744,7 @@ class FeatureToggleService { includeIds?: boolean, ): Promise { const result = await this.featureToggleClientStore.getClient( - query, + query || {}, includeIds, ); if (this.flagResolver.isEnabled('cleanClientApi')) { @@ -912,7 +908,11 @@ class FeatureToggleService { const strategyTasks = newToggle.environments.flatMap((e) => e.strategies.map((s) => { - if (replaceGroupId && s.parameters.hasOwnProperty('groupId')) { + if ( + replaceGroupId && + s.parameters && + s.parameters.hasOwnProperty('groupId') + ) { s.parameters.groupId = newFeatureName; } const context = { @@ -986,7 +986,7 @@ class FeatureToggleService { strategyId, ); - const segments = await this.segmentService.getByStrategy(strategyId); // TODO coupled with enterprise feature + const segments = await this.segmentService.getByStrategy(strategyId); let result: Saved = { id: strategy.id, name: strategy.strategyName, @@ -1580,7 +1580,8 @@ class FeatureToggleService { featureName, environment, }) - ).variants; + ).variants || + []; await this.eventStore.store( new EnvironmentVariantEvent({ diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index c4378a6345..ecaa092609 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -148,7 +148,7 @@ export const createServices = ( const versionService = new VersionService(stores, config); const healthService = new HealthService(stores, config); const userFeedbackService = new UserFeedbackService(stores, config); - const segmentService = new SegmentService(stores, config); // TODO coupled with enterprise feature + const segmentService = new SegmentService(stores, config); const featureToggleServiceV2 = new FeatureToggleService( stores, config, diff --git a/src/lib/services/instance-stats-service.ts b/src/lib/services/instance-stats-service.ts index ae23ecd67c..78f73c69e0 100644 --- a/src/lib/services/instance-stats-service.ts +++ b/src/lib/services/instance-stats-service.ts @@ -193,7 +193,7 @@ export class InstanceStatsService { this.groupStore.count(), this.roleStore.count(), this.environmentStore.count(), - this.segmentStore.count(), // TODO coupled with enterprise feature + this.segmentStore.count(), this.strategyStore.count(), this.hasSAML(), this.hasOIDC(),