feat(db): implement AddressInterface for MockDb (#968)

Co-authored-by: jeeva <jeeva.ramu@codurance.com>
This commit is contained in:
Jeeva
2023-05-19 10:10:59 +01:00
committed by GitHub
parent 985670da9c
commit 39405bb478
4 changed files with 114 additions and 15 deletions

View File

@ -73,6 +73,7 @@ impl StorageInterface for Store {}
#[derive(Clone)]
pub struct MockDb {
addresses: Arc<Mutex<Vec<storage::Address>>>,
merchant_accounts: Arc<Mutex<Vec<storage::MerchantAccount>>>,
merchant_connector_accounts: Arc<Mutex<Vec<storage::MerchantConnectorAccount>>>,
payment_attempts: Arc<Mutex<Vec<storage::PaymentAttempt>>>,
@ -88,6 +89,7 @@ pub struct MockDb {
impl MockDb {
pub async fn new(redis: &crate::configs::settings::Settings) -> Self {
Self {
addresses: Default::default(),
merchant_accounts: Default::default(),
merchant_connector_accounts: Default::default(),
payment_attempts: Default::default(),

View File

@ -1,4 +1,5 @@
use error_stack::IntoReport;
use storage_models::address::AddressUpdateInternal;
use super::{MockDb, Store};
use crate::{
@ -93,36 +94,110 @@ impl AddressInterface for Store {
impl AddressInterface for MockDb {
async fn find_address(
&self,
_address_id: &str,
address_id: &str,
) -> CustomResult<storage::Address, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
match self
.addresses
.lock()
.await
.iter()
.find(|address| address.address_id == address_id)
{
Some(address) => return Ok(address.clone()),
None => {
return Err(
errors::StorageError::ValueNotFound("address not found".to_string()).into(),
)
}
}
}
async fn update_address(
&self,
_address_id: String,
_address: storage::AddressUpdate,
address_id: String,
address_update: storage::AddressUpdate,
) -> CustomResult<storage::Address, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
match self
.addresses
.lock()
.await
.iter_mut()
.find(|address| address.address_id == address_id)
.map(|a| {
let address_updated =
AddressUpdateInternal::from(address_update).create_address(a.clone());
*a = address_updated.clone();
address_updated
}) {
Some(address_updated) => Ok(address_updated),
None => {
return Err(errors::StorageError::ValueNotFound(
"cannot find address to update".to_string(),
)
.into())
}
}
}
async fn insert_address(
&self,
_address: storage::AddressNew,
address_new: storage::AddressNew,
) -> CustomResult<storage::Address, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
let mut addresses = self.addresses.lock().await;
let now = common_utils::date_time::now();
let address = storage::Address {
#[allow(clippy::as_conversions)]
id: addresses.len() as i32,
address_id: address_new.address_id,
city: address_new.city,
country: address_new.country,
line1: address_new.line1,
line2: address_new.line2,
line3: address_new.line3,
state: address_new.state,
zip: address_new.zip,
first_name: address_new.first_name,
last_name: address_new.last_name,
phone_number: address_new.phone_number,
country_code: address_new.country_code,
created_at: now,
modified_at: now,
customer_id: address_new.customer_id,
merchant_id: address_new.merchant_id,
};
addresses.push(address.clone());
Ok(address)
}
async fn update_address_by_merchant_id_customer_id(
&self,
_customer_id: &str,
_merchant_id: &str,
_address: storage::AddressUpdate,
customer_id: &str,
merchant_id: &str,
address_update: storage::AddressUpdate,
) -> CustomResult<Vec<storage::Address>, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
match self
.addresses
.lock()
.await
.iter_mut()
.find(|address| {
address.customer_id == customer_id && address.merchant_id == merchant_id
})
.map(|a| {
let address_updated =
AddressUpdateInternal::from(address_update).create_address(a.clone());
*a = address_updated.clone();
address_updated
}) {
Some(address) => Ok(vec![address]),
None => {
return Err(
errors::StorageError::ValueNotFound("address not found".to_string()).into(),
)
}
}
}
}