Skip to content

Commit

Permalink
Centralize demo mode DB clearing logic in ExperimentUserService (#1845)
Browse files Browse the repository at this point in the history
* Centralize demo mode DB clearing logic in ExperimentUserService

* Add Authorized decorator and remove redundant OnUndefined decorator
  • Loading branch information
zackcl authored Aug 22, 2024
1 parent ed8289a commit 056283f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -1090,13 +1098,9 @@ export class ExperimentClientController {
* '500':
* description: DEMO mode is disabled
*/
@Authorized()
@Delete('clearDB')
public async clearDB(@Req() request: AppRequest): Promise<string> {
// 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InternalServerError,
Delete,
Patch,
OnUndefined,
Authorized,
} from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
Expand All @@ -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';
Expand Down Expand Up @@ -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<string> {
return this.experimentUserService.clearDB(request.logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InternalServerError,
Delete,
Patch,
OnUndefined,
Authorized,
} from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
Expand All @@ -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';
Expand Down Expand Up @@ -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<string> {
return this.experimentUserService.clearDB(request.logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InternalServerError,
Delete,
Patch,
OnUndefined,
Authorized,
} from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
Expand All @@ -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';
Expand Down Expand Up @@ -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<string> {
return this.experimentUserService.clearDB(request.logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -345,10 +346,16 @@ export class ExperimentUserService {
}
}

public async clearDB(logger: UpgradeLogger): Promise<void> {
public async clearDB(logger: UpgradeLogger): Promise<string> {
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';
}

/**
Expand Down

0 comments on commit 056283f

Please sign in to comment.