Skip to content

Commit

Permalink
feat(db): implement BusinessProfileInterface for MockDb (#2101)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalprahcd authored Sep 9, 2023
1 parent e5ada41 commit 0792605
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 12 deletions.
48 changes: 48 additions & 0 deletions crates/diesel_models/src/business_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,51 @@ pub struct BusinessProfileUpdateInternal {
pub payout_routing_algorithm: Option<serde_json::Value>,
pub is_recon_enabled: Option<bool>,
}

impl From<BusinessProfileNew> for BusinessProfile {
fn from(new: BusinessProfileNew) -> Self {
Self {
profile_id: new.profile_id,
merchant_id: new.merchant_id,
profile_name: new.profile_name,
created_at: new.created_at,
modified_at: new.modified_at,
return_url: new.return_url,
enable_payment_response_hash: new.enable_payment_response_hash,
payment_response_hash_key: new.payment_response_hash_key,
redirect_to_merchant_with_http_post: new.redirect_to_merchant_with_http_post,
webhook_details: new.webhook_details,
metadata: new.metadata,
routing_algorithm: new.routing_algorithm,
intent_fulfillment_time: new.intent_fulfillment_time,
frm_routing_algorithm: new.frm_routing_algorithm,
payout_routing_algorithm: new.payout_routing_algorithm,
is_recon_enabled: new.is_recon_enabled,
}
}
}

impl BusinessProfileUpdateInternal {
pub fn apply_changeset(self, source: BusinessProfile) -> BusinessProfile {
BusinessProfile {
profile_name: self.profile_name.unwrap_or(source.profile_name),
modified_at: self.modified_at.unwrap_or(source.modified_at),
return_url: self.return_url,
enable_payment_response_hash: self
.enable_payment_response_hash
.unwrap_or(source.enable_payment_response_hash),
payment_response_hash_key: self.payment_response_hash_key,
redirect_to_merchant_with_http_post: self
.redirect_to_merchant_with_http_post
.unwrap_or(source.redirect_to_merchant_with_http_post),
webhook_details: self.webhook_details,
metadata: self.metadata,
routing_algorithm: self.routing_algorithm,
intent_fulfillment_time: self.intent_fulfillment_time,
frm_routing_algorithm: self.frm_routing_algorithm,
payout_routing_algorithm: self.payout_routing_algorithm,
is_recon_enabled: self.is_recon_enabled.unwrap_or(source.is_recon_enabled),
..source
}
}
}
76 changes: 64 additions & 12 deletions crates/router/src/db/business_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,90 @@ impl BusinessProfileInterface for Store {
impl BusinessProfileInterface for MockDb {
async fn insert_business_profile(
&self,
_business_profile: business_profile::BusinessProfileNew,
business_profile: business_profile::BusinessProfileNew,
) -> CustomResult<business_profile::BusinessProfile, errors::StorageError> {
Err(errors::StorageError::MockDbError)?
let business_profile_insert = business_profile::BusinessProfile::from(business_profile);
self.business_profiles
.lock()
.await
.push(business_profile_insert.clone());
Ok(business_profile_insert)
}

async fn find_business_profile_by_profile_id(
&self,
_profile_id: &str,
profile_id: &str,
) -> CustomResult<business_profile::BusinessProfile, errors::StorageError> {
Err(errors::StorageError::MockDbError)?
self.business_profiles
.lock()
.await
.iter()
.find(|business_profile| business_profile.profile_id == profile_id)
.ok_or(
errors::StorageError::ValueNotFound(format!(
"No business profile found for profile_id = {}",
profile_id
))
.into(),
)
.cloned()
}

async fn update_business_profile_by_profile_id(
&self,
_current_state: business_profile::BusinessProfile,
_business_profile_update: business_profile::BusinessProfileUpdateInternal,
current_state: business_profile::BusinessProfile,
business_profile_update: business_profile::BusinessProfileUpdateInternal,
) -> CustomResult<business_profile::BusinessProfile, errors::StorageError> {
Err(errors::StorageError::MockDbError)?
self.business_profiles
.lock()
.await
.iter_mut()
.find(|bp| bp.profile_id == current_state.profile_id)
.map(|bp| {
let business_profile_updated =
business_profile_update.apply_changeset(current_state.clone());
*bp = business_profile_updated.clone();
business_profile_updated
})
.ok_or(
errors::StorageError::ValueNotFound(format!(
"No business profile found for profile_id = {}",
current_state.profile_id
))
.into(),
)
}

async fn delete_business_profile_by_profile_id_merchant_id(
&self,
_profile_id: &str,
_merchant_id: &str,
profile_id: &str,
merchant_id: &str,
) -> CustomResult<bool, errors::StorageError> {
Err(errors::StorageError::MockDbError)?
let mut business_profiles = self.business_profiles.lock().await;
let index = business_profiles
.iter()
.position(|bp| bp.profile_id == profile_id && bp.merchant_id == merchant_id)
.ok_or::<errors::StorageError>(errors::StorageError::ValueNotFound(format!(
"No business profile found for profile_id = {} and merchant_id = {}",
profile_id, merchant_id
)))?;
business_profiles.remove(index);
Ok(true)
}

async fn list_business_profile_by_merchant_id(
&self,
_merchant_id: &str,
merchant_id: &str,
) -> CustomResult<Vec<business_profile::BusinessProfile>, errors::StorageError> {
Err(errors::StorageError::MockDbError)?
let business_profile_by_merchant_id = self
.business_profiles
.lock()
.await
.iter()
.filter(|business_profile| business_profile.merchant_id == merchant_id)
.cloned()
.collect();

Ok(business_profile_by_merchant_id)
}
}
2 changes: 2 additions & 0 deletions crates/storage_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ pub struct MockDb {
pub mandates: Arc<Mutex<Vec<store::Mandate>>>,
pub captures: Arc<Mutex<Vec<crate::store::capture::Capture>>>,
pub merchant_key_store: Arc<Mutex<Vec<crate::store::merchant_key_store::MerchantKeyStore>>>,
pub business_profiles: Arc<Mutex<Vec<crate::store::business_profile::BusinessProfile>>>,
}

impl MockDb {
Expand All @@ -263,6 +264,7 @@ impl MockDb {
mandates: Default::default(),
captures: Default::default(),
merchant_key_store: Default::default(),
business_profiles: Default::default(),
}
}
}
Expand Down

0 comments on commit 0792605

Please sign in to comment.