Skip to content

Commit

Permalink
Add new column dispute_currency and read/write on the column
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSagnik007 committed Nov 13, 2024
1 parent 0a506b1 commit 8c724f3
Show file tree
Hide file tree
Showing 27 changed files with 55 additions and 38 deletions.
11 changes: 6 additions & 5 deletions crates/api_models/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use serde::de::Error;
use time::PrimitiveDateTime;
use utoipa::ToSchema;

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

#[derive(Clone, Debug, Serialize, ToSchema, Eq, PartialEq)]
pub struct DisputeResponse {
Expand All @@ -21,7 +21,8 @@ pub struct DisputeResponse {
/// The dispute amount
pub amount: String,
/// The three-letter ISO currency code
pub currency: String,
#[schema(value_type = Currency)]
pub currency: Currency,
/// Stage of the dispute
pub dispute_stage: DisputeStage,
/// Status of the dispute
Expand Down Expand Up @@ -137,7 +138,7 @@ pub struct DisputeListGetConstraints {
pub connector: Option<Vec<String>>,
/// The comma separated list of currencies of the disputes
#[serde(default, deserialize_with = "parse_comma_separated")]
pub currency: Option<Vec<common_enums::Currency>>,
pub currency: Option<Vec<Currency>>,
/// The merchant connector id to filter the disputes list
pub merchant_connector_id: Option<common_utils::id_type::MerchantConnectorAccountId>,
/// The time range for which objects are needed. TimeRange has two fields start_time and end_time from which objects can be filtered as per required scenarios (created_at, time less than, greater than etc).
Expand All @@ -150,7 +151,7 @@ pub struct DisputeListFilters {
/// The map of available connector filters, where the key is the connector name and the value is a list of MerchantConnectorInfo instances
pub connector: HashMap<String, Vec<MerchantConnectorInfo>>,
/// The list of available currency filters
pub currency: Vec<enums::Currency>,
pub currency: Vec<Currency>,
/// The list of available dispute status filters
pub dispute_status: Vec<DisputeStatus>,
/// The list of available dispute stage filters
Expand Down
2 changes: 2 additions & 0 deletions crates/diesel_models/src/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +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,
}

#[derive(Clone, Debug, PartialEq, Serialize, Identifiable, Queryable, Selectable)]
Expand Down Expand Up @@ -61,6 +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,
}

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ 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 @@ -384,6 +383,7 @@ diesel::table! {
dispute_amount -> Int8,
#[max_length = 32]
organization_id -> Varchar,
dispute_currency -> Currency,
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ pub struct AirwallexObjectData {
pub struct AirwallexDisputeObject {
pub payment_intent_id: String,
pub dispute_amount: i64,
pub dispute_currency: String,
pub dispute_currency: enums::Currency,
pub stage: AirwallexDisputeStage,
pub dispute_id: String,
pub dispute_reason_type: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/hyperswitch_interfaces/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct DisputePayload {
/// amount
pub amount: String,
/// currency
pub currency: String,
pub currency: common_enums::enums::Currency,
/// dispute_stage
pub dispute_stage: common_enums::enums::DisputeStage,
/// connector_status
Expand Down
4 changes: 2 additions & 2 deletions 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},
enums::{DisputeStatus, MandateStatus, Currency},
webhooks::{self as api},
};
#[cfg(feature = "payouts")]
Expand Down Expand Up @@ -93,7 +93,7 @@ pub enum StripeWebhookObject {
pub struct StripeDisputeResponse {
pub id: String,
pub amount: String,
pub currency: String,
pub currency: Currency,
pub payment_intent: common_utils::id_type::PaymentId,
pub reason: Option<String>,
pub status: StripeDisputeStatus,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/adyen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,7 @@ impl api::IncomingWebhook for Adyen {
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
Ok(api::disputes::DisputePayload {
amount: notif.amount.value.to_string(),
currency: notif.amount.currency.to_string(),
currency: notif.amount.currency,
dispute_stage: api_models::enums::DisputeStage::from(notif.event_code.clone()),
connector_dispute_id: notif.psp_reference,
connector_reason: notif.reason,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/bluesnap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ impl api::IncomingWebhook for Bluesnap {
dispute_details.invoice_charge_amount.abs().to_string(),
dispute_details.currency,
)?,
currency: dispute_details.currency.to_string(),
currency: dispute_details.currency,
dispute_stage: api_models::enums::DisputeStage::Dispute,
connector_dispute_id: dispute_details.reversal_ref_num,
connector_reason: dispute_details.reversal_reason,
Expand Down
5 changes: 1 addition & 4 deletions crates/router/src/connector/braintree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod transformers;
use std::str::FromStr;

use api_models::webhooks::IncomingWebhookEvent;
use base64::Engine;
Expand Down Expand Up @@ -999,12 +998,10 @@ impl api::IncomingWebhook for Braintree {

match response.dispute {
Some(dispute_data) => {
let currency = enums::Currency::from_str(dispute_data.currency_iso_code.as_str())
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
Ok(api::disputes::DisputePayload {
amount: connector_utils::to_currency_lower_unit(
dispute_data.amount_disputed.to_string(),
currency,
dispute_data.currency_iso_code,
)?,
currency: dispute_data.currency_iso_code,
dispute_stage: transformers::get_dispute_stage(dispute_data.kind.as_str())?,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/braintree/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ pub struct BraintreeDisputeData {
pub amount_won: Option<String>,
pub case_number: Option<String>,
pub chargeback_protection_level: Option<String>,
pub currency_iso_code: String,
pub currency_iso_code: enums::Currency,
#[serde(default, with = "common_utils::custom_serde::iso8601::option")]
pub created_at: Option<PrimitiveDateTime>,
pub evidence: Option<DisputeEvidence>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/checkout/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ pub struct CheckoutDisputeWebhookData {
pub payment_id: Option<String>,
pub action_id: Option<String>,
pub amount: i32,
pub currency: String,
pub currency: enums::Currency,
#[serde(default, with = "common_utils::custom_serde::iso8601::option")]
pub evidence_required_by: Option<PrimitiveDateTime>,
pub reason_code: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/payme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ impl api::IncomingWebhook for Payme {

Ok(api::disputes::DisputePayload {
amount: webhook_object.price.to_string(),
currency: webhook_object.currency.to_string(),
currency: webhook_object.currency,
dispute_stage: api_models::enums::DisputeStage::Dispute,
connector_dispute_id: webhook_object.payme_transaction_id,
connector_reason: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/paypal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ impl api::IncomingWebhook for Paypal {
payload.dispute_amount.value.get_amount_as_string(),
payload.dispute_amount.currency_code,
)?,
currency: payload.dispute_amount.currency_code.to_string(),
currency: payload.dispute_amount.currency_code,
dispute_stage: api_models::enums::DisputeStage::from(
payload.dispute_life_cycle_stage.clone(),
),
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/rapyd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ impl api::IncomingWebhook for Rapyd {
}?;
Ok(api::disputes::DisputePayload {
amount: webhook_dispute_data.amount.to_string(),
currency: webhook_dispute_data.currency.to_string(),
currency: webhook_dispute_data.currency,
dispute_stage: api_models::enums::DisputeStage::Dispute,
connector_dispute_id: webhook_dispute_data.token,
connector_reason: Some(webhook_dispute_data.dispute_reason_description),
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/connector/stripe/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3636,7 +3636,8 @@ pub struct WebhookEventObjectData {
pub id: String,
pub object: WebhookEventObjectType,
pub amount: Option<i32>,
pub currency: String,
#[serde(default, deserialize_with = "connector_util::convert_uppercase")]
pub currency: enums::Currency,
pub payment_intent: Option<String>,
pub client_secret: Option<Secret<String>>,
pub reason: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/trustpay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ pub struct WebhookReferences {
#[serde(rename_all = "PascalCase")]
pub struct WebhookAmount {
pub amount: f64,
pub currency: String,
pub currency: enums::Currency,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down
13 changes: 12 additions & 1 deletion 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};
use masking::{ExposeInterface, Secret, Deserialize};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Serializer;
Expand Down Expand Up @@ -3139,3 +3139,14 @@ impl NetworkTokenData for domain::NetworkTokenData {
Secret::new(year)
}
}

pub fn convert_uppercase<'de, D, T>(v: D) -> Result<T, D::Error>
where
D: serde::Deserializer<'de>,
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug + std::fmt::Display + std::error::Error,
{
use serde::de::Error;
let output = <&str>::deserialize(v)?;
output.to_uppercase().parse::<T>().map_err(D::Error::custom)
}
3 changes: 2 additions & 1 deletion crates/router/src/core/webhooks/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ async fn get_or_update_dispute_object(
let new_dispute = diesel_models::dispute::DisputeNew {
dispute_id,
amount: dispute_details.amount.clone(),
currency: dispute_details.currency,
currency: dispute_details.currency.to_string(),
dispute_stage: dispute_details.dispute_stage,
dispute_status: common_enums::DisputeStatus::foreign_try_from(event_type)
.change_context(errors::ApiErrorResponse::WebhookProcessingFailure)
Expand All @@ -945,6 +945,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,
};
state
.store
Expand Down
4 changes: 3 additions & 1 deletion crates/router/src/db/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl DisputeInterface for MockDb {
merchant_connector_id: dispute.merchant_connector_id,
dispute_amount: dispute.dispute_amount,
organization_id: dispute.organization_id,
dispute_currency: dispute.dispute_currency,
};

locked_disputes.push(new_dispute.clone());
Expand Down Expand Up @@ -310,7 +311,7 @@ impl DisputeInterface for MockDb {
.map_or(true, |currencies| {
currencies
.iter()
.any(|currency| dispute.currency.as_str() == currency.to_string())
.any(|currency| &dispute.dispute_currency == currency)
})
&& dispute_constraints
.time_range
Expand Down Expand Up @@ -502,6 +503,7 @@ mod tests {
merchant_connector_id: None,
dispute_amount: 1040,
organization_id: common_utils::id_type::OrganizationId::default(),
dispute_currency: common_enums::Currency::default(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/services/kafka/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 String,
pub currency: &'a 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,7 @@ impl<'a> KafkaDispute<'a> {
Self {
dispute_id: &dispute.dispute_id,
dispute_amount: dispute.amount.parse::<i64>().unwrap_or_default(),
currency: &dispute.currency,
currency: &dispute.dispute_currency,
dispute_stage: &dispute.dispute_stage,
dispute_status: &dispute.dispute_status,
payment_id: &dispute.payment_id,
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/services/kafka/dispute_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::types::storage::dispute::Dispute;
pub struct KafkaDisputeEvent<'a> {
pub dispute_id: &'a String,
pub dispute_amount: i64,
pub currency: &'a String,
pub currency: &'a 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 +41,7 @@ impl<'a> KafkaDisputeEvent<'a> {
Self {
dispute_id: &dispute.dispute_id,
dispute_amount: dispute.amount.parse::<i64>().unwrap_or_default(),
currency: &dispute.currency,
currency: &dispute.dispute_currency,
dispute_stage: &dispute.dispute_stage,
dispute_status: &dispute.dispute_status,
payment_id: &dispute.payment_id,
Expand Down
8 changes: 1 addition & 7 deletions crates/router/src/types/storage/dispute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ impl DisputeDbExt for Dispute {
if let Some(dispute_status) = &dispute_list_constraints.dispute_status {
filter = filter.filter(dsl::dispute_status.eq_any(dispute_status.clone()));
}

if let Some(currency_list) = &dispute_list_constraints.currency {
let currency: Vec<String> = currency_list
.iter()
.map(|currency| currency.to_string())
.collect();

filter = filter.filter(dsl::currency.eq_any(currency));
filter = filter.filter(dsl::dispute_currency.eq_any(currency_list.clone()));
}
if let Some(merchant_connector_id) = &dispute_list_constraints.merchant_connector_id {
filter = filter.filter(dsl::merchant_connector_id.eq(merchant_connector_id.clone()))
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/types/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ impl ForeignFrom<storage::Dispute> for api_models::disputes::DisputeResponse {
payment_id: dispute.payment_id,
attempt_id: dispute.attempt_id,
amount: dispute.amount,
currency: dispute.currency,
currency: dispute.dispute_currency,
dispute_stage: dispute.dispute_stage,
dispute_status: dispute.dispute_status,
connector: dispute.connector,
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/utils/user/sample_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ 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(),
})
} else {
None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE dispute DROP COLUMN IF EXISTS dispute_currency;
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 TYPE "Currency" USING currency::"Currency"; -- Migration query to be run after deployment before running this query

0 comments on commit 8c724f3

Please sign in to comment.