From d8dc971ada4c0a968049795f767559d5891803f0 Mon Sep 17 00:00:00 2001 From: Zack Lee Date: Tue, 6 Aug 2024 23:13:35 +0900 Subject: [PATCH 1/3] Update getKeys to respond 404 when /featureflag is requested without /init --- .../src/api/services/FeatureFlagService.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts b/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts index d87e7f023a..2fca81e15e 100644 --- a/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts +++ b/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts @@ -39,7 +39,21 @@ export class FeatureFlagService { } public async getKeys(experimentUserDoc: ExperimentUser, context: string, logger: UpgradeLogger): Promise { - logger.info({ message: 'Get all feature flags' }); + logger.info({ message: `Get all feature flag keys: User: ${experimentUserDoc?.id}` }); + + // throw error if user not defined + if (!experimentUserDoc || !experimentUserDoc.id) { + logger.error({ message: `User not defined in getAllExperimentConditions: ${experimentUserDoc?.id}` }); + const error = new Error( + JSON.stringify({ + type: SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED, + message: `User not defined in getAllExperimentConditions: ${experimentUserDoc?.id}`, + }) + ); + (error as any).type = SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED; + (error as any).httpCode = 404; + throw error; + } const filteredFeatureFlags = await this.featureFlagRepository.getFlagsFromContext(context); From 9e88c7beaa06dc678d4d2e906d91a63d527aa4e3 Mon Sep 17 00:00:00 2001 From: Zack Lee Date: Wed, 7 Aug 2024 18:06:01 +0900 Subject: [PATCH 2/3] Change the experimentUserDoc type to RequestedExperimentUser to use requestedUserId for consistency --- .../Upgrade/src/api/services/FeatureFlagService.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts b/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts index 2fca81e15e..8bc819efa8 100644 --- a/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts +++ b/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts @@ -22,6 +22,7 @@ import { ExperimentUser } from '../models/ExperimentUser'; import { ExperimentAssignmentService } from './ExperimentAssignmentService'; import { SegmentService } from './SegmentService'; import { ErrorWithType } from '../errors/ErrorWithType'; +import { RequestedExperimentUser } from '../controllers/validators/ExperimentUserValidator'; @Service() export class FeatureFlagService { @@ -38,16 +39,20 @@ export class FeatureFlagService { return this.featureFlagRepository.find(); } - public async getKeys(experimentUserDoc: ExperimentUser, context: string, logger: UpgradeLogger): Promise { - logger.info({ message: `Get all feature flag keys: User: ${experimentUserDoc?.id}` }); + public async getKeys( + experimentUserDoc: RequestedExperimentUser, + context: string, + logger: UpgradeLogger + ): Promise { + logger.info({ message: `Get all feature flag keys: User: ${experimentUserDoc?.requestedUserId}` }); // throw error if user not defined if (!experimentUserDoc || !experimentUserDoc.id) { - logger.error({ message: `User not defined in getAllExperimentConditions: ${experimentUserDoc?.id}` }); + logger.error({ message: `User not defined in getKeys: ${experimentUserDoc?.requestedUserId}` }); const error = new Error( JSON.stringify({ type: SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED, - message: `User not defined in getAllExperimentConditions: ${experimentUserDoc?.id}`, + message: `User not defined in getKeys: ${experimentUserDoc?.requestedUserId}`, }) ); (error as any).type = SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED; From 2abc3d0a3c3d37d17b21e21cfbc170e037e31220 Mon Sep 17 00:00:00 2001 From: Zack Lee Date: Sat, 10 Aug 2024 00:10:01 +0900 Subject: [PATCH 3/3] Remove undefined experimentUserDoc?.requestedUserId from getKeys --- .../packages/Upgrade/src/api/services/FeatureFlagService.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts b/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts index 8bc819efa8..b5f6b0d52a 100644 --- a/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts +++ b/backend/packages/Upgrade/src/api/services/FeatureFlagService.ts @@ -44,15 +44,15 @@ export class FeatureFlagService { context: string, logger: UpgradeLogger ): Promise { - logger.info({ message: `Get all feature flag keys: User: ${experimentUserDoc?.requestedUserId}` }); + logger.info({ message: `getKeys: User: ${experimentUserDoc?.requestedUserId}` }); // throw error if user not defined if (!experimentUserDoc || !experimentUserDoc.id) { - logger.error({ message: `User not defined in getKeys: ${experimentUserDoc?.requestedUserId}` }); + logger.error({ message: 'User not defined in getKeys' }); const error = new Error( JSON.stringify({ type: SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED, - message: `User not defined in getKeys: ${experimentUserDoc?.requestedUserId}`, + message: 'User not defined in getKeys', }) ); (error as any).type = SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED;