@@ -298,31 +298,32 @@ export class ExperimentService {
298298 deleteAuditLogData ,
299299 currentUser
300300 ) ;
301-
302- try {
303- await transactionalEntityManager
304- . getRepository ( Segment )
305- . delete ( experiment . experimentSegmentInclusion . segment . id ) ;
306- } catch ( err ) {
307- const error = err as ErrorWithType ;
308- error . details = 'Error in deleting Include Segment fron DB' ;
309- error . type = SERVER_ERROR . QUERY_FAILED ;
310- logger . error ( error ) ;
311- throw error ;
301+ if ( experiment . experimentSegmentInclusion ) {
302+ try {
303+ await transactionalEntityManager
304+ . getRepository ( Segment )
305+ . delete ( experiment . experimentSegmentInclusion . segment . id ) ;
306+ } catch ( err ) {
307+ const error = err as ErrorWithType ;
308+ error . details = 'Error in deleting Include Segment fron DB' ;
309+ error . type = SERVER_ERROR . QUERY_FAILED ;
310+ logger . error ( error ) ;
311+ throw error ;
312+ }
312313 }
313-
314- try {
315- await transactionalEntityManager
316- . getRepository ( Segment )
317- . delete ( experiment . experimentSegmentExclusion . segment . id ) ;
318- } catch ( err ) {
319- const error = err as ErrorWithType ;
320- error . details = 'Error in deleting Exclude Segment fron DB' ;
321- error . type = SERVER_ERROR . QUERY_FAILED ;
322- logger . error ( error ) ;
323- throw error ;
314+ if ( experiment . experimentSegmentExclusion ) {
315+ try {
316+ await transactionalEntityManager
317+ . getRepository ( Segment )
318+ . delete ( experiment . experimentSegmentExclusion . segment . id ) ;
319+ } catch ( err ) {
320+ const error = err as ErrorWithType ;
321+ error . details = 'Error in deleting Exclude Segment fron DB' ;
322+ error . type = SERVER_ERROR . QUERY_FAILED ;
323+ logger . error ( error ) ;
324+ throw error ;
325+ }
324326 }
325-
326327 return deletedExperiment ;
327328 }
328329
@@ -1131,79 +1132,100 @@ export class ExperimentService {
11311132 logger . error ( error ) ;
11321133 throw error ;
11331134 }
1134-
1135- // creating Include Segment
1135+ let includeSegmentExists = true ;
1136+ let segmentIncludeDoc : Segment ;
1137+ let segmentIncludeDocToSave : Partial < ExperimentSegmentInclusion > = { } ;
11361138 experimentDoc . experimentSegmentInclusion = experimentSegmentInclusion ;
1137- let segmentInclude ;
1138- if ( experimentDoc . experimentSegmentInclusion . segment ) {
1139- const includeSegment = experimentDoc . experimentSegmentInclusion . segment ;
1140- segmentInclude = {
1141- ...experimentSegmentInclusion ,
1142- type : includeSegment . type ,
1143- userIds : includeSegment . individualForSegment . map ( ( x ) => x . userId ) ,
1144- groups : includeSegment . groupForSegment . map ( ( x ) => {
1145- return { type : x . type , groupId : x . groupId } ;
1146- } ) ,
1147- subSegmentIds : includeSegment . subSegments . map ( ( x ) => x . id ) ,
1139+ if ( experimentDoc . experimentSegmentInclusion ) {
1140+ // creating Include Segment
1141+ let segmentInclude ;
1142+ if ( experimentDoc . experimentSegmentInclusion . segment ) {
1143+ const includeSegment = experimentDoc . experimentSegmentInclusion . segment ;
1144+ segmentInclude = {
1145+ ...experimentSegmentInclusion ,
1146+ type : includeSegment . type ,
1147+ userIds : includeSegment . individualForSegment . map ( ( x ) => x . userId ) ,
1148+ groups : includeSegment . groupForSegment . map ( ( x ) => {
1149+ return { type : x . type , groupId : x . groupId } ;
1150+ } ) ,
1151+ subSegmentIds : includeSegment . subSegments . map ( ( x ) => x . id ) ,
1152+ } ;
1153+ } else {
1154+ segmentInclude = experimentDoc . experimentSegmentInclusion ;
1155+ }
1156+
1157+ const segmentIncludeData : SegmentInputValidator = {
1158+ ...segmentInclude ,
1159+ id : uuid ( ) ,
1160+ name : experiment . id + ' Inclusion Segment' ,
1161+ description : experiment . id + ' Inclusion Segment' ,
1162+ context : experiment . context [ 0 ] ,
1163+ type : SEGMENT_TYPE . PRIVATE ,
11481164 } ;
1165+ try {
1166+ segmentIncludeDoc = await this . segmentService . upsertSegment ( segmentIncludeData , logger ) ;
1167+ } catch ( err ) {
1168+ const error = err as ErrorWithType ;
1169+ error . details = 'Error in adding segment in DB' ;
1170+ error . type = SERVER_ERROR . QUERY_FAILED ;
1171+ logger . error ( error ) ;
1172+ throw error ;
1173+ }
1174+ // creating segmentInclude doc
1175+ const includeTempDoc = new ExperimentSegmentInclusion ( ) ;
1176+ includeTempDoc . segment = segmentIncludeDoc ;
1177+ includeTempDoc . experiment = experimentDoc ;
1178+ segmentIncludeDocToSave = this . getSegmentDoc ( includeTempDoc ) ;
11491179 } else {
1150- segmentInclude = experimentDoc . experimentSegmentInclusion ;
1180+ includeSegmentExists = false ;
11511181 }
11521182
1153- const segmentIncludeData : SegmentInputValidator = {
1154- ...segmentInclude ,
1155- id : uuid ( ) ,
1156- name : experiment . id + ' Inclusion Segment' ,
1157- description : experiment . id + ' Inclusion Segment' ,
1158- context : experiment . context [ 0 ] ,
1159- type : SEGMENT_TYPE . PRIVATE ,
1160- } ;
1161- let segmentIncludeDoc : Segment ;
1162- try {
1163- segmentIncludeDoc = await this . segmentService . upsertSegment ( segmentIncludeData , logger ) ;
1164- } catch ( err ) {
1165- const error = err as ErrorWithType ;
1166- error . details = 'Error in adding segment in DB' ;
1167- error . type = SERVER_ERROR . QUERY_FAILED ;
1168- logger . error ( error ) ;
1169- throw error ;
1170- }
1171-
1172- // creating Exclude Segment
1183+ let excludeSegmentExists = true ;
1184+ let segmentExcludeDoc : Segment ;
1185+ let segmentExcludeDocToSave : Partial < ExperimentSegmentExclusion > = { } ;
11731186 experimentDoc . experimentSegmentExclusion = experimentSegmentExclusion ;
1174- let segmentExclude ;
1175- if ( experimentDoc . experimentSegmentExclusion . segment ) {
1176- const excludeSegment = experimentDoc . experimentSegmentExclusion . segment ;
1177- segmentExclude = {
1178- ...experimentSegmentExclusion ,
1179- type : excludeSegment . type ,
1180- userIds : excludeSegment . individualForSegment . map ( ( x ) => x . userId ) ,
1181- groups : excludeSegment . groupForSegment . map ( ( x ) => {
1182- return { type : x . type , groupId : x . groupId } ;
1183- } ) ,
1184- subSegmentIds : excludeSegment . subSegments . map ( ( x ) => x . id ) ,
1187+ if ( experimentDoc . experimentSegmentExclusion ) {
1188+ // creating Exclude Segment
1189+ let segmentExclude ;
1190+ if ( experimentDoc . experimentSegmentExclusion . segment ) {
1191+ const excludeSegment = experimentDoc . experimentSegmentExclusion . segment ;
1192+ segmentExclude = {
1193+ ...experimentSegmentExclusion ,
1194+ type : excludeSegment . type ,
1195+ userIds : excludeSegment . individualForSegment . map ( ( x ) => x . userId ) ,
1196+ groups : excludeSegment . groupForSegment . map ( ( x ) => {
1197+ return { type : x . type , groupId : x . groupId } ;
1198+ } ) ,
1199+ subSegmentIds : excludeSegment . subSegments . map ( ( x ) => x . id ) ,
1200+ } ;
1201+ } else {
1202+ segmentExclude = experimentDoc . experimentSegmentExclusion ;
1203+ }
1204+
1205+ const segmentExcludeData : SegmentInputValidator = {
1206+ ...segmentExclude ,
1207+ id : uuid ( ) ,
1208+ name : experiment . id + ' Exclusion Segment' ,
1209+ description : experiment . id + ' Exclusion Segment' ,
1210+ context : experiment . context [ 0 ] ,
1211+ type : SEGMENT_TYPE . PRIVATE ,
11851212 } ;
1213+ try {
1214+ segmentExcludeDoc = await this . segmentService . upsertSegment ( segmentExcludeData , logger ) ;
1215+ } catch ( err ) {
1216+ const error = err as ErrorWithType ;
1217+ error . details = 'Error in adding segment in DB' ;
1218+ error . type = SERVER_ERROR . QUERY_FAILED ;
1219+ logger . error ( error ) ;
1220+ throw error ;
1221+ }
1222+ // creating segmentExclude doc
1223+ const excludeTempDoc = new ExperimentSegmentExclusion ( ) ;
1224+ excludeTempDoc . segment = segmentExcludeDoc ;
1225+ excludeTempDoc . experiment = experimentDoc ;
1226+ segmentExcludeDocToSave = this . getSegmentDoc ( excludeTempDoc ) ;
11861227 } else {
1187- segmentExclude = experimentDoc . experimentSegmentExclusion ;
1188- }
1189-
1190- const segmentExcludeData : SegmentInputValidator = {
1191- ...segmentExclude ,
1192- id : uuid ( ) ,
1193- name : experiment . id + ' Exclusion Segment' ,
1194- description : experiment . id + ' Exclusion Segment' ,
1195- context : experiment . context [ 0 ] ,
1196- type : SEGMENT_TYPE . PRIVATE ,
1197- } ;
1198- let segmentExcludeDoc : Segment ;
1199- try {
1200- segmentExcludeDoc = await this . segmentService . upsertSegment ( segmentExcludeData , logger ) ;
1201- } catch ( err ) {
1202- const error = err as ErrorWithType ;
1203- error . details = 'Error in adding segment in DB' ;
1204- error . type = SERVER_ERROR . QUERY_FAILED ;
1205- logger . error ( error ) ;
1206- throw error ;
1228+ excludeSegmentExists = false ;
12071229 }
12081230
12091231 // creating condition docs
@@ -1226,6 +1248,9 @@ export class ExperimentService {
12261248 return decisionPoint ;
12271249 } ) ;
12281250
1251+ if ( ! conditionPayloads ) {
1252+ experiment = { ...experiment , conditionPayloads : [ ] } ;
1253+ }
12291254 // update conditionPayloads condition uuids:
12301255 if ( conditionPayloads ) {
12311256 conditionPayloads . map ( ( conditionPayload ) => {
@@ -1237,7 +1262,6 @@ export class ExperimentService {
12371262 }
12381263 } ) ;
12391264 }
1240-
12411265 const conditionPayloadDocToSave : Array < Partial < ConditionPayload > > =
12421266 ( conditionPayloads &&
12431267 conditionPayloads . length > 0 &&
@@ -1253,22 +1277,12 @@ export class ExperimentService {
12531277 } ) ) ||
12541278 [ ] ;
12551279
1256- // creating segmentInclude doc
1257- const includeTempDoc = new ExperimentSegmentInclusion ( ) ;
1258- includeTempDoc . segment = segmentIncludeDoc ;
1259- includeTempDoc . experiment = experimentDoc ;
1260- const segmentIncludeDocToSave = this . getSegmentDoc ( includeTempDoc ) ;
1261-
1262- // creating segmentExclude doc
1263- const excludeTempDoc = new ExperimentSegmentExclusion ( ) ;
1264- excludeTempDoc . segment = segmentExcludeDoc ;
1265- excludeTempDoc . experiment = experimentDoc ;
1266- const segmentExcludeDocToSave = this . getSegmentDoc ( excludeTempDoc ) ;
12671280 // creating queries docs
12681281 const promiseArray = [ ] ;
12691282 let queryDocsToSave =
1270- ( queries [ 0 ] &&
1283+ ( queries &&
12711284 queries . length > 0 &&
1285+ queries [ 0 ] &&
12721286 queries . map ( ( query : any ) => {
12731287 promiseArray . push ( this . metricRepository . findOne ( query . metric . key ) ) ;
12741288 // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -1307,17 +1321,26 @@ export class ExperimentService {
13071321 ] = await Promise . all ( [
13081322 this . experimentConditionRepository . insertConditions ( conditionDocsToSave , transactionalEntityManager ) ,
13091323 this . decisionPointRepository . insertDecisionPoint ( decisionPointDocsToSave , transactionalEntityManager ) ,
1310- this . experimentSegmentInclusionRepository . insertData (
1311- segmentIncludeDocToSave ,
1312- logger ,
1313- transactionalEntityManager
1314- ) ,
1315- this . experimentSegmentExclusionRepository . insertData (
1316- segmentExcludeDocToSave ,
1317- logger ,
1318- transactionalEntityManager
1319- ) ,
1320- this . conditionPayloadRepository . insertConditionPayload ( conditionPayloadDocToSave , transactionalEntityManager ) ,
1324+ includeSegmentExists
1325+ ? this . experimentSegmentInclusionRepository . insertData (
1326+ segmentIncludeDocToSave ,
1327+ logger ,
1328+ transactionalEntityManager
1329+ )
1330+ : ( Promise . resolve ( [ ] ) as any ) ,
1331+ excludeSegmentExists
1332+ ? this . experimentSegmentExclusionRepository . insertData (
1333+ segmentExcludeDocToSave ,
1334+ logger ,
1335+ transactionalEntityManager
1336+ )
1337+ : ( Promise . resolve ( [ ] ) as any ) ,
1338+ conditionPayloadDocToSave . length > 0
1339+ ? this . conditionPayloadRepository . insertConditionPayload (
1340+ conditionPayloadDocToSave ,
1341+ transactionalEntityManager
1342+ )
1343+ : ( Promise . resolve ( [ ] ) as any ) ,
13211344 queryDocsToSave . length > 0
13221345 ? this . queryRepository . insertQueries ( queryDocsToSave , transactionalEntityManager )
13231346 : ( Promise . resolve ( [ ] ) as any ) ,
@@ -1361,17 +1384,29 @@ export class ExperimentService {
13611384 factorDocToReturn = this . formatingFactorAndLevels ( factorDoc , levelDoc ) ;
13621385 conditionDocToReturn = this . formatingElements ( conditionDocToReturn , levelCombinationElementDoc , levelDoc ) ;
13631386 }
1364-
1365- const newExperiment = {
1366- ...experimentDoc ,
1367- conditions : conditionDocToReturn as any ,
1368- partitions : decisionPointDocToReturn as any ,
1369- factors : factorDocToReturn as any ,
1370- experimentSegmentInclusion : { ...experimentSegmentInclusionDoc , segment : segmentIncludeDoc } as any ,
1371- experimentSegmentExclusion : { ...experimentSegmentExclusionDoc , segment : segmentExcludeDoc } as any ,
1372- conditionPayloads : conditionPayloadDocToReturn as any ,
1373- queries : ( queryDocToReturn as any ) || [ ] ,
1374- } ;
1387+ let newExperimentObject ;
1388+ if ( experimentDoc . experimentSegmentInclusion && experimentDoc . experimentSegmentExclusion ) {
1389+ newExperimentObject = {
1390+ ...experimentDoc ,
1391+ conditions : conditionDocToReturn as any ,
1392+ partitions : decisionPointDocToReturn as any ,
1393+ factors : factorDocToReturn as any ,
1394+ experimentSegmentInclusion : { ...experimentSegmentInclusionDoc , segment : segmentIncludeDoc } as any ,
1395+ experimentSegmentExclusion : { ...experimentSegmentExclusionDoc , segment : segmentExcludeDoc } as any ,
1396+ conditionPayloads : conditionPayloadDocToReturn as any ,
1397+ queries : ( queryDocToReturn as any ) || [ ] ,
1398+ } ;
1399+ } else {
1400+ newExperimentObject = {
1401+ ...experimentDoc ,
1402+ conditions : conditionDocToReturn as any ,
1403+ partitions : decisionPointDocToReturn as any ,
1404+ factors : factorDocToReturn as any ,
1405+ conditionPayloads : conditionPayloadDocToReturn as any ,
1406+ queries : ( queryDocToReturn as any ) || [ ] ,
1407+ } ;
1408+ }
1409+ const newExperiment = newExperimentObject ;
13751410 return newExperiment ;
13761411 } ) ;
13771412 // create schedules to start experiment and end experiment
0 commit comments