Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClientLib changes for featureFlags to getKeys #1585

Merged
merged 18 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonController, Post, Body, UseBefore, Get, Req, InternalServerError, Delete } from 'routing-controllers';
import { JsonController, Post, Body, UseBefore, Req, InternalServerError, Delete } from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
import { MarkExperimentValidator } from './validators/MarkExperimentValidator';
Expand All @@ -10,7 +10,6 @@ import { MonitoredDecisionPoint } from '../models/MonitoredDecisionPoint';
import { SERVER_ERROR, PAYLOAD_TYPE } from 'upgrade_types';
import { FailedParamsValidator } from './validators/FailedParamsValidator';
import { ExperimentError } from '../models/ExperimentError';
import { FeatureFlag } from '../models/FeatureFlag';
import { FeatureFlagService } from '../services/FeatureFlagService';
import { ClientLibMiddleware } from '../middlewares/ClientLibMiddleware';
import { LogValidator } from './validators/LogValidator';
Expand Down Expand Up @@ -828,8 +827,24 @@ export class ExperimentClientController {
/**
* @swagger
* /featureflag:
* get:
* post:
* description: Get all feature flags using SDK
* consumes:
* - application/json
* parameters:
* - in: body
* name: user
* required: true
* schema:
* type: object
* properties:
* userId:
* type: string
* example: user1
* context:
* type: string
* example: add
* description: User Document
* produces:
* - application/json
* tags:
Expand All @@ -838,9 +853,14 @@ export class ExperimentClientController {
* '200':
* description: Feature flags list
*/
@Get('featureflag')
public getAllFlags(@Req() request: AppRequest): Promise<FeatureFlag[]> {
return this.featureFlagService.find(request.logger);
@Post('featureflag')
public async getAllFlags(
@Req() request: AppRequest,
@Body({ validate: true })
experiment: ExperimentAssignmentValidator
): Promise<string[]> {
const experimentUserDoc = await this.experimentUserService.getUserDoc(experiment.userId, request.logger);
return this.featureFlagService.getKeys(experimentUserDoc, experiment.context, request.logger);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return 404 when the user is not found, like with experiment assignment (and the removed 'keys' endpoint).


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Post,
Body,
UseBefore,
Get,
Req,
InternalServerError,
Delete,
Expand All @@ -20,7 +19,6 @@ import { UpdateWorkingGroupValidator } from './validators/UpdateWorkingGroupVali
import { SERVER_ERROR, IGroupMembership, IUserAliases, IWorkingGroup } from 'upgrade_types';
import { FailedParamsValidator } from './validators/FailedParamsValidator.v1';
import { ExperimentError } from '../models/ExperimentError';
import { FeatureFlag } from '../models/FeatureFlag';
import { FeatureFlagService } from '../services/FeatureFlagService';
import { ClientLibMiddleware } from '../middlewares/ClientLibMiddleware';
import { LogValidator } from './validators/LogValidator';
Expand Down Expand Up @@ -795,8 +793,24 @@ export class ExperimentClientController {
/**
* @swagger
* /featureflag:
* get:
* post:
* description: Get all feature flags using SDK
* consumes:
* - application/json
* parameters:
* - in: body
* name: user
* required: true
* schema:
* type: object
* properties:
* userId:
* type: string
* example: user1
* context:
* type: string
* example: add
* description: User Document
* produces:
* - application/json
* tags:
Expand All @@ -805,9 +819,14 @@ export class ExperimentClientController {
* '200':
* description: Feature flags list
*/
@Get('featureflag')
public getAllFlags(@Req() request: AppRequest): Promise<FeatureFlag[]> {
return this.featureFlagService.find(request.logger);
@Post('featureflag')
public async getAllFlags(
@Req() request: AppRequest,
@Body({ validate: true })
experiment: ExperimentAssignmentValidator
): Promise<string[]> {
const experimentUserDoc = await this.experimentUserService.getUserDoc(experiment.userId, request.logger);
return this.featureFlagService.getKeys(experimentUserDoc, experiment.context, request.logger);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Post,
Body,
UseBefore,
Get,
Req,
InternalServerError,
Delete,
Expand All @@ -25,7 +24,6 @@ import {
PAYLOAD_TYPE,
IPayload,
} from 'upgrade_types';
import { FeatureFlag } from '../models/FeatureFlag';
import { FeatureFlagService } from '../services/FeatureFlagService';
import { ClientLibMiddleware } from '../middlewares/ClientLibMiddleware';
import { LogValidator } from './validators/LogValidator';
Expand Down Expand Up @@ -757,8 +755,24 @@ export class ExperimentClientController {
/**
* @swagger
* /featureflag:
* get:
* post:
* description: Get all feature flags using SDK
* consumes:
* - application/json
* parameters:
* - in: body
* name: user
* required: true
* schema:
* type: object
* properties:
* userId:
* type: string
* example: user1
* context:
* type: string
* example: add
* description: User Document
* produces:
* - application/json
* tags:
Expand All @@ -767,9 +781,14 @@ export class ExperimentClientController {
* '200':
* description: Feature flags list
*/
@Get('featureflag')
public getAllFlags(@Req() request: AppRequest): Promise<FeatureFlag[]> {
return this.featureFlagService.find(request.logger);
@Post('featureflag')
public async getAllFlags(
@Req() request: AppRequest,
@Body({ validate: true })
experiment: ExperimentAssignmentValidator
): Promise<string[]> {
const experimentUserDoc = await this.experimentUserService.getUserDoc(experiment.userId, request.logger);
return this.featureFlagService.getKeys(experimentUserDoc, experiment.context, request.logger);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Post,
Body,
UseBefore,
Get,
Req,
InternalServerError,
Delete,
Expand All @@ -25,7 +24,6 @@ import {
PAYLOAD_TYPE,
IPayload,
} from 'upgrade_types';
import { FeatureFlag } from '../models/FeatureFlag';
import { FeatureFlagService } from '../services/FeatureFlagService';
import { ClientLibMiddleware } from '../middlewares/ClientLibMiddleware';
import { LogValidator } from './validators/LogValidator';
Expand Down Expand Up @@ -714,8 +712,24 @@ export class ExperimentClientController {
/**
* @swagger
* /featureflag:
* get:
* post:
* description: Get all feature flags using SDK
* consumes:
* - application/json
* parameters:
* - in: body
* name: user
* required: true
* schema:
* type: object
* properties:
* userId:
* type: string
* example: user1
* context:
* type: string
* example: add
* description: User Document
* produces:
* - application/json
* tags:
Expand All @@ -724,9 +738,14 @@ export class ExperimentClientController {
* '200':
* description: Feature flags list
*/
@Get('featureflag')
public getAllFlags(@Req() request: AppRequest): Promise<FeatureFlag[]> {
return this.featureFlagService.find(request.logger);
@Post('featureflag')
public async getAllFlags(
@Req() request: AppRequest,
@Body({ validate: true })
experiment: ExperimentAssignmentValidator
): Promise<string[]> {
const experimentUserDoc = await this.experimentUserService.getUserDoc(experiment.userId, request.logger);
return this.featureFlagService.getKeys(experimentUserDoc, experiment.context, request.logger);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { FeatureFlagStatusUpdateValidator } from './validators/FeatureFlagStatus
import { FeatureFlagPaginatedParamsValidator } from './validators/FeatureFlagsPaginatedParamsValidator';
import { AppRequest, PaginationResponse } from '../../types';
import { SERVER_ERROR } from 'upgrade_types';
import { FeatureFlagValidation, UserParamsValidator } from './validators/FeatureFlagValidator';
import { ExperimentUserService } from '../services/ExperimentUserService';
import { isUUID } from 'class-validator';
import { FeatureFlagValidation } from './validators/FeatureFlagValidator';

interface FeatureFlagsPaginationInfo extends PaginationResponse {
nodes: FeatureFlag[];
Expand Down Expand Up @@ -134,7 +135,7 @@ interface FeatureFlagsPaginationInfo extends PaginationResponse {
@Authorized()
@JsonController('/flags')
export class FeatureFlagsController {
constructor(public featureFlagService: FeatureFlagService) {}
constructor(public featureFlagService: FeatureFlagService, public experimentUserService: ExperimentUserService) {}

/**
* @swagger
Expand All @@ -161,6 +162,59 @@ export class FeatureFlagsController {
return this.featureFlagService.find(request.logger);
}

/**
* @swagger
* /flags:
* post:
* description: Get feature flags for user
* consumes:
* - application/json
* parameters:
* - in: body
* name: user
* required: true
* schema:
* type: object
* properties:
* userId:
* type: string
* example: user1
* context:
* type: string
* example: add
* description: User Document
* tags:
* - Feature Flags
* produces:
* - application/json
* responses:
* '200':
* description: Feature Flag List
* schema:
* type: array
* items:
* $ref: '#/definitions/FeatureFlag'
* '401':
* description: AuthorizationRequiredError
*/

@Post('/keys')
public async getKeys(
@Body({ validate: true })
userParams: UserParamsValidator,
@Req() request: AppRequest
): Promise<string[]> {
const experimentUserDoc = await this.experimentUserService.getUserDoc(userParams.userId, request.logger);
if (!experimentUserDoc) {
const error = new Error(`User not defined in markExperimentPoint: ${userParams.userId}`);
(error as any).type = SERVER_ERROR.EXPERIMENT_USER_NOT_DEFINED;
(error as any).httpCode = 404;
request.logger.error(error);
throw error;
}
return this.featureFlagService.getKeys(experimentUserDoc, userParams.context, request.logger);
}

/**
* @swagger
* /flags/{id}:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IsNotEmpty, IsDefined, IsString, IsArray, IsEnum, IsOptional, ValidateNested } from 'class-validator';
import { ParticipantsValidator } from '../../DTO/ExperimentDTO';
import { Column } from 'typeorm';
import { FILTER_MODE } from 'upgrade_types';
import { FEATURE_FLAG_STATUS } from 'upgrade_types';
import { Type } from 'class-transformer';
Expand Down Expand Up @@ -29,14 +30,13 @@ export class FeatureFlagValidation {
status: FEATURE_FLAG_STATUS;

@IsNotEmpty()
@Column('text', { array: true })
public context: string[];

@IsDefined()
@IsEnum(FILTER_MODE)
filterMode: FILTER_MODE;

@IsNotEmpty()
@IsArray()
public context: string[];

@IsNotEmpty()
@IsArray()
public tags: string[];
Expand All @@ -51,3 +51,15 @@ export class FeatureFlagValidation {
@Type(() => ParticipantsValidator)
public featureFlagSegmentExclusion: ParticipantsValidator;
}

export class UserParamsValidator {
@IsNotEmpty()
@IsDefined()
@IsString()
public userId: string;

@IsNotEmpty()
@IsDefined()
@IsString()
public context: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,27 @@ export class FeatureFlagRepository extends Repository<FeatureFlag> {

return result.raw;
}

public async getFlagsFromContext(context: string): Promise<FeatureFlag[]> {
const result = await this.createQueryBuilder('feature_flag')
.leftJoinAndSelect('feature_flag.featureFlagSegmentInclusion', 'featureFlagSegmentInclusion')
.leftJoinAndSelect('featureFlagSegmentInclusion.segment', 'segmentInclusion')
.leftJoinAndSelect('segmentInclusion.individualForSegment', 'individualForSegment')
.leftJoinAndSelect('segmentInclusion.groupForSegment', 'groupForSegment')
.leftJoinAndSelect('segmentInclusion.subSegments', 'subSegment')
.leftJoinAndSelect('feature_flag.featureFlagSegmentExclusion', 'featureFlagSegmentExclusion')
.leftJoinAndSelect('featureFlagSegmentExclusion.segment', 'segmentExclusion')
.leftJoinAndSelect('segmentExclusion.individualForSegment', 'individualForSegmentExclusion')
.leftJoinAndSelect('segmentExclusion.groupForSegment', 'groupForSegmentExclusion')
.leftJoinAndSelect('segmentExclusion.subSegments', 'subSegmentExclusion')
.where('feature_flag.context @> :searchContext', { searchContext: [context] })
.andWhere('feature_flag.status = :status', { status: FEATURE_FLAG_STATUS.ENABLED })
.getMany()
.catch((errorMsg: any) => {
const errorMsgString = repositoryError('FeatureFlagRepository', 'getFlagsFromContext', { context }, errorMsg);
throw errorMsgString;
});

return result;
}
}
Loading
Loading