Skip to content

Commit

Permalink
Add new column dispute_currency
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSagnik007 committed Nov 20, 2024
1 parent 31221af commit d901c8d
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 42 deletions.
2 changes: 1 addition & 1 deletion crates/api_models/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::Error;
use time::PrimitiveDateTime;
use utoipa::ToSchema;

use super::enums::{DisputeStage, DisputeStatus, Currency};
use super::enums::{Currency, DisputeStage, DisputeStatus};
use crate::{admin::MerchantConnectorInfo, files};

#[derive(Clone, Debug, Serialize, ToSchema, Eq, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions crates/diesel_models/src/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct DisputeNew {
pub merchant_connector_id: Option<common_utils::id_type::MerchantConnectorAccountId>,
pub dispute_amount: i64,
pub organization_id: common_utils::id_type::OrganizationId,
pub dispute_currency: storage_enums::Currency,
pub dispute_currency: Option<storage_enums::Currency>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Identifiable, Queryable, Selectable)]
Expand Down Expand Up @@ -62,7 +62,7 @@ pub struct Dispute {
pub merchant_connector_id: Option<common_utils::id_type::MerchantConnectorAccountId>,
pub dispute_amount: i64,
pub organization_id: common_utils::id_type::OrganizationId,
pub dispute_currency: storage_enums::Currency,
pub dispute_currency: Option<storage_enums::Currency>,
}

