mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-01 02:57:02 +08:00 
			
		
		
		
	feat(db): implement ConfigInterface for MockDb (#1586)
				
					
				
			This commit is contained in:
		 Panagiotis Ganelis
					Panagiotis Ganelis
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							d528132932
						
					
				
				
					commit
					2ac1f2e29e
				
			| @ -98,6 +98,7 @@ impl StorageInterface for Store {} | |||||||
| #[derive(Clone)] | #[derive(Clone)] | ||||||
| pub struct MockDb { | pub struct MockDb { | ||||||
|     addresses: Arc<Mutex<Vec<storage::Address>>>, |     addresses: Arc<Mutex<Vec<storage::Address>>>, | ||||||
|  |     configs: Arc<Mutex<Vec<storage::Config>>>, | ||||||
|     merchant_accounts: Arc<Mutex<Vec<storage::MerchantAccount>>>, |     merchant_accounts: Arc<Mutex<Vec<storage::MerchantAccount>>>, | ||||||
|     merchant_connector_accounts: Arc<Mutex<Vec<storage::MerchantConnectorAccount>>>, |     merchant_connector_accounts: Arc<Mutex<Vec<storage::MerchantConnectorAccount>>>, | ||||||
|     payment_attempts: Arc<Mutex<Vec<storage::PaymentAttempt>>>, |     payment_attempts: Arc<Mutex<Vec<storage::PaymentAttempt>>>, | ||||||
| @ -121,6 +122,7 @@ impl MockDb { | |||||||
|     pub async fn new(redis: &crate::configs::settings::Settings) -> Self { |     pub async fn new(redis: &crate::configs::settings::Settings) -> Self { | ||||||
|         Self { |         Self { | ||||||
|             addresses: Default::default(), |             addresses: Default::default(), | ||||||
|  |             configs: Default::default(), | ||||||
|             merchant_accounts: Default::default(), |             merchant_accounts: Default::default(), | ||||||
|             merchant_connector_accounts: Default::default(), |             merchant_connector_accounts: Default::default(), | ||||||
|             payment_attempts: Default::default(), |             payment_attempts: Default::default(), | ||||||
|  | |||||||
| @ -1,4 +1,5 @@ | |||||||
| use error_stack::IntoReport; | use error_stack::IntoReport; | ||||||
|  | use storage_models::configs::ConfigUpdateInternal; | ||||||
|  |  | ||||||
| use super::{cache, MockDb, Store}; | use super::{cache, MockDb, Store}; | ||||||
| use crate::{ | use crate::{ | ||||||
| @ -113,47 +114,107 @@ impl ConfigInterface for Store { | |||||||
| impl ConfigInterface for MockDb { | impl ConfigInterface for MockDb { | ||||||
|     async fn insert_config( |     async fn insert_config( | ||||||
|         &self, |         &self, | ||||||
|         _config: storage::ConfigNew, |         config: storage::ConfigNew, | ||||||
|     ) -> CustomResult<storage::Config, errors::StorageError> { |     ) -> CustomResult<storage::Config, errors::StorageError> { | ||||||
|         // [#172]: Implement function for `MockDb` |         let mut configs = self.configs.lock().await; | ||||||
|         Err(errors::StorageError::MockDbError)? |  | ||||||
|  |         let config_new = storage::Config { | ||||||
|  |             #[allow(clippy::as_conversions)] | ||||||
|  |             id: configs.len() as i32, | ||||||
|  |             key: config.key, | ||||||
|  |             config: config.config, | ||||||
|  |         }; | ||||||
|  |         configs.push(config_new.clone()); | ||||||
|  |         Ok(config_new) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async fn find_config_by_key( |     async fn find_config_by_key( | ||||||
|         &self, |         &self, | ||||||
|         _key: &str, |         key: &str, | ||||||
|     ) -> CustomResult<storage::Config, errors::StorageError> { |     ) -> CustomResult<storage::Config, errors::StorageError> { | ||||||
|         // [#172]: Implement function for `MockDb` |         let configs = self.configs.lock().await; | ||||||
|         Err(errors::StorageError::MockDbError)? |         let config = configs.iter().find(|c| c.key == key).cloned(); | ||||||
|  |  | ||||||
|  |         config.ok_or_else(|| { | ||||||
|  |             errors::StorageError::ValueNotFound("cannot find config".to_string()).into() | ||||||
|  |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async fn update_config_by_key( |     async fn update_config_by_key( | ||||||
|         &self, |         &self, | ||||||
|         _key: &str, |         key: &str, | ||||||
|         _config_update: storage::ConfigUpdate, |         config_update: storage::ConfigUpdate, | ||||||
|     ) -> CustomResult<storage::Config, errors::StorageError> { |     ) -> CustomResult<storage::Config, errors::StorageError> { | ||||||
|         // [#172]: Implement function for `MockDb` |         let result = self | ||||||
|         Err(errors::StorageError::MockDbError)? |             .configs | ||||||
|  |             .lock() | ||||||
|  |             .await | ||||||
|  |             .iter_mut() | ||||||
|  |             .find(|c| c.key == key) | ||||||
|  |             .ok_or_else(|| { | ||||||
|  |                 errors::StorageError::ValueNotFound("cannot find config to update".to_string()) | ||||||
|  |                     .into() | ||||||
|  |             }) | ||||||
|  |             .map(|c| { | ||||||
|  |                 let config_updated = | ||||||
|  |                     ConfigUpdateInternal::from(config_update).create_config(c.clone()); | ||||||
|  |                 *c = config_updated.clone(); | ||||||
|  |                 config_updated | ||||||
|  |             }); | ||||||
|  |  | ||||||
|  |         result | ||||||
|     } |     } | ||||||
|     async fn update_config_cached( |     async fn update_config_cached( | ||||||
|         &self, |         &self, | ||||||
|         _key: &str, |         key: &str, | ||||||
|         _config_update: storage::ConfigUpdate, |         config_update: storage::ConfigUpdate, | ||||||
|     ) -> CustomResult<storage::Config, errors::StorageError> { |     ) -> CustomResult<storage::Config, errors::StorageError> { | ||||||
|         // [#172]: Implement function for `MockDb` |         let result = self | ||||||
|         Err(errors::StorageError::MockDbError)? |             .configs | ||||||
|  |             .lock() | ||||||
|  |             .await | ||||||
|  |             .iter_mut() | ||||||
|  |             .find(|c| c.key == key) | ||||||
|  |             .ok_or_else(|| { | ||||||
|  |                 errors::StorageError::ValueNotFound("cannot find config to update".to_string()) | ||||||
|  |                     .into() | ||||||
|  |             }) | ||||||
|  |             .map(|c| { | ||||||
|  |                 let config_updated = | ||||||
|  |                     ConfigUpdateInternal::from(config_update).create_config(c.clone()); | ||||||
|  |                 *c = config_updated.clone(); | ||||||
|  |                 config_updated | ||||||
|  |             }); | ||||||
|  |  | ||||||
|  |         result | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async fn delete_config_by_key(&self, _key: &str) -> CustomResult<bool, errors::StorageError> { |     async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> { | ||||||
|         // [#172]: Implement function for `MockDb` |         let mut configs = self.configs.lock().await; | ||||||
|         Err(errors::StorageError::MockDbError)? |         let result = configs | ||||||
|  |             .iter() | ||||||
|  |             .position(|c| c.key == key) | ||||||
|  |             .map(|index| { | ||||||
|  |                 configs.remove(index); | ||||||
|  |                 true | ||||||
|  |             }) | ||||||
|  |             .ok_or_else(|| { | ||||||
|  |                 errors::StorageError::ValueNotFound("cannot find config to delete".to_string()) | ||||||
|  |                     .into() | ||||||
|  |             }); | ||||||
|  |  | ||||||
|  |         result | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async fn find_config_by_key_cached( |     async fn find_config_by_key_cached( | ||||||
|         &self, |         &self, | ||||||
|         _key: &str, |         key: &str, | ||||||
|     ) -> CustomResult<storage::Config, errors::StorageError> { |     ) -> CustomResult<storage::Config, errors::StorageError> { | ||||||
|         // [#172]: Implement function for `MockDb` |         let configs = self.configs.lock().await; | ||||||
|         Err(errors::StorageError::MockDbError)? |         let config = configs.iter().find(|c| c.key == key).cloned(); | ||||||
|  |  | ||||||
|  |         config.ok_or_else(|| { | ||||||
|  |             errors::StorageError::ValueNotFound("cannot find config".to_string()).into() | ||||||
|  |         }) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -34,6 +34,12 @@ pub struct ConfigUpdateInternal { | |||||||
|     config: Option<String>, |     config: Option<String>, | ||||||
| } | } | ||||||
|  |  | ||||||
|  | impl ConfigUpdateInternal { | ||||||
|  |     pub fn create_config(self, source: Config) -> Config { | ||||||
|  |         Config { ..source } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| impl From<ConfigUpdate> for ConfigUpdateInternal { | impl From<ConfigUpdate> for ConfigUpdateInternal { | ||||||
|     fn from(config_update: ConfigUpdate) -> Self { |     fn from(config_update: ConfigUpdate) -> Self { | ||||||
|         match config_update { |         match config_update { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user