Skip to content

Commit

Permalink
nit fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanmaji4 committed Jan 7, 2025
1 parent 3222033 commit acd3a20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
20 changes: 10 additions & 10 deletions crates/api_models/src/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,25 @@ pub struct PaymentsMandateReference(
pub HashMap<id_type::MerchantConnectorAccountId, PaymentsMandateReferenceRecord>,
);

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct PayoutsMandateReference(
pub HashMap<id_type::MerchantConnectorAccountId, PayoutsMandateReferenceRecord>,
);

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct PayoutsMandateReferenceRecord {
pub transfer_method_id: Option<String>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct PaymentsMandateReferenceRecord {
pub connector_mandate_id: String,
pub payment_method_type: Option<common_enums::PaymentMethodType>,
pub original_payment_authorized_amount: Option<i64>,
pub original_payment_authorized_currency: Option<common_enums::Currency>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct CommonMandateReference {
pub payments: Option<PaymentsMandateReference>,
pub payouts: Option<PayoutsMandateReference>,
Expand Down Expand Up @@ -353,17 +353,17 @@ where
{
let value: Option<serde_json::Value> = Option::<serde_json::Value>::deserialize(deserializer)?;
if let Some(connector_mandate_value) = value {
if let Ok(common_mandate) =
serde_json::from_value::<CommonMandateReference>(connector_mandate_value.clone())
{
Ok(Some(common_mandate))
} else if let Ok(payment_mandate_record) =
serde_json::from_value::<PaymentsMandateReference>(connector_mandate_value)
if let Ok(payment_mandate_record) =
serde_json::from_value::<PaymentsMandateReference>(connector_mandate_value.clone())
{
Ok(Some(CommonMandateReference {
payments: Some(payment_mandate_record),
payouts: None,
}))
} else if let Ok(common_mandate) =
serde_json::from_value::<CommonMandateReference>(connector_mandate_value)
{
Ok(Some(common_mandate))
} else {
Err(de::Error::custom(
"Failed to deserialize connector_mandate_details",
Expand Down
18 changes: 7 additions & 11 deletions crates/diesel_models/src/payment_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ impl std::ops::DerefMut for PaymentsMandateReference {

common_utils::impl_to_sql_from_sql_json!(PaymentsMandateReference);

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct PayoutsMandateReferenceRecord {
pub transfer_method_id: Option<String>,
}
Expand Down Expand Up @@ -1048,24 +1048,20 @@ where
serde_json::Value: diesel::deserialize::FromSql<diesel::sql_types::Jsonb, DB>,
{
fn from_sql(bytes: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
// Deserialize into a generic serde_json::Value first
let value = <serde_json::Value as diesel::deserialize::FromSql<
diesel::sql_types::Jsonb,
DB,
>>::from_sql(bytes)?;

// Try to directly deserialize into CommonMandateReference
if let Ok(common_reference) = serde_json::from_value::<Self>(value.clone()) {
return Ok(common_reference);
}

// If that fails, try deserializing into PaymentsMandateReference
if let Ok(payment_reference) = serde_json::from_value::<PaymentsMandateReference>(value) {
// Convert PaymentsMandateReference to CommonMandateReference
if let Ok(payment_reference) =
serde_json::from_value::<PaymentsMandateReference>(value.clone())
{
return Ok(Self::from(payment_reference));
}

// If neither succeeds, return an error
if let Ok(common_reference) = serde_json::from_value::<Self>(value) {
return Ok(common_reference);
}
Err(
report!(ParsingError::StructParseFailure("CommonMandateReference")).attach_printable(
"Failed to parse JSON into CommonMandateReference or PaymentsMandateReference",
Expand Down

0 comments on commit acd3a20

Please sign in to comment.