diff --git a/backend/packages/Upgrade/src/api/services/ExperimentService.ts b/backend/packages/Upgrade/src/api/services/ExperimentService.ts index 43ee1e1741..49cc6a0f95 100644 --- a/backend/packages/Upgrade/src/api/services/ExperimentService.ts +++ b/backend/packages/Upgrade/src/api/services/ExperimentService.ts @@ -298,31 +298,32 @@ export class ExperimentService { deleteAuditLogData, currentUser ); - - try { - await transactionalEntityManager - .getRepository(Segment) - .delete(experiment.experimentSegmentInclusion.segment.id); - } catch (err) { - const error = err as ErrorWithType; - error.details = 'Error in deleting Include Segment fron DB'; - error.type = SERVER_ERROR.QUERY_FAILED; - logger.error(error); - throw error; + if (experiment.experimentSegmentInclusion) { + try { + await transactionalEntityManager + .getRepository(Segment) + .delete(experiment.experimentSegmentInclusion.segment.id); + } catch (err) { + const error = err as ErrorWithType; + error.details = 'Error in deleting Include Segment fron DB'; + error.type = SERVER_ERROR.QUERY_FAILED; + logger.error(error); + throw error; + } } - - try { - await transactionalEntityManager - .getRepository(Segment) - .delete(experiment.experimentSegmentExclusion.segment.id); - } catch (err) { - const error = err as ErrorWithType; - error.details = 'Error in deleting Exclude Segment fron DB'; - error.type = SERVER_ERROR.QUERY_FAILED; - logger.error(error); - throw error; + if (experiment.experimentSegmentExclusion) { + try { + await transactionalEntityManager + .getRepository(Segment) + .delete(experiment.experimentSegmentExclusion.segment.id); + } catch (err) { + const error = err as ErrorWithType; + error.details = 'Error in deleting Exclude Segment fron DB'; + error.type = SERVER_ERROR.QUERY_FAILED; + logger.error(error); + throw error; + } } - return deletedExperiment; } @@ -1131,79 +1132,100 @@ export class ExperimentService { logger.error(error); throw error; } - - // creating Include Segment + let includeSegmentExists = true; + let segmentIncludeDoc: Segment; + let segmentIncludeDocToSave: Partial = {}; experimentDoc.experimentSegmentInclusion = experimentSegmentInclusion; - let segmentInclude; - if (experimentDoc.experimentSegmentInclusion.segment) { - const includeSegment = experimentDoc.experimentSegmentInclusion.segment; - segmentInclude = { - ...experimentSegmentInclusion, - type: includeSegment.type, - userIds: includeSegment.individualForSegment.map((x) => x.userId), - groups: includeSegment.groupForSegment.map((x) => { - return { type: x.type, groupId: x.groupId }; - }), - subSegmentIds: includeSegment.subSegments.map((x) => x.id), + if (experimentDoc.experimentSegmentInclusion) { + // creating Include Segment + let segmentInclude; + if (experimentDoc.experimentSegmentInclusion.segment) { + const includeSegment = experimentDoc.experimentSegmentInclusion.segment; + segmentInclude = { + ...experimentSegmentInclusion, + type: includeSegment.type, + userIds: includeSegment.individualForSegment.map((x) => x.userId), + groups: includeSegment.groupForSegment.map((x) => { + return { type: x.type, groupId: x.groupId }; + }), + subSegmentIds: includeSegment.subSegments.map((x) => x.id), + }; + } else { + segmentInclude = experimentDoc.experimentSegmentInclusion; + } + + const segmentIncludeData: SegmentInputValidator = { + ...segmentInclude, + id: uuid(), + name: experiment.id + ' Inclusion Segment', + description: experiment.id + ' Inclusion Segment', + context: experiment.context[0], + type: SEGMENT_TYPE.PRIVATE, }; + try { + segmentIncludeDoc = await this.segmentService.upsertSegment(segmentIncludeData, logger); + } catch (err) { + const error = err as ErrorWithType; + error.details = 'Error in adding segment in DB'; + error.type = SERVER_ERROR.QUERY_FAILED; + logger.error(error); + throw error; + } + // creating segmentInclude doc + const includeTempDoc = new ExperimentSegmentInclusion(); + includeTempDoc.segment = segmentIncludeDoc; + includeTempDoc.experiment = experimentDoc; + segmentIncludeDocToSave = this.getSegmentDoc(includeTempDoc); } else { - segmentInclude = experimentDoc.experimentSegmentInclusion; + includeSegmentExists = false; } - const segmentIncludeData: SegmentInputValidator = { - ...segmentInclude, - id: uuid(), - name: experiment.id + ' Inclusion Segment', - description: experiment.id + ' Inclusion Segment', - context: experiment.context[0], - type: SEGMENT_TYPE.PRIVATE, - }; - let segmentIncludeDoc: Segment; - try { - segmentIncludeDoc = await this.segmentService.upsertSegment(segmentIncludeData, logger); - } catch (err) { - const error = err as ErrorWithType; - error.details = 'Error in adding segment in DB'; - error.type = SERVER_ERROR.QUERY_FAILED; - logger.error(error); - throw error; - } - - // creating Exclude Segment + let excludeSegmentExists = true; + let segmentExcludeDoc: Segment; + let segmentExcludeDocToSave: Partial = {}; experimentDoc.experimentSegmentExclusion = experimentSegmentExclusion; - let segmentExclude; - if (experimentDoc.experimentSegmentExclusion.segment) { - const excludeSegment = experimentDoc.experimentSegmentExclusion.segment; - segmentExclude = { - ...experimentSegmentExclusion, - type: excludeSegment.type, - userIds: excludeSegment.individualForSegment.map((x) => x.userId), - groups: excludeSegment.groupForSegment.map((x) => { - return { type: x.type, groupId: x.groupId }; - }), - subSegmentIds: excludeSegment.subSegments.map((x) => x.id), + if (experimentDoc.experimentSegmentExclusion) { + // creating Exclude Segment + let segmentExclude; + if (experimentDoc.experimentSegmentExclusion.segment) { + const excludeSegment = experimentDoc.experimentSegmentExclusion.segment; + segmentExclude = { + ...experimentSegmentExclusion, + type: excludeSegment.type, + userIds: excludeSegment.individualForSegment.map((x) => x.userId), + groups: excludeSegment.groupForSegment.map((x) => { + return { type: x.type, groupId: x.groupId }; + }), + subSegmentIds: excludeSegment.subSegments.map((x) => x.id), + }; + } else { + segmentExclude = experimentDoc.experimentSegmentExclusion; + } + + const segmentExcludeData: SegmentInputValidator = { + ...segmentExclude, + id: uuid(), + name: experiment.id + ' Exclusion Segment', + description: experiment.id + ' Exclusion Segment', + context: experiment.context[0], + type: SEGMENT_TYPE.PRIVATE, }; + try { + segmentExcludeDoc = await this.segmentService.upsertSegment(segmentExcludeData, logger); + } catch (err) { + const error = err as ErrorWithType; + error.details = 'Error in adding segment in DB'; + error.type = SERVER_ERROR.QUERY_FAILED; + logger.error(error); + throw error; + } + // creating segmentExclude doc + const excludeTempDoc = new ExperimentSegmentExclusion(); + excludeTempDoc.segment = segmentExcludeDoc; + excludeTempDoc.experiment = experimentDoc; + segmentExcludeDocToSave = this.getSegmentDoc(excludeTempDoc); } else { - segmentExclude = experimentDoc.experimentSegmentExclusion; - } - - const segmentExcludeData: SegmentInputValidator = { - ...segmentExclude, - id: uuid(), - name: experiment.id + ' Exclusion Segment', - description: experiment.id + ' Exclusion Segment', - context: experiment.context[0], - type: SEGMENT_TYPE.PRIVATE, - }; - let segmentExcludeDoc: Segment; - try { - segmentExcludeDoc = await this.segmentService.upsertSegment(segmentExcludeData, logger); - } catch (err) { - const error = err as ErrorWithType; - error.details = 'Error in adding segment in DB'; - error.type = SERVER_ERROR.QUERY_FAILED; - logger.error(error); - throw error; + excludeSegmentExists = false; } // creating condition docs @@ -1226,6 +1248,9 @@ export class ExperimentService { return decisionPoint; }); + if (!conditionPayloads) { + experiment = { ...experiment, conditionPayloads: [] }; + } // update conditionPayloads condition uuids: if (conditionPayloads) { conditionPayloads.map((conditionPayload) => { @@ -1237,7 +1262,6 @@ export class ExperimentService { } }); } - const conditionPayloadDocToSave: Array> = (conditionPayloads && conditionPayloads.length > 0 && @@ -1253,22 +1277,12 @@ export class ExperimentService { })) || []; - // creating segmentInclude doc - const includeTempDoc = new ExperimentSegmentInclusion(); - includeTempDoc.segment = segmentIncludeDoc; - includeTempDoc.experiment = experimentDoc; - const segmentIncludeDocToSave = this.getSegmentDoc(includeTempDoc); - - // creating segmentExclude doc - const excludeTempDoc = new ExperimentSegmentExclusion(); - excludeTempDoc.segment = segmentExcludeDoc; - excludeTempDoc.experiment = experimentDoc; - const segmentExcludeDocToSave = this.getSegmentDoc(excludeTempDoc); // creating queries docs const promiseArray = []; let queryDocsToSave = - (queries[0] && + (queries && queries.length > 0 && + queries[0] && queries.map((query: any) => { promiseArray.push(this.metricRepository.findOne(query.metric.key)); // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -1307,17 +1321,26 @@ export class ExperimentService { ] = await Promise.all([ this.experimentConditionRepository.insertConditions(conditionDocsToSave, transactionalEntityManager), this.decisionPointRepository.insertDecisionPoint(decisionPointDocsToSave, transactionalEntityManager), - this.experimentSegmentInclusionRepository.insertData( - segmentIncludeDocToSave, - logger, - transactionalEntityManager - ), - this.experimentSegmentExclusionRepository.insertData( - segmentExcludeDocToSave, - logger, - transactionalEntityManager - ), - this.conditionPayloadRepository.insertConditionPayload(conditionPayloadDocToSave, transactionalEntityManager), + includeSegmentExists + ? this.experimentSegmentInclusionRepository.insertData( + segmentIncludeDocToSave, + logger, + transactionalEntityManager + ) + : (Promise.resolve([]) as any), + excludeSegmentExists + ? this.experimentSegmentExclusionRepository.insertData( + segmentExcludeDocToSave, + logger, + transactionalEntityManager + ) + : (Promise.resolve([]) as any), + conditionPayloadDocToSave.length > 0 + ? this.conditionPayloadRepository.insertConditionPayload( + conditionPayloadDocToSave, + transactionalEntityManager + ) + : (Promise.resolve([]) as any), queryDocsToSave.length > 0 ? this.queryRepository.insertQueries(queryDocsToSave, transactionalEntityManager) : (Promise.resolve([]) as any), @@ -1361,17 +1384,29 @@ export class ExperimentService { factorDocToReturn = this.formatingFactorAndLevels(factorDoc, levelDoc); conditionDocToReturn = this.formatingElements(conditionDocToReturn, levelCombinationElementDoc, levelDoc); } - - const newExperiment = { - ...experimentDoc, - conditions: conditionDocToReturn as any, - partitions: decisionPointDocToReturn as any, - factors: factorDocToReturn as any, - experimentSegmentInclusion: { ...experimentSegmentInclusionDoc, segment: segmentIncludeDoc } as any, - experimentSegmentExclusion: { ...experimentSegmentExclusionDoc, segment: segmentExcludeDoc } as any, - conditionPayloads: conditionPayloadDocToReturn as any, - queries: (queryDocToReturn as any) || [], - }; + let newExperimentObject; + if (experimentDoc.experimentSegmentInclusion && experimentDoc.experimentSegmentExclusion) { + newExperimentObject = { + ...experimentDoc, + conditions: conditionDocToReturn as any, + partitions: decisionPointDocToReturn as any, + factors: factorDocToReturn as any, + experimentSegmentInclusion: { ...experimentSegmentInclusionDoc, segment: segmentIncludeDoc } as any, + experimentSegmentExclusion: { ...experimentSegmentExclusionDoc, segment: segmentExcludeDoc } as any, + conditionPayloads: conditionPayloadDocToReturn as any, + queries: (queryDocToReturn as any) || [], + }; + } else { + newExperimentObject = { + ...experimentDoc, + conditions: conditionDocToReturn as any, + partitions: decisionPointDocToReturn as any, + factors: factorDocToReturn as any, + conditionPayloads: conditionPayloadDocToReturn as any, + queries: (queryDocToReturn as any) || [], + }; + } + const newExperiment = newExperimentObject; return newExperiment; }); // create schedules to start experiment and end experiment diff --git a/frontend/projects/upgrade/src/app/features/dashboard/home/components/experiment-participants/experiment-participants.component.ts b/frontend/projects/upgrade/src/app/features/dashboard/home/components/experiment-participants/experiment-participants.component.ts index 887472d7a9..dab45ab97a 100644 --- a/frontend/projects/upgrade/src/app/features/dashboard/home/components/experiment-participants/experiment-participants.component.ts +++ b/frontend/projects/upgrade/src/app/features/dashboard/home/components/experiment-participants/experiment-participants.component.ts @@ -124,8 +124,11 @@ export class ExperimentParticipantsComponent implements OnInit { }); this.participantsForm.get('inclusionCriteria').setValue(INCLUSION_CRITERIA.INCLUDE_SPECIFIC); - - if (this.experimentInfo) { + if ( + this.experimentInfo && + this.experimentInfo.experimentSegmentInclusion && + this.experimentInfo.experimentSegmentExclusion + ) { if (this.experimentInfo.filterMode === FILTER_MODE.EXCLUDE_ALL) { this.participantsForm.get('inclusionCriteria').setValue(INCLUSION_CRITERIA.INCLUDE_SPECIFIC); this.experimentInfo.experimentSegmentInclusion.segment.individualForSegment.forEach((id) => { @@ -167,6 +170,7 @@ export class ExperimentParticipantsComponent implements OnInit { this.members2.push(this.addMembers2(MemberTypes.SEGMENT, id.name)); }); } + } else { this.members1.removeAt(0); this.members2.removeAt(0); } diff --git a/frontend/projects/upgrade/src/app/features/dashboard/home/components/modal/import-experiment/import-experiment.component.html b/frontend/projects/upgrade/src/app/features/dashboard/home/components/modal/import-experiment/import-experiment.component.html index 2632f05b83..c20dc9e774 100644 --- a/frontend/projects/upgrade/src/app/features/dashboard/home/components/modal/import-experiment/import-experiment.component.html +++ b/frontend/projects/upgrade/src/app/features/dashboard/home/components/modal/import-experiment/import-experiment.component.html @@ -13,13 +13,13 @@ File Name - {{ element.fileName }} + {{ element.filename }} - Error + Error/Warning {{ element.error }} @@ -42,8 +42,11 @@