diff --git a/services/feature-flags/src/main.rs b/services/feature-flags/src/main.rs --- a/services/feature-flags/src/main.rs +++ b/services/feature-flags/src/main.rs @@ -5,6 +5,7 @@ pub mod config; pub mod constants; pub mod database; +pub mod service; fn configure_logging() -> Result<()> { let filter = EnvFilter::builder() diff --git a/services/feature-flags/src/service.rs b/services/feature-flags/src/service.rs new file mode 100644 --- /dev/null +++ b/services/feature-flags/src/service.rs @@ -0,0 +1,56 @@ +use crate::database::{DatabaseClient, Error, FeatureConfig, Platform}; +use std::collections::HashSet; + +pub struct FeatureFlagsService { + _db: DatabaseClient, +} + +impl FeatureFlagsService { + pub fn new(db_client: DatabaseClient) -> Self { + FeatureFlagsService { _db: db_client } + } + + pub async fn _enabled_features_set( + db: &DatabaseClient, + platform: Platform, + code_version: i32, + is_staff: bool, + ) -> Result, Error> { + let features_config = db.get_features_configuration(platform).await?; + Ok( + features_config + .into_values() + .filter_map(|config| { + Self::_check_if_feature_is_enabled(code_version, is_staff, config) + }) + .collect(), + ) + } + + fn _check_if_feature_is_enabled( + code_version: i32, + is_staff: bool, + feature_config: FeatureConfig, + ) -> Option { + feature_config + .config + .keys() + .filter(|version| version.clone() <= &code_version) + .max() + .and_then(|version| feature_config.config.get(version)) + .map(|config| { + if is_staff { + config.staff + } else { + config.non_staff + } + }) + .and_then(|is_enabled| { + if is_enabled { + Some(feature_config.name) + } else { + None + } + }) + } +}