11/**
2- * Copyright 2017-2022, 2024, Optimizely
2+ * Copyright 2017-2022, 2024-2025 , Optimizely
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ export interface DecisionObj {
110110}
111111
112112interface DecisionServiceOptions {
113- userProfileService : UserProfileService | null ;
113+ userProfileService ? : UserProfileService ;
114114 logger ?: LoggerFacade ;
115115 UNSTABLE_conditionEvaluators : unknown ;
116116}
@@ -143,13 +143,13 @@ export class DecisionService {
143143 private logger ?: LoggerFacade ;
144144 private audienceEvaluator : AudienceEvaluator ;
145145 private forcedVariationMap : { [ key : string ] : { [ id : string ] : string } } ;
146- private userProfileService : UserProfileService | null ;
146+ private userProfileService ? : UserProfileService ;
147147
148148 constructor ( options : DecisionServiceOptions ) {
149149 this . logger = options . logger ;
150150 this . audienceEvaluator = createAudienceEvaluator ( options . UNSTABLE_conditionEvaluators , this . logger ) ;
151151 this . forcedVariationMap = { } ;
152- this . userProfileService = options . userProfileService || null ;
152+ this . userProfileService = options . userProfileService ;
153153 }
154154
155155 /**
@@ -170,18 +170,22 @@ export class DecisionService {
170170 ) : DecisionResponse < string | null > {
171171 const userId = user . getUserId ( ) ;
172172 const attributes = user . getAttributes ( ) ;
173+
173174 // by default, the bucketing ID should be the user ID
174175 const bucketingId = this . getBucketingId ( userId , attributes ) ;
175- const decideReasons : ( string | number ) [ ] [ ] = [ ] ;
176176 const experimentKey = experiment . key ;
177- if ( ! this . checkIfExperimentIsActive ( configObj , experimentKey ) ) {
177+
178+ const decideReasons : ( string | number ) [ ] [ ] = [ ] ;
179+
180+ if ( ! isActive ( configObj , experimentKey ) ) {
178181 this . logger ?. info ( EXPERIMENT_NOT_RUNNING , experimentKey ) ;
179182 decideReasons . push ( [ EXPERIMENT_NOT_RUNNING , experimentKey ] ) ;
180183 return {
181184 result : null ,
182185 reasons : decideReasons ,
183186 } ;
184187 }
188+
185189 const decisionForcedVariation = this . getForcedVariation ( configObj , experimentKey , userId ) ;
186190 decideReasons . push ( ...decisionForcedVariation . reasons ) ;
187191 const forcedVariationKey = decisionForcedVariation . result ;
@@ -192,6 +196,7 @@ export class DecisionService {
192196 reasons : decideReasons ,
193197 } ;
194198 }
199+
195200 const decisionWhitelistedVariation = this . getWhitelistedVariation ( experiment , userId ) ;
196201 decideReasons . push ( ...decisionWhitelistedVariation . reasons ) ;
197202 let variation = decisionWhitelistedVariation . result ;
@@ -202,7 +207,6 @@ export class DecisionService {
202207 } ;
203208 }
204209
205-
206210 // check for sticky bucketing if decide options do not include shouldIgnoreUPS
207211 if ( ! shouldIgnoreUPS ) {
208212 variation = this . getStoredVariation ( configObj , experiment , userId , userProfileTracker . userProfile ) ;
@@ -349,16 +353,6 @@ export class DecisionService {
349353 return { ...userProfile . experiment_bucket_map , ...attributeExperimentBucketMap as any } ;
350354 }
351355
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-
362356 /**
363357 * Checks if user is whitelisted into any variation and return that variation if so
364358 * @param {Experiment } experiment
@@ -621,7 +615,7 @@ export class DecisionService {
621615 isProfileUpdated : false ,
622616 userProfile : null ,
623617 }
624- const shouldIgnoreUPS = options [ OptimizelyDecideOption . IGNORE_USER_PROFILE_SERVICE ] ;
618+ const shouldIgnoreUPS = ! ! options [ OptimizelyDecideOption . IGNORE_USER_PROFILE_SERVICE ] ;
625619
626620 if ( ! shouldIgnoreUPS ) {
627621 userProfileTracker . userProfile = this . resolveExperimentBucketMap ( userId , attributes ) ;
@@ -661,10 +655,10 @@ export class DecisionService {
661655 }
662656
663657 if ( ! shouldIgnoreUPS ) {
664- this . saveUserProfile ( userId , userProfileTracker )
658+ this . saveUserProfile ( userId , userProfileTracker ) ;
665659 }
666660
667- return decisions
661+ return decisions ;
668662
669663 }
670664
@@ -968,7 +962,7 @@ export class DecisionService {
968962 * @param {string } experimentKey Key representing the experiment id
969963 * @throws If the user id is not valid or not in the forced variation map
970964 */
971- removeForcedVariation ( userId : string , experimentId : string , experimentKey : string ) : void {
965+ private removeForcedVariation ( userId : string , experimentId : string , experimentKey : string ) : void {
972966 if ( ! userId ) {
973967 throw new OptimizelyError ( INVALID_USER_ID ) ;
974968 }
@@ -1176,7 +1170,7 @@ export class DecisionService {
11761170 }
11771171 }
11781172
1179- getVariationFromExperimentRule (
1173+ private getVariationFromExperimentRule (
11801174 configObj : ProjectConfig ,
11811175 flagKey : string ,
11821176 rule : Experiment ,
@@ -1207,7 +1201,7 @@ export class DecisionService {
12071201 } ;
12081202 }
12091203
1210- getVariationFromDeliveryRule (
1204+ private getVariationFromDeliveryRule (
12111205 configObj : ProjectConfig ,
12121206 flagKey : string ,
12131207 rules : Experiment [ ] ,
0 commit comments