@@ -10,16 +10,21 @@ import {
1010 Logger ,
1111 HttpCode ,
1212 HttpStatus ,
13+ UseGuards ,
14+ Request ,
1315} from '@nestjs/common' ;
14- import { ABTestingService } from './ab-testing.service' ;
16+ import { ABTestingService , CreateExperimentDto } from './ab-testing.service' ;
1517import { ExperimentService } from './experiments/experiment.service' ;
1618import { StatisticalAnalysisService } from './analysis/statistical-analysis.service' ;
1719import { AutomatedDecisionService } from './automation/automated-decision.service' ;
1820import { ABTestingReportsService } from './reporting/ab-testing-reports.service' ;
19- import { CreateExperimentDto } from './ab-testing.service' ;
20- import { ExperimentStatus , ExperimentType } from './entities/experiment.entity' ;
21+ import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard' ;
22+ import { RolesGuard } from '../auth/guards/roles.guard' ;
23+ import { Roles } from '../auth/decorators/roles.decorator' ;
24+ import { UserRole } from '../users/entities/user.entity' ;
2125
2226@Controller ( 'ab-testing' )
27+ @UseGuards ( JwtAuthGuard , RolesGuard )
2328export class ABTestingController {
2429 private readonly logger = new Logger ( ABTestingController . name ) ;
2530
@@ -32,6 +37,7 @@ export class ABTestingController {
3237 ) { }
3338
3439 @Get ( 'experiments' )
40+ @Roles ( UserRole . ADMIN , UserRole . TEACHER )
3541 async getAllExperiments ( ) {
3642 this . logger . log ( 'Fetching all experiments' ) ;
3743 return await this . abTestingService . getAllExperiments ( ) ;
@@ -45,34 +51,39 @@ export class ABTestingController {
4551
4652 @Post ( 'experiments' )
4753 @HttpCode ( HttpStatus . CREATED )
48- async createExperiment ( @Body ( ) createExperimentDto : CreateExperimentDto ) {
54+ @Roles ( UserRole . ADMIN )
55+ async createExperiment ( @Request ( ) req , @Body ( ) createExperimentDto : CreateExperimentDto ) {
4956 this . logger . log ( `Creating new experiment: ${ createExperimentDto . name } ` ) ;
5057 return await this . abTestingService . createExperiment ( createExperimentDto ) ;
5158 }
5259
5360 @Post ( 'experiments/:id/start' )
5461 @HttpCode ( HttpStatus . OK )
62+ @Roles ( UserRole . ADMIN )
5563 async startExperiment ( @Param ( 'id' ) id : string ) {
5664 this . logger . log ( `Starting experiment: ${ id } ` ) ;
5765 return await this . abTestingService . startExperiment ( id ) ;
5866 }
5967
6068 @Post ( 'experiments/:id/stop' )
6169 @HttpCode ( HttpStatus . OK )
70+ @Roles ( UserRole . ADMIN )
6271 async stopExperiment ( @Param ( 'id' ) id : string ) {
6372 this . logger . log ( `Stopping experiment: ${ id } ` ) ;
6473 return await this . abTestingService . stopExperiment ( id ) ;
6574 }
6675
6776 @Put ( 'experiments/:id' )
6877 @HttpCode ( HttpStatus . OK )
78+ @Roles ( UserRole . ADMIN )
6979 async updateExperiment ( @Param ( 'id' ) id : string , @Body ( ) updateData : any ) {
7080 this . logger . log ( `Updating experiment: ${ id } ` ) ;
7181 return await this . experimentService . updateExperiment ( id , updateData ) ;
7282 }
7383
7484 @Delete ( 'experiments/:id' )
7585 @HttpCode ( HttpStatus . OK )
86+ @Roles ( UserRole . ADMIN )
7687 async deleteExperiment ( @Param ( 'id' ) id : string ) {
7788 this . logger . log ( `Deleting experiment: ${ id } ` ) ;
7889 // Implementation would go here
@@ -87,13 +98,15 @@ export class ABTestingController {
8798
8899 @Post ( 'experiments/:id/variants' )
89100 @HttpCode ( HttpStatus . CREATED )
101+ @Roles ( UserRole . ADMIN )
90102 async addVariant ( @Param ( 'id' ) experimentId : string , @Body ( ) variantData : any ) {
91103 this . logger . log ( `Adding variant to experiment: ${ experimentId } ` ) ;
92104 return await this . experimentService . addVariant ( experimentId , variantData ) ;
93105 }
94106
95107 @Delete ( 'variants/:id' )
96108 @HttpCode ( HttpStatus . OK )
109+ @Roles ( UserRole . ADMIN )
97110 async removeVariant ( @Param ( 'id' ) variantId : string ) {
98111 this . logger . log ( `Removing variant: ${ variantId } ` ) ;
99112 await this . experimentService . removeVariant ( variantId ) ;
@@ -102,6 +115,7 @@ export class ABTestingController {
102115
103116 @Put ( 'experiments/:id/traffic-allocation' )
104117 @HttpCode ( HttpStatus . OK )
118+ @Roles ( UserRole . ADMIN )
105119 async updateTrafficAllocation (
106120 @Param ( 'id' ) experimentId : string ,
107121 @Body ( ) allocations : Record < string , number > ,
@@ -125,6 +139,7 @@ export class ABTestingController {
125139
126140 @Post ( 'experiments/:id/auto-select-winner' )
127141 @HttpCode ( HttpStatus . OK )
142+ @Roles ( UserRole . ADMIN )
128143 async autoSelectWinner ( @Param ( 'id' ) id : string , @Body ( ) criteria ?: any ) {
129144 this . logger . log ( `Auto-selecting winner for experiment: ${ id } ` ) ;
130145 return await this . automatedDecisionService . autoSelectWinner ( id , criteria ) ;
@@ -138,13 +153,15 @@ export class ABTestingController {
138153
139154 @Post ( 'experiments/:id/auto-allocate-traffic' )
140155 @HttpCode ( HttpStatus . OK )
156+ @Roles ( UserRole . ADMIN )
141157 async autoAllocateTraffic ( @Param ( 'id' ) id : string ) {
142158 this . logger . log ( `Auto-allocating traffic for experiment: ${ id } ` ) ;
143159 await this . automatedDecisionService . autoAllocateTraffic ( id ) ;
144160 return { message : 'Traffic auto-allocated successfully' } ;
145161 }
146162
147163 @Get ( 'reports/dashboard' )
164+ @Roles ( UserRole . ADMIN , UserRole . TEACHER )
148165 async getDashboardSummary ( @Query ( ) filters ?: any ) {
149166 this . logger . log ( 'Generating dashboard summary' ) ;
150167 return await this . reportsService . getDashboardSummary ( filters ) ;
@@ -180,26 +197,30 @@ export class ABTestingController {
180197
181198 @Post ( 'experiments/:id/pause' )
182199 @HttpCode ( HttpStatus . OK )
200+ @Roles ( UserRole . ADMIN )
183201 async pauseExperiment ( @Param ( 'id' ) id : string ) {
184202 this . logger . log ( `Pausing experiment: ${ id } ` ) ;
185203 return await this . experimentService . pauseExperiment ( id ) ;
186204 }
187205
188206 @Post ( 'experiments/:id/resume' )
189207 @HttpCode ( HttpStatus . OK )
208+ @Roles ( UserRole . ADMIN )
190209 async resumeExperiment ( @Param ( 'id' ) id : string ) {
191210 this . logger . log ( `Resuming experiment: ${ id } ` ) ;
192211 return await this . experimentService . resumeExperiment ( id ) ;
193212 }
194213
195214 @Post ( 'experiments/:id/archive' )
196215 @HttpCode ( HttpStatus . OK )
216+ @Roles ( UserRole . ADMIN )
197217 async archiveExperiment ( @Param ( 'id' ) id : string ) {
198218 this . logger . log ( `Archiving experiment: ${ id } ` ) ;
199219 return await this . experimentService . archiveExperiment ( id ) ;
200220 }
201221
202222 @Get ( 'experiments/:id/assign-user/:userId' )
223+ @Roles ( UserRole . ADMIN )
203224 async assignUserToVariant ( @Param ( 'id' ) experimentId : string , @Param ( 'userId' ) userId : string ) {
204225 this . logger . log ( `Assigning user ${ userId } to variant for experiment: ${ experimentId } ` ) ;
205226 return await this . abTestingService . assignUserToVariant ( experimentId , userId ) ;
0 commit comments