Skip to content

Commit

Permalink
Merge branch 'dev' into code-cleanup/remove-experimentusercontroller-…
Browse files Browse the repository at this point in the history
…issue-2084
  • Loading branch information
ppratikcr7 committed Oct 30, 2024
2 parents 92188ff + 0186da0 commit 703bbd9
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 142 deletions.
7 changes: 7 additions & 0 deletions backend/packages/Upgrade/src/api/DTO/ExperimentDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IsObject,
IsOptional,
IsString,
IsUUID,
MaxLength,
MinLength,
ValidateIf,
Expand Down Expand Up @@ -458,6 +459,12 @@ export class ExperimentDTO {
public type: EXPERIMENT_TYPE;
}

export class ExperimentIdValidator {
@IsNotEmpty()
@IsUUID()
public id: string;
}

export interface ExperimentFile {
fileName: string;
fileContent: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class ExperimentClientController {
public metricService: MetricService
) {}

private async checkIfUserExist(
public async checkIfUserExist(
userId: string,
logger: UpgradeLogger,
api?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Get,
JsonController,
OnUndefined,
Param,
Post,
Put,
Delete,
Expand All @@ -17,8 +16,6 @@ import { Experiment } from '../models/Experiment';
import { ExperimentNotFoundError } from '../errors/ExperimentNotFoundError';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
import { SERVER_ERROR } from 'upgrade_types';
import { isUUID } from 'class-validator';
import { ExperimentCondition } from '../models/ExperimentCondition';
import { ExperimentPaginatedParamsValidator } from './validators/ExperimentPaginatedParamsValidator';
import { UserDTO } from '../DTO/UserDTO';
Expand All @@ -28,7 +25,7 @@ import { AppRequest, PaginationResponse } from '../../types';
import { ExperimentDTO, ExperimentFile, ValidatedExperimentError } from '../DTO/ExperimentDTO';
import { ExperimentIds } from './validators/ExperimentIdsValidator';
import { NotFoundException } from '@nestjs/common/exceptions';
import { IdValidator } from './validators/FeatureFlagValidator';
import { ExperimentIdValidator } from '../DTO/ExperimentDTO';

interface ExperimentPaginationInfo extends PaginationResponse {
nodes: Experiment[];
Expand Down Expand Up @@ -634,7 +631,7 @@ export class ExperimentController {
* description: AuthorizationRequiredError
*/
@Get()
public find(@Req() request: AppRequest): Promise<Experiment[]> {
public find(@Req() request: AppRequest): Promise<ExperimentDTO[]> {
return this.experimentService.find(request.logger);
}

Expand Down Expand Up @@ -743,14 +740,6 @@ export class ExperimentController {
@Req()
request: AppRequest
): Promise<ExperimentPaginationInfo> {
if (!paginatedParams) {
return Promise.reject(
new Error(
JSON.stringify({ type: SERVER_ERROR.MISSING_PARAMS, message: ' : paginatedParams should not be null.' })
)
);
}

const [experiments, count] = await Promise.all([
this.experimentService.findPaginated(
paginatedParams.skip,
Expand Down Expand Up @@ -841,7 +830,10 @@ export class ExperimentController {
*/
@Get('/single/:id')
@OnUndefined(ExperimentNotFoundError)
public one(@Params({ validate: true }) { id }: IdValidator, @Req() request: AppRequest): Promise<Experiment> {
public one(
@Params({ validate: true }) { id }: ExperimentIdValidator,
@Req() request: AppRequest
): Promise<ExperimentDTO> {
return this.experimentService.getSingleExperiment(id, request.logger);
}

Expand Down Expand Up @@ -920,7 +912,7 @@ export class ExperimentController {
@Get('/conditions/:id')
@OnUndefined(ExperimentNotFoundError)
public async getCondition(
@Params({ validate: true }) { id }: IdValidator,
@Params({ validate: true }) { id }: ExperimentIdValidator,
@Req() request: AppRequest
): Promise<ExperimentCondition[]> {
return this.experimentService.getExperimentalConditions(id, request.logger);
Expand Down Expand Up @@ -1042,7 +1034,7 @@ export class ExperimentController {

@Delete('/:id')
public async delete(
@Params({ validate: true }) { id }: IdValidator,
@Params({ validate: true }) { id }: ExperimentIdValidator,
@CurrentUser() currentUser: UserDTO,
@Req() request: AppRequest
): Promise<Experiment | undefined> {
Expand Down Expand Up @@ -1140,21 +1132,14 @@ export class ExperimentController {
*/
@Put('/:id')
public update(
@Param('id') id: string,
@Params({ validate: true }) { id }: ExperimentIdValidator,
@Body({ validate: true })
experiment: ExperimentDTO,
@CurrentUser() currentUser: UserDTO,
@Req() request: AppRequest
): Promise<Experiment> {
if (!isUUID(id)) {
return Promise.reject(
new Error(
JSON.stringify({ type: SERVER_ERROR.INCORRECT_PARAM_FORMAT, message: ' : id should be of type UUID.' })
)
);
}
): Promise<ExperimentDTO> {
request.logger.child({ user: currentUser });
return this.experimentService.update(experiment, currentUser, request.logger);
return this.experimentService.update({ ...experiment, id }, currentUser, request.logger);
}

/**
Expand Down Expand Up @@ -1345,7 +1330,7 @@ export class ExperimentController {
@Get('/getGroupAssignmentStatus/:id')
@OnUndefined(ExperimentNotFoundError)
public async getGroupAssignmentStatus(
@Params({ validate: true }) { id }: IdValidator,
@Params({ validate: true }) { id }: ExperimentIdValidator,
@Req() request: AppRequest
): Promise<number> | undefined {
return this.experimentAssignmentService.getGroupAssignmentStatus(id, request.logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,6 @@ export class FeatureFlagsController {
paginatedParams: FeatureFlagPaginatedParamsValidator,
@Req() request: AppRequest
): Promise<FeatureFlagsPaginationInfo> {
if (!paginatedParams) {
return Promise.reject(
new Error(
JSON.stringify({ type: SERVER_ERROR.MISSING_PARAMS, message: ' : paginatedParams should not be null.' })
)
);
}

const [featureFlags, count] = await Promise.all([
this.featureFlagService.findPaginated(
paginatedParams.skip,
Expand Down Expand Up @@ -453,7 +445,7 @@ export class FeatureFlagsController {
@CurrentUser() currentUser: UserDTO,
@Req() request: AppRequest
): Promise<FeatureFlag> {
return this.featureFlagService.update(flag, currentUser, request.logger);
return this.featureFlagService.update({ ...flag, id }, currentUser, request.logger);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Authorized, JsonController, Get, Delete, Param, Post, Req, Body, Params } from 'routing-controllers';
import { Authorized, JsonController, Get, Delete, Post, Req, Body, Params } from 'routing-controllers';
import { MetricService } from '../services/MetricService';
import { IMetricUnit } from 'upgrade_types';
import { AppRequest } from '../../types';
import { MetricKeyValidator, MetricValidator } from './validators/MetricValidator';
import { ContextValidator, MetricKeyValidator, MetricValidator } from './validators/MetricValidator';

/**
* @swagger
Expand Down Expand Up @@ -56,7 +56,10 @@ export class MetricController {
* description: Metrics Context not found
*/
@Get('/:context')
public getMetricsByContext(@Param('context') context: string, @Req() request: AppRequest): Promise<IMetricUnit[]> {
public getMetricsByContext(
@Params({ validate: true }) { context }: ContextValidator,
@Req() request: AppRequest
): Promise<IMetricUnit[]> {
return this.metricService.getMetricsByContext(context, request.logger);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { JsonController, Get, OnUndefined, Param, Post, Put, Body, Authorized, Delete, Req } from 'routing-controllers';
import {
JsonController,
Get,
OnUndefined,
Post,
Put,
Body,
Authorized,
Delete,
Req,
Params,
} from 'routing-controllers';
import { UserNotFoundError } from '../errors/UserNotFoundError';
import { SERVER_ERROR } from 'upgrade_types';
import { PreviewUserService } from '../services/PreviewUserService';
import { PreviewUser } from '../models/PreviewUser';
import { isString } from 'class-validator';
import { PaginatedParamsValidator } from './validators/PaginatedParamsValidator';
import { AppRequest, PaginationResponse } from '../../types';
import { PreviewUserValidator } from './validators/PreviewUserValidator';
import { IdValidator } from './validators/ExperimentUserValidator';

interface PreviewUserPaginationInfo extends PaginationResponse {
nodes: PreviewUser[];
Expand Down Expand Up @@ -83,14 +93,6 @@ export class PreviewUserController {
@Body({ validate: true }) paginatedParams: PaginatedParamsValidator,
@Req() request: AppRequest
): Promise<PreviewUserPaginationInfo> {
if (!paginatedParams) {
return Promise.reject(
new Error(
JSON.stringify({ type: SERVER_ERROR.MISSING_PARAMS, message: ' : paginatedParams should not be null.' })
)
);
}

const [previewUsers, count] = await Promise.all([
this.previewUserService.findPaginated(paginatedParams.skip, paginatedParams.take, request.logger),
this.previewUserService.getTotalCount(request.logger),
Expand Down Expand Up @@ -126,14 +128,10 @@ export class PreviewUserController {
*/
@Get('/:id')
@OnUndefined(UserNotFoundError)
public one(@Param('id') id: string, @Req() request: AppRequest): Promise<PreviewUser | undefined> {
if (!isString(id)) {
return Promise.reject(
new Error(
JSON.stringify({ type: SERVER_ERROR.INCORRECT_PARAM_FORMAT, message: ' : id should be of type string.' })
)
);
}
public one(
@Params({ validate: true }) { id }: IdValidator,
@Req() request: AppRequest
): Promise<PreviewUser | undefined> {
return this.previewUserService.findOne(id, request.logger);
}

Expand Down Expand Up @@ -161,7 +159,10 @@ export class PreviewUserController {
* description: New ExperimentUser is created
*/
@Post()
public create(@Body({ validate: true }) users: PreviewUserValidator, @Req() request: AppRequest): Promise<PreviewUser> {
public create(
@Body({ validate: true }) users: PreviewUserValidator,
@Req() request: AppRequest
): Promise<PreviewUser> {
return this.previewUserService.create(users, request.logger);
}

Expand Down Expand Up @@ -196,7 +197,7 @@ export class PreviewUserController {
*/
@Put('/:id')
public update(
@Param('id') id: string,
@Params({ validate: true }) { id }: IdValidator,
@Body({ validate: true }) user: PreviewUserValidator,
@Req() request: AppRequest
): Promise<PreviewUser> {
Expand Down Expand Up @@ -224,7 +225,10 @@ export class PreviewUserController {
* description: Delete User By Id
*/
@Delete('/:id')
public delete(@Param('id') id: string, @Req() request: AppRequest): Promise<PreviewUser | undefined> {
public delete(
@Params({ validate: true }) { id }: IdValidator,
@Req() request: AppRequest
): Promise<PreviewUser | undefined> {
return this.previewUserService.delete(id, request.logger);
}

Expand Down Expand Up @@ -252,7 +256,10 @@ export class PreviewUserController {
* description: Assignment is created
*/
@Post('/assign')
public assign(@Body({ validate: true }) user: PreviewUserValidator, @Req() request: AppRequest): Promise<PreviewUser> {
public assign(
@Body({ validate: true }) user: PreviewUserValidator,
@Req() request: AppRequest
): Promise<PreviewUser> {
return this.previewUserService.upsertExperimentConditionAssignment(user, request.logger);
}
}
31 changes: 6 additions & 25 deletions backend/packages/Upgrade/src/api/controllers/SegmentController.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import {
JsonController,
Get,
Delete,
Param,
Authorized,
Post,
Req,
Body,
QueryParams,
Params,
} from 'routing-controllers';
import { JsonController, Get, Delete, Authorized, Post, Req, Body, QueryParams, Params } from 'routing-controllers';
import { SegmentService, SegmentWithStatus } from '../services/SegmentService';
import { Segment } from '../models/Segment';
import { SERVER_ERROR } from 'upgrade_types';
import { isUUID } from 'class-validator';
import { AppRequest } from '../../types';
import {
IdValidator,
SegmentFile,
SegmentIds,
SegmentIdValidator,
SegmentImportError,
SegmentInputValidator,
} from './validators/SegmentInputValidator';
Expand Down Expand Up @@ -383,17 +371,10 @@ export class SegmentController {
* description: Internal Server Error, SegmentId is not valid
*/
@Delete('/:segmentId')
public deleteSegment(@Param('segmentId') segmentId: string, @Req() request: AppRequest): Promise<Segment> {
if (!segmentId) {
return Promise.reject(new Error(SERVER_ERROR.MISSING_PARAMS + ' : segmentId should not be null.'));
}
if (!isUUID(segmentId)) {
return Promise.reject(
new Error(
JSON.stringify({ type: SERVER_ERROR.INCORRECT_PARAM_FORMAT, message: ' : segmentId should be of type UUID.' })
)
);
}
public deleteSegment(
@Params({ validate: true }) { segmentId }: SegmentIdValidator,
@Req() request: AppRequest
): Promise<Segment> {
return this.segmentService.deleteSegment(segmentId, request.logger);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { JsonController, Get, Delete, Param, Authorized, Req, Res, Body, Post } from 'routing-controllers';
import { SERVER_ERROR } from 'upgrade_types';
import { JsonController, Get, Delete, Authorized, Req, Res, Body, Post, Params } from 'routing-controllers';
import { AppRequest } from '../../types';
import { UserStratificationFactor } from '../models/UserStratificationFactor';
import { StratificationService } from '../services/StratificationService';
import { FactorStrata, UploadedFilesArrayValidator } from './validators/StratificationValidator';
import {
FactorStrata,
StratificationFactorValidator,
UploadedFilesArrayValidator,
} from './validators/StratificationValidator';
import { StratificationFactor } from '../models/StratificationFactor';
import * as express from 'express';

Expand Down Expand Up @@ -110,16 +113,11 @@ export class StratificationController {
*/
@Get('/download/:factor')
public async getStratificationByFactor(
@Param('factor') factor: string,
@Params({ validate: true }) { factor }: StratificationFactorValidator,
@Req() request: AppRequest,
@Res() res: express.Response
): Promise<express.Response> {
if (!factor) {
return Promise.reject(new Error(SERVER_ERROR.MISSING_PARAMS + ' : stratification Factor should not be null.'));
}

const csvData = await this.stratificationService.getCSVDataByFactor(factor, request.logger);

// return csv file with appropriate headers to request;
res.setHeader('Content-Type', 'text/csv; charset=UTF-8');
res.setHeader('Content-Disposition', `attachment; filename="${factor}.csv"`);
Expand Down Expand Up @@ -190,12 +188,9 @@ export class StratificationController {
*/
@Delete('/:factor')
public deleteStratification(
@Param('factor') stratificationFactor: string,
@Params({ validate: true }) { factor }: StratificationFactorValidator,
@Req() request: AppRequest
): Promise<StratificationFactor> {
if (!stratificationFactor) {
return Promise.reject(new Error(SERVER_ERROR.MISSING_PARAMS + ' : stratification Factor should not be null.'));
}
return this.stratificationService.deleteStratification(stratificationFactor, request.logger);
return this.stratificationService.deleteStratification(factor, request.logger);
}
}
Loading

0 comments on commit 703bbd9

Please sign in to comment.