1
1
# frozen_string_literal: true
2
2
3
3
#
4
- # Copyright 2017-2021 , Optimizely and contributors
4
+ # Copyright 2017-2022 , Optimizely and contributors
5
5
#
6
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
7
# you may not use this file except in compliance with the License.
@@ -265,7 +265,7 @@ def get_variation_from_experiment_rule(project_config, flag_key, rule, user, opt
265
265
reasons = [ ]
266
266
267
267
context = Optimizely ::OptimizelyUserContext ::OptimizelyDecisionContext . new ( flag_key , rule [ 'key' ] )
268
- variation , forced_reasons = user . find_validated_forced_decision ( context )
268
+ variation , forced_reasons = validated_forced_decision ( project_config , context , user )
269
269
reasons . push ( *forced_reasons )
270
270
271
271
return [ variation [ 'id' ] , reasons ] if variation
@@ -290,7 +290,7 @@ def get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index
290
290
skip_to_everyone_else = false
291
291
rule = rules [ rule_index ]
292
292
context = Optimizely ::OptimizelyUserContext ::OptimizelyDecisionContext . new ( flag_key , rule [ 'key' ] )
293
- variation , forced_reasons = user . find_validated_forced_decision ( context )
293
+ variation , forced_reasons = validated_forced_decision ( project_config , context , user )
294
294
reasons . push ( *forced_reasons )
295
295
296
296
return [ variation , skip_to_everyone_else , reasons ] if variation
@@ -417,6 +417,28 @@ def get_forced_variation(project_config, experiment_key, user_id)
417
417
[ variation , decide_reasons ]
418
418
end
419
419
420
+ def validated_forced_decision ( project_config , context , user_context )
421
+ decision = user_context . get_forced_decision ( context )
422
+ flag_key = context [ :flag_key ]
423
+ rule_key = context [ :rule_key ]
424
+ variation_key = decision ? decision [ :variation_key ] : decision
425
+ reasons = [ ]
426
+ target = rule_key ? "flag (#{ flag_key } ), rule (#{ rule_key } )" : "flag (#{ flag_key } )"
427
+ if variation_key
428
+ variation = project_config . get_variation_from_flag ( flag_key , variation_key , 'key' )
429
+ if variation
430
+ reason = "Variation (#{ variation_key } ) is mapped to #{ target } and user (#{ user_context . user_id } ) in the forced decision map."
431
+ reasons . push ( reason )
432
+ return variation , reasons
433
+ else
434
+ reason = "Invalid variation is mapped to #{ target } and user (#{ user_context . user_id } ) in the forced decision map."
435
+ reasons . push ( reason )
436
+ end
437
+ end
438
+
439
+ [ nil , reasons ]
440
+ end
441
+
420
442
private
421
443
422
444
def get_whitelisted_variation_id ( project_config , experiment_id , user_id )
0 commit comments