#[derive(Debug)]
Expand Down
3 changes: 2 additions & 1 deletion crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ diesel::table! {
dispute_id -> Varchar,
#[max_length = 255]
amount -> Varchar,
#[max_length = 255]
currency -> Varchar,
dispute_stage -> DisputeStage,
dispute_status -> DisputeStatus,
Expand Down Expand Up @@ -383,7 +384,7 @@ diesel::table! {
dispute_amount -> Int8,
#[max_length = 32]
organization_id -> Varchar,
dispute_currency -> Currency,
dispute_currency -> Nullable<Currency>,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/diesel_models/src/schema_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ diesel::table! {
dispute_amount -> Int8,
#[max_length = 32]
organization_id -> Varchar,
dispute_currency -> Currency,
dispute_currency -> Nullable<Currency>,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hyperswitch_connectors/src/connectors/novalnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ impl webhooks::IncomingWebhook for Novalnet {
novalnet::get_novalnet_dispute_status(notif.event.event_type).to_string();
Ok(disputes::DisputePayload {
amount: novalnet::option_to_result(amount)?.to_string(),
currency: novalnet::option_to_result(currency)?.to_string(),
currency: novalnet::option_to_result(currency)?,
dispute_stage: api_models::enums::DisputeStage::Dispute,
connector_dispute_id: notif.event.tid.to_string(),
connector_reason: reason,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/compatibility/stripe/webhooks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "payouts")]
use api_models::payouts as payout_models;
use api_models::{
enums::{DisputeStatus, MandateStatus, Currency},
enums::{Currency, DisputeStatus, MandateStatus},
webhooks::{self as api},
};
#[cfg(feature = "payouts")]
Expand Down
32 changes: 15 additions & 17 deletions crates/router/src/connector/braintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,23 +997,21 @@ impl api::IncomingWebhook for Braintree {
let response = decode_webhook_payload(notif.bt_payload.replace('\n', "").as_bytes())?;

match response.dispute {
Some(dispute_data) => {
Ok(api::disputes::DisputePayload {
amount: connector_utils::to_currency_lower_unit(
dispute_data.amount_disputed.to_string(),
dispute_data.currency_iso_code,
)?,
currency: dispute_data.currency_iso_code,
dispute_stage: transformers::get_dispute_stage(dispute_data.kind.as_str())?,
connector_dispute_id: dispute_data.id,
connector_reason: dispute_data.reason,
connector_reason_code: dispute_data.reason_code,
challenge_required_by: dispute_data.reply_by_date,
connector_status: dispute_data.status,
created_at: dispute_data.created_at,
updated_at: dispute_data.updated_at,
})
}
Some(dispute_data) => Ok(api::disputes::DisputePayload {
amount: connector_utils::to_currency_lower_unit(
dispute_data.amount_disputed.to_string(),
dispute_data.currency_iso_code,
)?,
currency: dispute_data.currency_iso_code,
dispute_stage: transformers::get_dispute_stage(dispute_data.kind.as_str())?,
connector_dispute_id: dispute_data.id,
connector_reason: dispute_data.reason,
connector_reason_code: dispute_data.reason_code,
challenge_required_by: dispute_data.reply_by_date,
connector_status: dispute_data.status,
created_at: dispute_data.created_at,
updated_at: dispute_data.updated_at,
}),
None => Err(errors::ConnectorError::WebhookResourceObjectNotFound)?,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use hyperswitch_domain_models::{
SyncIntegrityObject,
},
};
use masking::{ExposeInterface, Secret, Deserialize};
use masking::{Deserialize, ExposeInterface, Secret};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Serializer;
Expand Down Expand Up @@ -3153,4 +3153,4 @@ where
use serde::de::Error;
let output = <&str>::deserialize(v)?;
output.to_uppercase().parse::<T>().map_err(D::Error::custom)
}
}
2 changes: 1 addition & 1 deletion crates/router/src/core/webhooks/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ async fn get_or_update_dispute_object(
merchant_connector_id: payment_attempt.merchant_connector_id.clone(),
dispute_amount: dispute_details.amount.parse::<i64>().unwrap_or(0),
organization_id: organization_id.clone(),
dispute_currency: dispute_details.currency,
dispute_currency: Some(dispute_details.currency),
};
state
.store
Expand Down
11 changes: 7 additions & 4 deletions crates/router/src/db/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,12 @@ impl DisputeInterface for MockDb {
.currency
.as_ref()
.map_or(true, |currencies| {
currencies
.iter()
.any(|currency| &dispute.dispute_currency == currency)
currencies.iter().any(|currency| {
dispute
.dispute_currency
.map(|dispute_currency| &dispute_currency == currency)
.unwrap_or(dispute.currency.as_str() == currency.to_string())
})
})
&& dispute_constraints
.time_range
Expand Down Expand Up @@ -503,7 +506,7 @@ mod tests {
merchant_connector_id: None,
dispute_amount: 1040,
organization_id: common_utils::id_type::OrganizationId::default(),
dispute_currency: common_enums::Currency::default(),
dispute_currency: Some(common_enums::Currency::default()),
}
}

Expand Down
12 changes: 9 additions & 3 deletions crates/router/src/services/kafka/dispute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use common_utils::id_type;
use common_utils::{ext_traits::StringExt, id_type};
use diesel_models::enums as storage_enums;
use masking::Secret;
use time::OffsetDateTime;
Expand All @@ -9,7 +9,7 @@ use crate::types::storage::dispute::Dispute;
pub struct KafkaDispute<'a> {
pub dispute_id: &'a String,
pub dispute_amount: i64,
pub currency: &'a storage_enums::Currency,
pub currency: storage_enums::Currency,
pub dispute_stage: &'a storage_enums::DisputeStage,
pub dispute_status: &'a storage_enums::DisputeStatus,
pub payment_id: &'a id_type::PaymentId,
Expand Down Expand Up @@ -41,7 +41,13 @@ impl<'a> KafkaDispute<'a> {
Self {
dispute_id: &dispute.dispute_id,
dispute_amount: dispute.amount.parse::<i64>().unwrap_or_default(),
currency: &dispute.dispute_currency,
currency: dispute.dispute_currency.unwrap_or(
dispute
.currency
.to_uppercase()
.parse_enum("Currency")
.unwrap_or_default(),
),
dispute_stage: &dispute.dispute_stage,
dispute_status: &dispute.dispute_status,
payment_id: &dispute.payment_id,
Expand Down
11 changes: 9 additions & 2 deletions crates/router/src/services/kafka/dispute_event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_utils::ext_traits::StringExt;
use diesel_models::enums as storage_enums;
use masking::Secret;
use time::OffsetDateTime;
Expand All @@ -9,7 +10,7 @@ use crate::types::storage::dispute::Dispute;
pub struct KafkaDisputeEvent<'a> {
pub dispute_id: &'a String,
pub dispute_amount: i64,
pub currency: &'a storage_enums::Currency,
pub currency: storage_enums::Currency,
pub dispute_stage: &'a storage_enums::DisputeStage,
pub dispute_status: &'a storage_enums::DisputeStatus,
pub payment_id: &'a common_utils::id_type::PaymentId,
Expand Down Expand Up @@ -41,7 +42,13 @@ impl<'a> KafkaDisputeEvent<'a> {
Self {
dispute_id: &dispute.dispute_id,
dispute_amount: dispute.amount.parse::<i64>().unwrap_or_default(),
currency: &dispute.dispute_currency,
currency: dispute.dispute_currency.unwrap_or(
dispute
.currency
.to_uppercase()
.parse_enum("Currency")
.unwrap_or_default(),
),
dispute_stage: &dispute.dispute_stage,
dispute_status: &dispute.dispute_status,
payment_id: &dispute.payment_id,
Expand Down
8 changes: 7 additions & 1 deletion crates/router/src/types/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,13 @@ impl ForeignFrom<storage::Dispute> for api_models::disputes::DisputeResponse {
payment_id: dispute.payment_id,
attempt_id: dispute.attempt_id,
amount: dispute.amount,
currency: dispute.dispute_currency,
currency: dispute.dispute_currency.unwrap_or(
dispute
.currency
.to_uppercase()
.parse_enum("Currency")
.unwrap_or_default(),
),
dispute_stage: dispute.dispute_stage,
dispute_status: dispute.dispute_status,
connector: dispute.connector,
Expand Down
4 changes: 1 addition & 3 deletions crates/router/src/utils/user/sample_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,7 @@ pub async fn generate_sample_data(
merchant_connector_id: payment_attempt.merchant_connector_id.clone(),
dispute_amount: amount * 100,
organization_id: org_id.clone(),
dispute_currency: payment_intent
.currency
.unwrap_or_default(),
dispute_currency: Some(payment_intent.currency.unwrap_or_default()),
})
} else {
None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE dispute ADD COLUMN IF NOT EXISTS dispute_currency "Currency";

This file was deleted.

0 comments on commit d901c8d

Please sign in to comment.