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 5 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
255 changes: 218 additions & 37 deletions backend/packages/Upgrade/src/api/controllers/FeatureFlagController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { JsonController, Authorized, Post, Body, CurrentUser, Delete, Param, Put, Req } from 'routing-controllers';
import { JsonController, Authorized, Post, Body, CurrentUser, Delete, Param, Put, Req, Get } from 'routing-controllers';
import { FeatureFlagService } from '../services/FeatureFlagService';
import { FeatureFlag } from '../models/FeatureFlag';
import { User } from '../models/User';
import { FeatureFlagStatusUpdateValidator } from './validators/FeatureFlagStatusUpdateValidator';
import { FeatureFlagPaginatedParamsValidator } from './validators/FeatureFlagsPaginatedParamsValidator';
import { AppRequest, PaginationResponse } from '../../types';
import { SERVER_ERROR } from 'upgrade_types';
import { FeatureFlagValidation } from './validators/FeatureFlagValidator';
import { FeatureFlagValidation, UserParamsValidator } from './validators/FeatureFlagValidator';
import { ExperimentUserService } from '../services/ExperimentUserService';

interface FeatureFlagsPaginationInfo extends PaginationResponse {
nodes: FeatureFlag[];
Expand All @@ -21,9 +22,8 @@ interface FeatureFlagsPaginationInfo extends PaginationResponse {
* - name
* - key
* - description
* - variationType
* - status
* - variations
* - context
* properties:
* id:
* type: string
Expand All @@ -33,36 +33,214 @@ interface FeatureFlagsPaginationInfo extends PaginationResponse {
* type: string
* description:
* type: string
* variationType:
* type: string
* status:
* type: boolean
* variations:
* type: array
* items:
* type: object
* properties:
* value:
* type: string
* name:
* type: string
* description:
* type: string
* defaultVariation:
* type: boolean[]
* type: string
* enum: [archived, enabled, disabled]
* context:
* type: array
* items:
* type: string
* tags:
* type: array
* items:
* type: string
* featureFlagSegmentInclusion:
* type: object
* properties:
* segment:
* type: object
* properties:
* type:
* type: string
* example: private
* individualForSegment:
* type: array
* items:
* type: object
* properties:
* userId:
* type: string
* example: user1
* groupForSegment:
* type: array
* items:
* type: object
* properties:
* groupId:
* type: string
* example: school1
* type:
* type: string
* example: schoolId
* subSegments:
* type: array
* items:
* type: object
* properties:
* id:
* type: string
* name:
* type: string
* context:
* type: string
* featureFlagSegmentExclusion:
* type: object
* properties:
* segment:
* type: object
* properties:
* type:
* type: string
* example: private
* individualForSegment:
* type: array
* items:
* type: object
* properties:
* userId:
* type: string
* example: user1
* groupForSegment:
* type: array
* items:
* type: object
* properties:
* groupId:
* type: string
* example: school1
* type:
* type: string
* example: schoolId
* subSegments:
* type: array
* items:
* type: object
* properties:
* id:
* type: string
* name:
* type: string
* context:
* type: string
*/

/**
* @swagger
* flags:
* - name: Feature flags
* - name: Feature Flags
* description: Get Feature flags related data
*/

@Authorized()
@JsonController('/flags')
export class FeatureFlagsController {
constructor(public featureFlagService: FeatureFlagService) {}
constructor(public featureFlagService: FeatureFlagService, public experimentUserService: ExperimentUserService) {}

/**
* @swagger
* /flags:
* get:
* description: Get all the feature flags
* tags:
* - Feature Flags
* produces:
* - application/json
* responses:
* '200':
* description: Feature Flag List
* schema:
* type: array
* items:
* $ref: '#/definitions/FeatureFlag'
* '401':
* description: AuthorizationRequiredError
*/

@Get()
public find(@Req() request: AppRequest): Promise<FeatureFlag[]> {
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) {
throw new Error('User not found');
RidhamShah marked this conversation as resolved.
Show resolved Hide resolved
}
return this.featureFlagService.getKeys(experimentUserDoc, userParams.context, request.logger);
}
/**
* @swagger
* /flags/{id}:
* get:
* description: Get feature flag by id
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: Feature Flag Id
* tags:
* - Feature Flags
* produces:
* - application/json
* responses:
* '200':
* description: Get Feature Flag By Id
* schema:
* $ref: '#/definitions/FeatureFlag'
* '401':
* description: AuthorizationRequiredError
* '404':
* description: Feature Flag not found
* '500':
* description: id should be of type UUID
*/
@Get('/:id')
public findOne(@Param('id') id: string, @Req() request: AppRequest): Promise<FeatureFlag | undefined> {
return this.featureFlagService.findOne(id, request.logger);
}

/**
* @swagger
Expand Down Expand Up @@ -103,13 +281,14 @@ export class FeatureFlagsController {
* type: string
* enum: [ASC, DESC]
* tags:
* - Feature flags
* - Feature Flags
* produces:
* - application/json
* responses:
* '200':
* description: Get Paginated Experiments
*/

@Post('/paginated')
public async paginatedFind(
@Body({ validate: true })
Expand Down Expand Up @@ -157,7 +336,7 @@ export class FeatureFlagsController {
* $ref: '#/definitions/FeatureFlag'
* description: Feature flag structure
* tags:
* - Feature flags
* - Feature Flags
* produces:
* - application/json
* responses:
Expand All @@ -182,19 +361,21 @@ export class FeatureFlagsController {
* - application/json
* parameters:
* - in: body
* name: flagId
* required: true
* name: statusUpdate
* description: Updating the featur flag's status
* schema:
* type: string
* description: Flag ID
* - in: body
* name: status
* required: true
* schema:
* type: boolean
* description: Flag State
* type: object
* required:
* - flagId
* - status
* properties:
* flagId:
* type: string
* status:
* type: string
* enum: [archived, enabled, disabled]
* tags:
* - Feature flags
* - Feature Flags
* produces:
* - application/json
* responses:
Expand Down Expand Up @@ -222,7 +403,7 @@ export class FeatureFlagsController {
* type: string
* description: Feature flag Id
* tags:
* - Feature flags
* - Feature Flags
* produces:
* - application/json
* responses:
Expand Down Expand Up @@ -265,7 +446,7 @@ export class FeatureFlagsController {
* $ref: '#/definitions/FeatureFlag'
* description: Feature Flag Structure
* tags:
* - Feature flags
* - Feature Flags
* produces:
* - application/json
* responses:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { IsNotEmpty, IsDefined, IsString, IsArray, IsEnum } from 'class-validator';
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';

export class FeatureFlagValidation {
@IsNotEmpty()
@IsDefined()
@IsOptional()
@IsString()
id: string;

Expand All @@ -29,15 +31,36 @@ 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[];
public tags: string[];

@IsNotEmpty()
@IsArray()
public tags: string[];
@ValidateNested()
@Type(() => ParticipantsValidator)
public featureFlagSegmentInclusion: ParticipantsValidator;

@IsNotEmpty()
@ValidateNested()
@Type(() => ParticipantsValidator)
public featureFlagSegmentExclusion: ParticipantsValidator;
}

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

@IsNotEmpty()
@IsDefined()
@IsString()
public context: string;
}
Loading