From e36ea184ae6d1363fb1af55c790162df9f8b451c Mon Sep 17 00:00:00 2001 From: Tommy shu Date: Fri, 25 Oct 2024 04:15:28 -0400 Subject: [PATCH] feat(sample_data): generate random disputes for sample data (#6341) --- crates/router/src/core/user/sample_data.rs | 24 ++++++-- crates/router/src/utils/user/sample_data.rs | 63 +++++++++++++++++++-- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/crates/router/src/core/user/sample_data.rs b/crates/router/src/core/user/sample_data.rs index 453b3edf00f..d098d1f76d9 100644 --- a/crates/router/src/core/user/sample_data.rs +++ b/crates/router/src/core/user/sample_data.rs @@ -1,6 +1,6 @@ use api_models::user::sample_data::SampleDataRequest; use common_utils::errors::ReportSwitchExt; -use diesel_models::RefundNew; +use diesel_models::{DisputeNew, RefundNew}; use error_stack::ResultExt; use hyperswitch_domain_models::payments::PaymentIntent; @@ -39,19 +39,23 @@ pub async fn generate_sample_data_for_user( .change_context(SampleDataError::InternalServerError) .attach_printable("Not able to fetch merchant key store")?; // If not able to fetch merchant key store for any reason, this should be an internal server error - let (payment_intents, payment_attempts, refunds): ( + let (payment_intents, payment_attempts, refunds, disputes): ( Vec, Vec, Vec, + Vec, ) = sample_data.into_iter().fold( - (Vec::new(), Vec::new(), Vec::new()), - |(mut pi, mut pa, mut rf), (payment_intent, payment_attempt, refund)| { + (Vec::new(), Vec::new(), Vec::new(), Vec::new()), + |(mut pi, mut pa, mut rf, mut dp), (payment_intent, payment_attempt, refund, dispute)| { pi.push(payment_intent); pa.push(payment_attempt); if let Some(refund) = refund { rf.push(refund); } - (pi, pa, rf) + if let Some(dispute) = dispute { + dp.push(dispute); + } + (pi, pa, rf, dp) }, ); @@ -70,6 +74,11 @@ pub async fn generate_sample_data_for_user( .insert_refunds_batch_for_sample_data(refunds) .await .switch()?; + state + .store + .insert_disputes_batch_for_sample_data(disputes) + .await + .switch()?; Ok(ApplicationResponse::StatusOk) } @@ -109,6 +118,11 @@ pub async fn delete_sample_data_for_user( .delete_refunds_for_sample_data(&merchant_id_del) .await .switch()?; + state + .store + .delete_disputes_for_sample_data(&merchant_id_del) + .await + .switch()?; Ok(ApplicationResponse::StatusOk) } diff --git a/crates/router/src/utils/user/sample_data.rs b/crates/router/src/utils/user/sample_data.rs index ea2556a5151..4f859d8e56a 100644 --- a/crates/router/src/utils/user/sample_data.rs +++ b/crates/router/src/utils/user/sample_data.rs @@ -8,7 +8,7 @@ use common_utils::{ }; #[cfg(feature = "v1")] use diesel_models::user::sample_data::PaymentAttemptBatchNew; -use diesel_models::RefundNew; +use diesel_models::{enums as storage_enums, DisputeNew, RefundNew}; use error_stack::ResultExt; use hyperswitch_domain_models::payments::PaymentIntent; use rand::{prelude::SliceRandom, thread_rng, Rng}; @@ -27,7 +27,14 @@ pub async fn generate_sample_data( req: SampleDataRequest, merchant_id: &id_type::MerchantId, org_id: &id_type::OrganizationId, -) -> SampleDataResult)>> { +) -> SampleDataResult< + Vec<( + PaymentIntent, + PaymentAttemptBatchNew, + Option, + Option, + )>, +> { let sample_data_size: usize = req.record.unwrap_or(100); let key_manager_state = &state.into(); if !(10..=100).contains(&sample_data_size) { @@ -120,13 +127,23 @@ pub async fn generate_sample_data( let mut refunds_count = 0; + // 2 disputes if generated data size is between 50 and 100, 1 dispute if it is less than 50. + let number_of_disputes: usize = if sample_data_size >= 50 { 2 } else { 1 }; + + let mut disputes_count = 0; + let mut random_array: Vec = (1..=sample_data_size).collect(); // Shuffle the array let mut rng = thread_rng(); random_array.shuffle(&mut rng); - let mut res: Vec<(PaymentIntent, PaymentAttemptBatchNew, Option)> = Vec::new(); + let mut res: Vec<( + PaymentIntent, + PaymentAttemptBatchNew, + Option, + Option, + )> = Vec::new(); let start_time = req .start_time .unwrap_or(common_utils::date_time::now() - time::Duration::days(7)) @@ -353,7 +370,7 @@ pub async fn generate_sample_data( internal_reference_id: common_utils::generate_id_with_default_len("test"), external_reference_id: None, payment_id: payment_id.clone(), - attempt_id, + attempt_id: attempt_id.clone(), merchant_id: merchant_id.clone(), connector_transaction_id, connector_refund_id: None, @@ -387,7 +404,43 @@ pub async fn generate_sample_data( None }; - res.push((payment_intent, payment_attempt, refund)); + let dispute = + if disputes_count < number_of_disputes && !is_failed_payment && refund.is_none() { + disputes_count += 1; + Some(DisputeNew { + dispute_id: common_utils::generate_id_with_default_len("test"), + amount: (amount * 100).to_string(), + currency: payment_intent + .currency + .unwrap_or(common_enums::Currency::USD) + .to_string(), + dispute_stage: storage_enums::DisputeStage::Dispute, + dispute_status: storage_enums::DisputeStatus::DisputeOpened, + payment_id: payment_id.clone(), + attempt_id: attempt_id.clone(), + merchant_id: merchant_id.clone(), + connector_status: "Sample connector status".into(), + connector_dispute_id: common_utils::generate_id_with_default_len("test"), + connector_reason: Some("Sample Dispute".into()), + connector_reason_code: Some("123".into()), + challenge_required_by: None, + connector_created_at: None, + connector_updated_at: None, + connector: payment_attempt + .connector + .clone() + .unwrap_or(DummyConnector4.to_string()), + evidence: None, + profile_id: payment_intent.profile_id.clone(), + merchant_connector_id: payment_attempt.merchant_connector_id.clone(), + dispute_amount: amount * 100, + organization_id: org_id.clone(), + }) + } else { + None + }; + + res.push((payment_intent, payment_attempt, refund, dispute)); } Ok(res) }