From b4c1c7fa2dd8d5193414deb65d5d5b96f47b9263 Mon Sep 17 00:00:00 2001 From: p-fernandez Date: Mon, 2 Oct 2023 17:20:50 +0200 Subject: [PATCH] feat(infra): translation manager feature flag --- apps/api/src/.env.development | 1 + apps/api/src/.env.production | 1 + apps/api/src/.env.test | 1 + apps/api/src/.example.env | 1 + apps/web/src/config/index.ts | 4 ++ apps/web/src/hooks/useFeatureFlags.ts | 11 +++++ libs/shared/src/types/feature-flags/index.ts | 5 ++- .../get-feature-flag/get-feature-flag.test.ts | 42 +++++++++++++++++++ ...is-translation-manager-enabled.use-case.ts | 25 +++++++++++ .../src/usecases/get-feature-flag/index.ts | 1 + 10 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 packages/application-generic/src/usecases/get-feature-flag/get-is-translation-manager-enabled.use-case.ts diff --git a/apps/api/src/.env.development b/apps/api/src/.env.development index 8af859ce673a..5260aef4b5ee 100644 --- a/apps/api/src/.env.development +++ b/apps/api/src/.env.development @@ -48,6 +48,7 @@ VERCEL_BASE_URL=https://api.vercel.com FF_IS_TOPIC_NOTIFICATION_ENABLED=true FF_IS_DISTRIBUTED_LOCK_LOGGING_ENABLED=false +IS_TRANSLATION_MANAGER_ENABLED=false STORE_NOTIFICATION_CONTENT= diff --git a/apps/api/src/.env.production b/apps/api/src/.env.production index a52b412d515a..d59fc71ebdcf 100644 --- a/apps/api/src/.env.production +++ b/apps/api/src/.env.production @@ -35,6 +35,7 @@ VERCEL_BASE_URL=https://api.vercel.com FF_IS_TOPIC_NOTIFICATION_ENABLED=true FF_IS_DISTRIBUTED_LOCK_LOGGING_ENABLED=false +IS_TRANSLATION_MANAGER_ENABLED=false STORE_NOTIFICATION_CONTENT= diff --git a/apps/api/src/.env.test b/apps/api/src/.env.test index 5b43216c2986..7821bc05f2ec 100644 --- a/apps/api/src/.env.test +++ b/apps/api/src/.env.test @@ -75,6 +75,7 @@ VERCEL_BASE_URL=https://api.vercel.com FF_IS_TOPIC_NOTIFICATION_ENABLED=true IS_MULTI_PROVIDER_CONFIGURATION_ENABLED=true +IS_TRANSLATION_MANAGER_ENABLED=false STORE_NOTIFICATION_CONTENT=true diff --git a/apps/api/src/.example.env b/apps/api/src/.example.env index b70a633dab1f..82d28e7217d7 100644 --- a/apps/api/src/.example.env +++ b/apps/api/src/.example.env @@ -55,6 +55,7 @@ VERCEL_REDIRECT_URI=http://localhost:4200/auth/login VERCEL_BASE_URL=https://api.vercel.com FF_IS_TOPIC_NOTIFICATION_ENABLED=true +IS_TRANSLATION_MANAGER_ENABLED=false STORE_NOTIFICATION_CONTENT=true diff --git a/apps/web/src/config/index.ts b/apps/web/src/config/index.ts index 7fc49a1cc547..e30dd0e8919f 100644 --- a/apps/web/src/config/index.ts +++ b/apps/web/src/config/index.ts @@ -74,3 +74,7 @@ export const IS_MULTI_PROVIDER_CONFIGURATION_ENABLED = isCypress export const IS_MULTI_TENANCY_ENABLED = isCypress ? window._env_.IS_MULTI_TENANCY_ENABLED || process.env.IS_MULTI_TENANCY_ENABLED || 'true' : window._env_.IS_MULTI_TENANCY_ENABLED || process.env.IS_MULTI_TENANCY_ENABLED || 'false'; + +export const IS_TRANSLATION_MANAGER_ENABLED = isCypress + ? window._env_.IS_TRANSLATION_MANAGER_ENABLED || process.env.IS_TRANSLATION_MANAGER_ENABLED || 'true' + : window._env_.IS_TRANSLATION_MANAGER_ENABLED || process.env.IS_TRANSLATION_MANAGER_ENABLED || 'false'; diff --git a/apps/web/src/hooks/useFeatureFlags.ts b/apps/web/src/hooks/useFeatureFlags.ts index adce50bda956..a5eac1434370 100644 --- a/apps/web/src/hooks/useFeatureFlags.ts +++ b/apps/web/src/hooks/useFeatureFlags.ts @@ -5,6 +5,7 @@ import { IS_TEMPLATE_STORE_ENABLED, IS_MULTI_PROVIDER_CONFIGURATION_ENABLED, IS_MULTI_TENANCY_ENABLED, + IS_TRANSLATION_MANAGER_ENABLED, } from '../config'; const prepareBooleanStringFeatureFlag = (value: string | undefined, defaultValue: boolean): boolean => { @@ -50,3 +51,13 @@ export const useIsMultiTenancyEnabled = (): boolean => { return isMultiTenancyEnabled ?? defaultValue; }; + +export const useIsTranslationManagerEnabled = (): boolean => { + const value = IS_TRANSLATION_MANAGER_ENABLED; + const fallbackValue = false; + const defaultValue = prepareBooleanStringFeatureFlag(value, fallbackValue); + + const isTranslationManagerEnabled = useGetFlagByKey(FeatureFlagsKeysEnum.IS_TRANSLATION_MANAGER_ENABLED); + + return isTranslationManagerEnabled ?? defaultValue; +}; diff --git a/libs/shared/src/types/feature-flags/index.ts b/libs/shared/src/types/feature-flags/index.ts index 86197ab2b3cd..8b3a58bbf964 100644 --- a/libs/shared/src/types/feature-flags/index.ts +++ b/libs/shared/src/types/feature-flags/index.ts @@ -1,8 +1,9 @@ export enum FeatureFlagsKeysEnum { - IS_TEMPLATE_STORE_ENABLED = 'IS_TEMPLATE_STORE_ENABLED', - IS_TOPIC_NOTIFICATION_ENABLED = 'IS_TOPIC_NOTIFICATION_ENABLED', IS_MULTI_PROVIDER_CONFIGURATION_ENABLED = 'IS_MULTI_PROVIDER_CONFIGURATION_ENABLED', IS_MULTI_TENANCY_ENABLED = 'IS_MULTI_TENANCY_ENABLED', + IS_TEMPLATE_STORE_ENABLED = 'IS_TEMPLATE_STORE_ENABLED', + IS_TOPIC_NOTIFICATION_ENABLED = 'IS_TOPIC_NOTIFICATION_ENABLED', + IS_TRANSLATION_MANAGER_ENABLED = 'IS_TRANSLATION_MANAGER_ENABLED', } export enum SystemCriticalFlagsEnum { diff --git a/packages/application-generic/src/usecases/get-feature-flag/get-feature-flag.test.ts b/packages/application-generic/src/usecases/get-feature-flag/get-feature-flag.test.ts index ace804a050f5..a0992e268936 100644 --- a/packages/application-generic/src/usecases/get-feature-flag/get-feature-flag.test.ts +++ b/packages/application-generic/src/usecases/get-feature-flag/get-feature-flag.test.ts @@ -2,6 +2,7 @@ import { GetIsMultiProviderConfigurationEnabled, GetIsTemplateStoreEnabled, GetIsTopicNotificationEnabled, + GetIsTranslationManagerEnabled, } from './index'; import { FeatureFlagCommand } from './get-feature-flag.command'; import { FeatureFlagsService } from '../../services'; @@ -106,6 +107,32 @@ describe('Get Feature Flag', () => { expect(result).toEqual(false); }); }); + + describe('IS_TRANSLATION_MANAGER_ENABLED', () => { + it('should return default hardcoded value when no SDK env is set and no feature flag is set', async () => { + process.env.IS_TRANSLATION_MANAGER_ENABLED = ''; + + const getIsTranslationManagerEnabled = + new GetIsTranslationManagerEnabled(new FeatureFlagsService()); + + const result = await getIsTranslationManagerEnabled.execute( + featureFlagCommand + ); + expect(result).toEqual(true); + }); + + it('should return env variable value when no SDK env is set but the feature flag is set', async () => { + process.env.IS_TRANSLATION_MANAGER_ENABLED = 'false'; + + const getIsTranslationManagerEnabled = + new GetIsTranslationManagerEnabled(new FeatureFlagsService()); + + const result = await getIsTranslationManagerEnabled.execute( + featureFlagCommand + ); + expect(result).toEqual(false); + }); + }); }); describe('SDK key environment variable is set', () => { @@ -166,6 +193,21 @@ describe('Get Feature Flag', () => { expect(result).toEqual(true); }); }); + + describe('IS_TRANSLATION_MANAGER_ENABLED', () => { + it(`should get the feature flag value stored in Launch Darkly (enabled) + when the SDK key env variable is set regardless of the feature flag set`, async () => { + process.env.IS_TRANSLATION_MANAGER_ENABLED = 'false'; + + const getIsTranslationManagerEnabled = + new GetIsTranslationManagerEnabled(new FeatureFlagsService()); + + const result = await getIsTranslationManagerEnabled.execute( + featureFlagCommand + ); + expect(result).toEqual(true); + }); + }); }); }); }); diff --git a/packages/application-generic/src/usecases/get-feature-flag/get-is-translation-manager-enabled.use-case.ts b/packages/application-generic/src/usecases/get-feature-flag/get-is-translation-manager-enabled.use-case.ts new file mode 100644 index 000000000000..5dccf3211f0d --- /dev/null +++ b/packages/application-generic/src/usecases/get-feature-flag/get-is-translation-manager-enabled.use-case.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@nestjs/common'; +import { FeatureFlagsKeysEnum } from '@novu/shared'; + +import { + GetFeatureFlagCommand, + FeatureFlagCommand, +} from './get-feature-flag.command'; +import { GetFeatureFlag } from './get-feature-flag.use-case'; + +@Injectable() +export class GetIsTranslationManagerEnabled extends GetFeatureFlag { + async execute(featureFlagCommand: FeatureFlagCommand): Promise { + const value = process.env.IS_TRANSLATION_MANAGER_ENABLED; + const fallbackValue = false; + const defaultValue = this.prepareBooleanStringFeatureFlag( + value, + fallbackValue + ); + const key = FeatureFlagsKeysEnum.IS_TRANSLATION_MANAGER_ENABLED; + + const command = this.buildCommand(key, defaultValue, featureFlagCommand); + + return await this.featureFlagsService.getWithContext(command); + } +} diff --git a/packages/application-generic/src/usecases/get-feature-flag/index.ts b/packages/application-generic/src/usecases/get-feature-flag/index.ts index 7fba52b5841f..db2ffacd2058 100644 --- a/packages/application-generic/src/usecases/get-feature-flag/index.ts +++ b/packages/application-generic/src/usecases/get-feature-flag/index.ts @@ -7,3 +7,4 @@ export { GetIsInMemoryClusterModeEnabled } from './get-is-in-memory-cluster-mode export { GetIsMultiProviderConfigurationEnabled } from './get-is-multi-provider-configuration-enabled.use-case'; export { GetIsTemplateStoreEnabled } from './get-is-template-store-enabled.use-case'; export { GetIsTopicNotificationEnabled } from './get-is-topic-notification-enabled.use-case'; +export { GetIsTranslationManagerEnabled } from './get-is-translation-manager-enabled.use-case';