mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-30 01:27:31 +08:00
feat(db): implement RefundInterface for MockDb (#1277)
Co-authored-by: Sanchith Hegde <22217505+SanchithHegde@users.noreply.github.com>
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
use storage_models::errors::DatabaseError;
|
use storage_models::{errors::DatabaseError, refund::RefundUpdateInternal};
|
||||||
|
|
||||||
use super::MockDb;
|
use super::MockDb;
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -604,12 +604,21 @@ mod storage {
|
|||||||
impl RefundInterface for MockDb {
|
impl RefundInterface for MockDb {
|
||||||
async fn find_refund_by_internal_reference_id_merchant_id(
|
async fn find_refund_by_internal_reference_id_merchant_id(
|
||||||
&self,
|
&self,
|
||||||
_internal_reference_id: &str,
|
internal_reference_id: &str,
|
||||||
_merchant_id: &str,
|
merchant_id: &str,
|
||||||
_storage_scheme: enums::MerchantStorageScheme,
|
_storage_scheme: enums::MerchantStorageScheme,
|
||||||
) -> CustomResult<storage_types::Refund, errors::StorageError> {
|
) -> CustomResult<storage_types::Refund, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
let refunds = self.refunds.lock().await;
|
||||||
Err(errors::StorageError::MockDbError)?
|
refunds
|
||||||
|
.iter()
|
||||||
|
.find(|refund| {
|
||||||
|
refund.merchant_id == merchant_id
|
||||||
|
&& refund.internal_reference_id == internal_reference_id
|
||||||
|
})
|
||||||
|
.cloned()
|
||||||
|
.ok_or_else(|| {
|
||||||
|
errors::StorageError::DatabaseError(DatabaseError::NotFound.into()).into()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn insert_refund(
|
async fn insert_refund(
|
||||||
@ -670,12 +679,24 @@ impl RefundInterface for MockDb {
|
|||||||
|
|
||||||
async fn update_refund(
|
async fn update_refund(
|
||||||
&self,
|
&self,
|
||||||
_this: storage_types::Refund,
|
this: storage_types::Refund,
|
||||||
_refund: storage_types::RefundUpdate,
|
refund: storage_types::RefundUpdate,
|
||||||
_storage_scheme: enums::MerchantStorageScheme,
|
_storage_scheme: enums::MerchantStorageScheme,
|
||||||
) -> CustomResult<storage_types::Refund, errors::StorageError> {
|
) -> CustomResult<storage_types::Refund, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
self.refunds
|
||||||
Err(errors::StorageError::MockDbError)?
|
.lock()
|
||||||
|
.await
|
||||||
|
.iter_mut()
|
||||||
|
.find(|refund| this.refund_id == refund.refund_id)
|
||||||
|
.map(|r| {
|
||||||
|
let refund_updated = RefundUpdateInternal::from(refund).create_refund(r.clone());
|
||||||
|
*r = refund_updated.clone();
|
||||||
|
refund_updated
|
||||||
|
})
|
||||||
|
.ok_or_else(|| {
|
||||||
|
errors::StorageError::ValueNotFound("cannot find refund to update".to_string())
|
||||||
|
.into()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_refund_by_merchant_id_refund_id(
|
async fn find_refund_by_merchant_id_refund_id(
|
||||||
@ -719,23 +740,34 @@ impl RefundInterface for MockDb {
|
|||||||
|
|
||||||
async fn find_refund_by_payment_id_merchant_id(
|
async fn find_refund_by_payment_id_merchant_id(
|
||||||
&self,
|
&self,
|
||||||
_payment_id: &str,
|
payment_id: &str,
|
||||||
_merchant_id: &str,
|
merchant_id: &str,
|
||||||
_storage_scheme: enums::MerchantStorageScheme,
|
_storage_scheme: enums::MerchantStorageScheme,
|
||||||
) -> CustomResult<Vec<storage_types::Refund>, errors::StorageError> {
|
) -> CustomResult<Vec<storage_types::Refund>, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
let refunds = self.refunds.lock().await;
|
||||||
Err(errors::StorageError::MockDbError)?
|
|
||||||
|
Ok(refunds
|
||||||
|
.iter()
|
||||||
|
.filter(|refund| refund.merchant_id == merchant_id && refund.payment_id == payment_id)
|
||||||
|
.cloned()
|
||||||
|
.collect::<Vec<_>>())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "olap")]
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_refund_by_constraints(
|
async fn filter_refund_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
_merchant_id: &str,
|
merchant_id: &str,
|
||||||
_refund_details: &api_models::refunds::RefundListRequest,
|
_refund_details: &api_models::refunds::RefundListRequest,
|
||||||
_storage_scheme: enums::MerchantStorageScheme,
|
_storage_scheme: enums::MerchantStorageScheme,
|
||||||
_limit: i64,
|
limit: i64,
|
||||||
) -> CustomResult<Vec<storage_models::refund::Refund>, errors::StorageError> {
|
) -> CustomResult<Vec<storage_models::refund::Refund>, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
let refunds = self.refunds.lock().await;
|
||||||
Err(errors::StorageError::MockDbError)?
|
|
||||||
|
Ok(refunds
|
||||||
|
.iter()
|
||||||
|
.filter(|refund| refund.merchant_id == merchant_id)
|
||||||
|
.take(usize::try_from(limit).unwrap_or(usize::MAX))
|
||||||
|
.cloned()
|
||||||
|
.collect::<Vec<_>>())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -115,6 +115,22 @@ pub struct RefundUpdateInternal {
|
|||||||
refund_error_code: Option<String>,
|
refund_error_code: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RefundUpdateInternal {
|
||||||
|
pub fn create_refund(self, source: Refund) -> Refund {
|
||||||
|
Refund {
|
||||||
|
connector_refund_id: self.connector_refund_id,
|
||||||
|
refund_status: self.refund_status.unwrap_or_default(),
|
||||||
|
sent_to_gateway: self.sent_to_gateway.unwrap_or_default(),
|
||||||
|
refund_error_message: self.refund_error_message,
|
||||||
|
refund_arn: self.refund_arn,
|
||||||
|
metadata: self.metadata,
|
||||||
|
refund_reason: self.refund_reason,
|
||||||
|
refund_error_code: self.refund_error_code,
|
||||||
|
..source
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<RefundUpdate> for RefundUpdateInternal {
|
impl From<RefundUpdate> for RefundUpdateInternal {
|
||||||
fn from(refund_update: RefundUpdate) -> Self {
|
fn from(refund_update: RefundUpdate) -> Self {
|
||||||
match refund_update {
|
match refund_update {
|
||||||
|
|||||||
Reference in New Issue
Block a user