mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 09:07:09 +08:00
feat(db): Implement AuthorizationInterface for MockDb (#3151)
This commit is contained in:
@ -57,6 +57,21 @@ pub struct AuthorizationUpdateInternal {
|
||||
pub connector_authorization_id: Option<String>,
|
||||
}
|
||||
|
||||
impl AuthorizationUpdateInternal {
|
||||
pub fn create_authorization(self, source: Authorization) -> Authorization {
|
||||
Authorization {
|
||||
status: self.status.unwrap_or(source.status),
|
||||
error_code: self.error_code.or(source.error_code),
|
||||
error_message: self.error_message.or(source.error_message),
|
||||
modified_at: self.modified_at.unwrap_or(common_utils::date_time::now()),
|
||||
connector_authorization_id: self
|
||||
.connector_authorization_id
|
||||
.or(source.connector_authorization_id),
|
||||
..source
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AuthorizationUpdate> for AuthorizationUpdateInternal {
|
||||
fn from(authorization_child_update: AuthorizationUpdate) -> Self {
|
||||
let now = Some(common_utils::date_time::now());
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use diesel_models::authorization::AuthorizationUpdateInternal;
|
||||
use error_stack::IntoReport;
|
||||
|
||||
use super::{MockDb, Store};
|
||||
@ -77,28 +78,71 @@ impl AuthorizationInterface for Store {
|
||||
impl AuthorizationInterface for MockDb {
|
||||
async fn insert_authorization(
|
||||
&self,
|
||||
_authorization: storage::AuthorizationNew,
|
||||
authorization: storage::AuthorizationNew,
|
||||
) -> CustomResult<storage::Authorization, errors::StorageError> {
|
||||
// TODO: Implement function for `MockDb`
|
||||
Err(errors::StorageError::MockDbError)?
|
||||
let mut authorizations = self.authorizations.lock().await;
|
||||
if authorizations.iter().any(|authorization_inner| {
|
||||
authorization_inner.authorization_id == authorization.authorization_id
|
||||
}) {
|
||||
Err(errors::StorageError::DuplicateValue {
|
||||
entity: "authorization_id",
|
||||
key: None,
|
||||
})?
|
||||
}
|
||||
let authorization = storage::Authorization {
|
||||
authorization_id: authorization.authorization_id,
|
||||
merchant_id: authorization.merchant_id,
|
||||
payment_id: authorization.payment_id,
|
||||
amount: authorization.amount,
|
||||
created_at: common_utils::date_time::now(),
|
||||
modified_at: common_utils::date_time::now(),
|
||||
status: authorization.status,
|
||||
error_code: authorization.error_code,
|
||||
error_message: authorization.error_message,
|
||||
connector_authorization_id: authorization.connector_authorization_id,
|
||||
previously_authorized_amount: authorization.previously_authorized_amount,
|
||||
};
|
||||
authorizations.push(authorization.clone());
|
||||
Ok(authorization)
|
||||
}
|
||||
|
||||
async fn find_all_authorizations_by_merchant_id_payment_id(
|
||||
&self,
|
||||
_merchant_id: &str,
|
||||
_payment_id: &str,
|
||||
merchant_id: &str,
|
||||
payment_id: &str,
|
||||
) -> CustomResult<Vec<storage::Authorization>, errors::StorageError> {
|
||||
// TODO: Implement function for `MockDb`
|
||||
Err(errors::StorageError::MockDbError)?
|
||||
let authorizations = self.authorizations.lock().await;
|
||||
let authorizations_found: Vec<storage::Authorization> = authorizations
|
||||
.iter()
|
||||
.filter(|a| a.merchant_id == merchant_id && a.payment_id == payment_id)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
Ok(authorizations_found)
|
||||
}
|
||||
|
||||
async fn update_authorization_by_merchant_id_authorization_id(
|
||||
&self,
|
||||
_merchant_id: String,
|
||||
_authorization_id: String,
|
||||
_authorization: storage::AuthorizationUpdate,
|
||||
merchant_id: String,
|
||||
authorization_id: String,
|
||||
authorization_update: storage::AuthorizationUpdate,
|
||||
) -> CustomResult<storage::Authorization, errors::StorageError> {
|
||||
// TODO: Implement function for `MockDb`
|
||||
Err(errors::StorageError::MockDbError)?
|
||||
let mut authorizations = self.authorizations.lock().await;
|
||||
authorizations
|
||||
.iter_mut()
|
||||
.find(|authorization| authorization.authorization_id == authorization_id && authorization.merchant_id == merchant_id)
|
||||
.map(|authorization| {
|
||||
let authorization_updated =
|
||||
AuthorizationUpdateInternal::from(authorization_update)
|
||||
.create_authorization(authorization.clone());
|
||||
*authorization = authorization_updated.clone();
|
||||
authorization_updated
|
||||
})
|
||||
.ok_or(
|
||||
errors::StorageError::ValueNotFound(format!(
|
||||
"cannot find authorization for authorization_id = {authorization_id} and merchant_id = {merchant_id}"
|
||||
))
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,6 +43,7 @@ pub struct MockDb {
|
||||
pub organizations: Arc<Mutex<Vec<store::organization::Organization>>>,
|
||||
pub users: Arc<Mutex<Vec<store::user::User>>>,
|
||||
pub user_roles: Arc<Mutex<Vec<store::user_role::UserRole>>>,
|
||||
pub authorizations: Arc<Mutex<Vec<store::authorization::Authorization>>>,
|
||||
pub dashboard_metadata: Arc<Mutex<Vec<store::user::dashboard_metadata::DashboardMetadata>>>,
|
||||
}
|
||||
|
||||
@ -79,6 +80,7 @@ impl MockDb {
|
||||
organizations: Default::default(),
|
||||
users: Default::default(),
|
||||
user_roles: Default::default(),
|
||||
authorizations: Default::default(),
|
||||
dashboard_metadata: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user