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

Feature Flag backend completion #1582

Merged
merged 22 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
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')
RidhamShah marked this conversation as resolved.
Show resolved Hide resolved
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}`);
VivekFitkariwala marked this conversation as resolved.
Show resolved Hide resolved
(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 @@ -30,14 +31,13 @@ export class FeatureFlagValidation {
status: FEATURE_FLAG_STATUS;

@IsNotEmpty()
@Column('text', { array: true })
VivekFitkariwala marked this conversation as resolved.
Show resolved Hide resolved
public context: string[];

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

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

@IsNotEmpty()
@IsArray()
public tags: string[];
Expand All @@ -52,3 +52,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