mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-04 14:07:18 +08:00 
			
		
		
		
	feat(sample_data): generate random disputes for sample data (#6341)
This commit is contained in:
		@ -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<PaymentIntent>,
 | 
			
		||||
        Vec<diesel_models::user::sample_data::PaymentAttemptBatchNew>,
 | 
			
		||||
        Vec<RefundNew>,
 | 
			
		||||
        Vec<DisputeNew>,
 | 
			
		||||
    ) = 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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<Vec<(PaymentIntent, PaymentAttemptBatchNew, Option<RefundNew>)>> {
 | 
			
		||||
) -> SampleDataResult<
 | 
			
		||||
    Vec<(
 | 
			
		||||
        PaymentIntent,
 | 
			
		||||
        PaymentAttemptBatchNew,
 | 
			
		||||
        Option<RefundNew>,
 | 
			
		||||
        Option<DisputeNew>,
 | 
			
		||||
    )>,
 | 
			
		||||
> {
 | 
			
		||||
    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<usize> = (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<RefundNew>)> = Vec::new();
 | 
			
		||||
    let mut res: Vec<(
 | 
			
		||||
        PaymentIntent,
 | 
			
		||||
        PaymentAttemptBatchNew,
 | 
			
		||||
        Option<RefundNew>,
 | 
			
		||||
        Option<DisputeNew>,
 | 
			
		||||
    )> = 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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user