11# frozen_string_literal: true
22
33#
4- # Copyright 2017-2021 , Optimizely and contributors
4+ # Copyright 2017-2022 , Optimizely and contributors
55#
66# Licensed under the Apache License, Version 2.0 (the "License");
77# 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
265265 reasons = [ ]
266266
267267 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 )
269269 reasons . push ( *forced_reasons )
270270
271271 return [ variation [ 'id' ] , reasons ] if variation
@@ -290,7 +290,7 @@ def get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index
290290 skip_to_everyone_else = false
291291 rule = rules [ rule_index ]
292292 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 )
294294 reasons . push ( *forced_reasons )
295295
296296 return [ variation , skip_to_everyone_else , reasons ] if variation
@@ -417,6 +417,28 @@ def get_forced_variation(project_config, experiment_key, user_id)
417417 [ variation , decide_reasons ]
418418 end
419419
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+
420442 private
421443
422444 def get_whitelisted_variation_id ( project_config , experiment_id , user_id )
0 commit comments