diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts index 5713161d03..4e6709435e 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts @@ -1,4 +1,13 @@ -import { JsonController, Post, Body, UseBefore, Req, InternalServerError, Delete } from 'routing-controllers'; +import { + JsonController, + Post, + Body, + UseBefore, + Req, + InternalServerError, + Delete, + Authorized, +} from 'routing-controllers'; import { ExperimentService } from '../services/ExperimentService'; import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService'; import { MarkExperimentValidator } from './validators/MarkExperimentValidator'; @@ -19,7 +28,6 @@ import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAlias import { Metric } from '../models/Metric'; import * as express from 'express'; import { AppRequest } from '../../types'; -import { env } from '../../env'; import flatten from 'lodash.flatten'; import { CaliperLogEnvelope } from './validators/CaliperLogEnvelope'; import { ExperimentUserValidator } from './validators/ExperimentUserValidator'; @@ -1090,13 +1098,9 @@ export class ExperimentClientController { * '500': * description: DEMO mode is disabled */ + @Authorized() @Delete('clearDB') public async clearDB(@Req() request: AppRequest): Promise { - // if DEMO mode is enabled, then clear the database: - if (env.app.demo) { - await this.experimentUserService.clearDB(request.logger); - return 'DB truncate successful'; - } - return Promise.resolve('DEMO mode is disabled. You cannot clear DB.'); + return this.experimentUserService.clearDB(request.logger); } } diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts index d36fa28c8e..481ef7e1c1 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts @@ -7,7 +7,7 @@ import { InternalServerError, Delete, Patch, - OnUndefined, + Authorized, } from 'routing-controllers'; import { ExperimentService } from '../services/ExperimentService'; import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService'; @@ -27,7 +27,6 @@ import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAlias import { Metric } from '../models/Metric'; import * as express from 'express'; import { AppRequest } from '../../types'; -import { env } from '../../env'; import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog'; import { Log } from '../models/Log'; import flatten from 'lodash.flatten'; @@ -1023,15 +1022,9 @@ export class ExperimentClientController { * '500': * description: DEMO mode is disabled */ + @Authorized() @Delete('clearDB') - @OnUndefined(204) - public clearDB(@Req() request: AppRequest) { - // if DEMO mode is enabled, then clear the database: - if (!env.app.demo) { - this.experimentUserService.clearDB(request.logger); - } else { - request.logger.error({ message: 'DEMO mode is disabled. You cannot clear DB.' }); - } - return; + public async clearDB(@Req() request: AppRequest): Promise { + return this.experimentUserService.clearDB(request.logger); } } diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts index b4ba343665..e392e8e5e7 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts @@ -7,7 +7,7 @@ import { InternalServerError, Delete, Patch, - OnUndefined, + Authorized, } from 'routing-controllers'; import { ExperimentService } from '../services/ExperimentService'; import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService'; @@ -32,7 +32,6 @@ import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAlias import { Metric } from '../models/Metric'; import * as express from 'express'; import { AppRequest } from '../../types'; -import { env } from '../../env'; import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog'; import { MarkExperimentValidatorv4 } from './validators/MarkExperimentValidator.v4'; import { Log } from '../models/Log'; @@ -979,15 +978,9 @@ export class ExperimentClientController { * '500': * description: DEMO mode is disabled */ + @Authorized() @Delete('clearDB') - @OnUndefined(204) - public clearDB(@Req() request: AppRequest) { - // if DEMO mode is enabled, then clear the database: - if (!env.app.demo) { - this.experimentUserService.clearDB(request.logger); - } else { - request.logger.error({ message: 'DEMO mode is disabled. You cannot clear DB.' }); - } - return; + public async clearDB(@Req() request: AppRequest): Promise { + return this.experimentUserService.clearDB(request.logger); } } diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts index 58f747b1e6..69caaf5d38 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts @@ -7,7 +7,7 @@ import { InternalServerError, Delete, Patch, - OnUndefined, + Authorized, } from 'routing-controllers'; import { ExperimentService } from '../services/ExperimentService'; import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService'; @@ -31,7 +31,6 @@ import { MetricService } from '../services/MetricService'; import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAliasesValidator'; import * as express from 'express'; import { AppRequest } from '../../types'; -import { env } from '../../env'; import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog'; import { MarkExperimentValidatorv5 } from './validators/MarkExperimentValidator.v5'; import { Log } from '../models/Log'; @@ -888,15 +887,9 @@ export class ExperimentClientController { * '500': * description: DEMO mode is disabled */ + @Authorized() @Delete('clearDB') - @OnUndefined(204) - public clearDB(@Req() request: AppRequest) { - // if DEMO mode is enabled, then clear the database: - if (!env.app.demo) { - this.experimentUserService.clearDB(request.logger); - } else { - request.logger.error({ message: 'DEMO mode is disabled. You cannot clear DB.' }); - } - return; + public async clearDB(@Req() request: AppRequest): Promise { + return this.experimentUserService.clearDB(request.logger); } } diff --git a/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts b/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts index db63bf8ea7..9b56de00d1 100644 --- a/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts +++ b/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts @@ -12,6 +12,7 @@ import { GroupExclusionRepository } from '../repositories/GroupExclusionReposito import { Experiment } from '../models/Experiment'; import isEqual from 'lodash/isEqual'; import { RequestedExperimentUser } from '../controllers/validators/ExperimentUserValidator'; +import { env } from '../../env'; @Service() export class ExperimentUserService { @@ -345,10 +346,16 @@ export class ExperimentUserService { } } - public async clearDB(logger: UpgradeLogger): Promise { + public async clearDB(logger: UpgradeLogger): Promise { + if (!env.app.demo) { + return 'DEMO mode is disabled. You cannot clear DB.'; + } + await getConnection().transaction(async (transactionalEntityManager) => { await this.experimentRepository.clearDB(transactionalEntityManager, logger); }); + + return 'DB truncate successful'; } /**