1
1
/**
2
- * Copyright 2017-2022, 2024, Optimizely
2
+ * Copyright 2017-2022, 2024-2025 , Optimizely
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ export interface DecisionObj {
110
110
}
111
111
112
112
interface DecisionServiceOptions {
113
- userProfileService : UserProfileService | null ;
113
+ userProfileService ? : UserProfileService ;
114
114
logger ?: LoggerFacade ;
115
115
UNSTABLE_conditionEvaluators : unknown ;
116
116
}
@@ -143,13 +143,13 @@ export class DecisionService {
143
143
private logger ?: LoggerFacade ;
144
144
private audienceEvaluator : AudienceEvaluator ;
145
145
private forcedVariationMap : { [ key : string ] : { [ id : string ] : string } } ;
146
- private userProfileService : UserProfileService | null ;
146
+ private userProfileService ? : UserProfileService ;
147
147
148
148
constructor ( options : DecisionServiceOptions ) {
149
149
this . logger = options . logger ;
150
150
this . audienceEvaluator = createAudienceEvaluator ( options . UNSTABLE_conditionEvaluators , this . logger ) ;
151
151
this . forcedVariationMap = { } ;
152
- this . userProfileService = options . userProfileService || null ;
152
+ this . userProfileService = options . userProfileService ;
153
153
}
154
154
155
155
/**
@@ -170,18 +170,22 @@ export class DecisionService {
170
170
) : DecisionResponse < string | null > {
171
171
const userId = user . getUserId ( ) ;
172
172
const attributes = user . getAttributes ( ) ;
173
+
173
174
// by default, the bucketing ID should be the user ID
174
175
const bucketingId = this . getBucketingId ( userId , attributes ) ;
175
- const decideReasons : ( string | number ) [ ] [ ] = [ ] ;
176
176
const experimentKey = experiment . key ;
177
- if ( ! this . checkIfExperimentIsActive ( configObj , experimentKey ) ) {
177
+
178
+ const decideReasons : ( string | number ) [ ] [ ] = [ ] ;
179
+
180
+ if ( ! isActive ( configObj , experimentKey ) ) {
178
181
this . logger ?. info ( EXPERIMENT_NOT_RUNNING , experimentKey ) ;
179
182
decideReasons . push ( [ EXPERIMENT_NOT_RUNNING , experimentKey ] ) ;
180
183
return {
181
184
result : null ,
182
185
reasons : decideReasons ,
183
186
} ;
184
187
}
188
+
185
189
const decisionForcedVariation = this . getForcedVariation ( configObj , experimentKey , userId ) ;
186
190
decideReasons . push ( ...decisionForcedVariation . reasons ) ;
187
191
const forcedVariationKey = decisionForcedVariation . result ;
@@ -192,6 +196,7 @@ export class DecisionService {
192
196
reasons : decideReasons ,
193
197
} ;
194
198
}
199
+
195
200
const decisionWhitelistedVariation = this . getWhitelistedVariation ( experiment , userId ) ;
196
201
decideReasons . push ( ...decisionWhitelistedVariation . reasons ) ;
197
202
let variation = decisionWhitelistedVariation . result ;
@@ -202,7 +207,6 @@ export class DecisionService {
202
207
} ;
203
208
}
204
209
205
-
206
210
// check for sticky bucketing if decide options do not include shouldIgnoreUPS
207
211
if ( ! shouldIgnoreUPS ) {
208
212
variation = this . getStoredVariation ( configObj , experiment , userId , userProfileTracker . userProfile ) ;
@@ -349,16 +353,6 @@ export class DecisionService {
349
353
return { ...userProfile . experiment_bucket_map , ...attributeExperimentBucketMap as any } ;
350
354
}
351
355
352
- /**
353
- * Checks whether the experiment is running
354
- * @param {ProjectConfig } configObj The parsed project configuration object
355
- * @param {string } experimentKey Key of experiment being validated
356
- * @return {boolean } True if experiment is running
357
- */
358
- private checkIfExperimentIsActive ( configObj : ProjectConfig , experimentKey : string ) : boolean {
359
- return isActive ( configObj , experimentKey ) ;
360
- }
361
-
362
356
/**
363
357
* Checks if user is whitelisted into any variation and return that variation if so
364
358
* @param {Experiment } experiment
@@ -621,7 +615,7 @@ export class DecisionService {
621
615
isProfileUpdated : false ,
622
616
userProfile : null ,
623
617
}
624
- const shouldIgnoreUPS = options [ OptimizelyDecideOption . IGNORE_USER_PROFILE_SERVICE ] ;
618
+ const shouldIgnoreUPS = ! ! options [ OptimizelyDecideOption . IGNORE_USER_PROFILE_SERVICE ] ;
625
619
626
620
if ( ! shouldIgnoreUPS ) {
627
621
userProfileTracker . userProfile = this . resolveExperimentBucketMap ( userId , attributes ) ;
@@ -661,10 +655,10 @@ export class DecisionService {
661
655
}
662
656
663
657
if ( ! shouldIgnoreUPS ) {
664
- this . saveUserProfile ( userId , userProfileTracker )
658
+ this . saveUserProfile ( userId , userProfileTracker ) ;
665
659
}
666
660
667
- return decisions
661
+ return decisions ;
668
662
669
663
}
670
664
@@ -968,7 +962,7 @@ export class DecisionService {
968
962
* @param {string } experimentKey Key representing the experiment id
969
963
* @throws If the user id is not valid or not in the forced variation map
970
964
*/
971
- removeForcedVariation ( userId : string , experimentId : string , experimentKey : string ) : void {
965
+ private removeForcedVariation ( userId : string , experimentId : string , experimentKey : string ) : void {
972
966
if ( ! userId ) {
973
967
throw new OptimizelyError ( INVALID_USER_ID ) ;
974
968
}
@@ -1176,7 +1170,7 @@ export class DecisionService {
1176
1170
}
1177
1171
}
1178
1172
1179
- getVariationFromExperimentRule (
1173
+ private getVariationFromExperimentRule (
1180
1174
configObj : ProjectConfig ,
1181
1175
flagKey : string ,
1182
1176
rule : Experiment ,
@@ -1207,7 +1201,7 @@ export class DecisionService {
1207
1201
} ;
1208
1202
}
1209
1203
1210
- getVariationFromDeliveryRule (
1204
+ private getVariationFromDeliveryRule (
1211
1205
configObj : ProjectConfig ,
1212
1206
flagKey : string ,
1213
1207
rules : Experiment [ ] ,
0 commit comments