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 admin endpoints #1474

Merged
merged 10 commits into from
May 21, 2024
206 changes: 169 additions & 37 deletions backend/packages/Upgrade/src/api/controllers/FeatureFlagController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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';
Expand All @@ -21,9 +21,8 @@ interface FeatureFlagsPaginationInfo extends PaginationResponse {
* - name
* - key
* - description
* - variationType
* - status
* - variations
* - context
* properties:
* id:
* type: string
Expand All @@ -33,29 +32,101 @@ 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
*/

Expand All @@ -64,6 +135,64 @@ interface FeatureFlagsPaginationInfo extends PaginationResponse {
export class FeatureFlagsController {
constructor(public featureFlagService: FeatureFlagService) {}

/**
* @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/{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
danoswaltCL marked this conversation as resolved.
Show resolved Hide resolved
*/
@Get('/:id')
public findOne(@Param('id') id: string, @Req() request: AppRequest): Promise<FeatureFlag | undefined> {
return this.featureFlagService.findOne(id, request.logger);
}

/**
* @swagger
* /flags/paginated:
Expand All @@ -90,26 +219,27 @@ export class FeatureFlagsController {
* properties:
* key:
* type: string
* enum: [all, name, key, status, variation Type]
* enum: [all, name, key, status, tag, context]
* string:
* type: string
* sortParams:
* type: object
* properties:
* key:
* type: string
* enum: [name, key, status, variationType]
* enum: [name, key, status, updatedAt]
* sortAs:
* type: string
* enum: [ASC, DESC]
* tags:
* - Feature flags
* - Feature Flags
* produces:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line 240 says Experiments instead of Feature Flags

* - application/json
* responses:
* '200':
* description: Get Paginated Experiments
*/

@Post('/paginated')
public async paginatedFind(
@Body({ validate: true })
Expand Down Expand Up @@ -157,7 +287,7 @@ export class FeatureFlagsController {
* $ref: '#/definitions/FeatureFlag'
* description: Feature flag structure
* tags:
* - Feature flags
* - Feature Flags
* produces:
* - application/json
* responses:
Expand All @@ -182,19 +312,21 @@ export class FeatureFlagsController {
* - application/json
* parameters:
* - in: body
* name: flagId
* required: true
* schema:
* type: string
* description: Flag ID
* - in: body
* name: status
* required: true
* name: statusUpdate
* description: Updating the featur flag's status
* 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 +354,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 +397,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,6 +31,7 @@ export class FeatureFlagValidation {
status: FEATURE_FLAG_STATUS;

@IsNotEmpty()
@Column('text', { array: true })
@IsDefined()
VivekFitkariwala marked this conversation as resolved.
Show resolved Hide resolved
@IsEnum(FILTER_MODE)
filterMode: FILTER_MODE;
Expand All @@ -40,4 +43,14 @@ export class FeatureFlagValidation {
@IsNotEmpty()
@IsArray()
public tags: string[];

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

@IsNotEmpty()
@ValidateNested()
@Type(() => ParticipantsValidator)
public featureFlagSegmentExclusion: ParticipantsValidator;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@ import { SORT_AS_DIRECTION } from 'upgrade_types';

// TODO: Move to upgrade types
export interface IFeatureFlagSearchParams {
key: FLAG_SEARCH_SORT_KEY;
key: FLAG_SEARCH_KEY;
string: string;
}
export interface IFeatureFlagSortParams {
key: FLAG_SEARCH_SORT_KEY;
key: FLAG_SORT_KEY;
sortAs: SORT_AS_DIRECTION;
}

export enum FLAG_SEARCH_SORT_KEY {
export enum FLAG_SORT_KEY {
NAME = 'name',
KEY = 'key',
STATUS = 'status',
UPDATED_AT = 'updatedAt',
}

export enum FLAG_SEARCH_KEY {
ALL = 'all',
NAME = 'name',
KEY = 'key',
STATUS = 'status',
VARIATION_TYPE = 'variationType',
TAG = 'tag',
CONTEXT = 'context',
}

class IFeatureFlagSortParamsValidator {
@IsNotEmpty()
@IsEnum(FLAG_SEARCH_SORT_KEY)
key: FLAG_SEARCH_SORT_KEY;
@IsEnum(FLAG_SORT_KEY)
key: FLAG_SORT_KEY;

@IsNotEmpty()
@IsEnum(SORT_AS_DIRECTION)
Expand All @@ -32,8 +40,8 @@ class IFeatureFlagSortParamsValidator {

class IFeatureFlagSearchParamsValidator {
@IsNotEmpty()
@IsEnum(FLAG_SEARCH_SORT_KEY)
key: FLAG_SEARCH_SORT_KEY;
@IsEnum(FLAG_SEARCH_KEY)
key: FLAG_SEARCH_KEY;

@IsNotEmpty()
@IsString()
Expand Down
Loading
Loading