diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts index 5e84e0ab49..a65f595e9c 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentController.ts @@ -1093,7 +1093,7 @@ export class ExperimentController { * @swagger * /experiments/{id}: * put: - * description: Create New Experiment + * description: Update Experiment * consumes: * - application/json * parameters: diff --git a/backend/packages/Upgrade/src/api/services/ExperimentAssignmentService.ts b/backend/packages/Upgrade/src/api/services/ExperimentAssignmentService.ts index 243d8bdbab..1a36054329 100644 --- a/backend/packages/Upgrade/src/api/services/ExperimentAssignmentService.ts +++ b/backend/packages/Upgrade/src/api/services/ExperimentAssignmentService.ts @@ -50,7 +50,6 @@ import isequal from 'lodash.isequal'; import flatten from 'lodash.flatten'; import { ILogInput, ENROLLMENT_CODE } from 'upgrade_types'; import { StateTimeLogsRepository } from '../repositories/StateTimeLogsRepository'; -import { StateTimeLog } from '../models/StateTimeLogs'; import { UpgradeLogger } from '../../lib/logger/UpgradeLogger'; import { SegmentService } from './SegmentService'; import { MonitoredDecisionPointLogRepository } from '../repositories/MonitoredDecisionPointLogRepository'; @@ -1067,17 +1066,14 @@ export class ExperimentAssignmentService { private async checkEnrollmentEndingCriteriaForCount(experiment: Experiment, logger: UpgradeLogger): Promise { const { enrollmentCompleteCondition } = experiment; const { groupCount, userCount } = enrollmentCompleteCondition; - - const timeLogDate = new Date(); /** * Create stateTimeLog document which will be inserted if ending criteria is met */ - const stateTimeLogDoc = new StateTimeLog(); - stateTimeLogDoc.id = uuid(); - stateTimeLogDoc.fromState = experiment.state; - stateTimeLogDoc.toState = EXPERIMENT_STATE.ENROLLMENT_COMPLETE; - stateTimeLogDoc.timeLog = timeLogDate; - stateTimeLogDoc.experiment = experiment; + const stateTimeLogDoc = await this.experimentService.prepareStateTimeLogDoc( + experiment, + experiment.state, + EXPERIMENT_STATE.ENROLLMENT_COMPLETE + ); if (groupCount && userCount && experiment.assignmentUnit === ASSIGNMENT_UNIT.GROUP) { const groupSatisfied: number = await this.getGroupAssignmentStatus(experiment.id, logger); diff --git a/backend/packages/Upgrade/src/api/services/ExperimentService.ts b/backend/packages/Upgrade/src/api/services/ExperimentService.ts index c872392117..084d03e5d5 100644 --- a/backend/packages/Upgrade/src/api/services/ExperimentService.ts +++ b/backend/packages/Upgrade/src/api/services/ExperimentService.ts @@ -403,6 +403,20 @@ export class ExperimentService { return [...conditionIds, ...decisionPointsIds]; } + public async prepareStateTimeLogDoc( + experimentDoc: Experiment, + fromState: EXPERIMENT_STATE, + toState: EXPERIMENT_STATE + ) { + const stateTimeLogDoc = new StateTimeLog(); + stateTimeLogDoc.id = uuid(); + stateTimeLogDoc.fromState = fromState; + stateTimeLogDoc.toState = toState; + stateTimeLogDoc.timeLog = new Date(); + stateTimeLogDoc.experiment = experimentDoc; + return stateTimeLogDoc; + } + public async updateState( experimentId: string, state: EXPERIMENT_STATE, @@ -459,14 +473,7 @@ export class ExperimentService { // add experiment audit logs await this.experimentAuditLogRepository.saveRawJson(LOG_TYPE.EXPERIMENT_STATE_CHANGED, data, user, entityManager); - const timeLogDate = new Date(); - - const stateTimeLogDoc = new StateTimeLog(); - stateTimeLogDoc.id = uuid(); - stateTimeLogDoc.fromState = oldExperiment.state; - stateTimeLogDoc.toState = state; - stateTimeLogDoc.timeLog = timeLogDate; - stateTimeLogDoc.experiment = oldExperiment; + const stateTimeLogDoc = await this.prepareStateTimeLogDoc(oldExperiment, oldExperiment.state, state); // updating the experiment and stateTimeLog const stateTimeLogRepo = entityManager ? entityManager.getRepository(StateTimeLog) : this.stateTimeLogsRepository; @@ -785,6 +792,13 @@ export class ExperimentService { let experimentDoc: Experiment; try { experimentDoc = await transactionalEntityManager.getRepository(Experiment).save(expDoc); + // Store state time log for the experiment + const stateTimeLogDoc = await this.prepareStateTimeLogDoc( + experimentDoc, + oldExperiment.state, + experimentDoc.state + ); + await transactionalEntityManager.getRepository(StateTimeLog).save(stateTimeLogDoc); } catch (err) { const error = err as ErrorWithType; error.details = `Error in updating experiment document "updateExperimentInDB"`; @@ -1228,6 +1242,15 @@ export class ExperimentService { let experimentDoc: Experiment; try { experimentDoc = await transactionalEntityManager.getRepository(Experiment).save(expDoc); + // Store state time log for the experiment in enrolling state. + if (experimentDoc.state !== EXPERIMENT_STATE.INACTIVE) { + const stateTimeLogDoc = await this.prepareStateTimeLogDoc( + experimentDoc, + EXPERIMENT_STATE.INACTIVE, + experimentDoc.state + ); + await transactionalEntityManager.getRepository(StateTimeLog).save(stateTimeLogDoc); + } } catch (err) { const error = err as ErrorWithType; error.details = 'Error in adding experiment in DB';