Repository: juspay/hyperswitch
File: crates/router/src/core/payment_methods/migration.rs (Rust)

Code before:
                        merchant_context.get_merchant_account().storage_scheme,
                    )
                    .await
                    .to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)?;

                let mut updated_connector_customer_data: HashMap<String, serde_json::Value> =
                    customer
                        .connector_customer
                        .as_ref()
                        .and_then(|cc| serde_json::from_value(cc.peek().clone()).ok())
                        .unwrap_or_default();

                if let Some(ref cache) = mca_data_cache {
                    for (_, mca) in cache.iter() {
                        let key = match mca.connector_type {
                            enums::ConnectorType::PayoutProcessor => {
                                format!(
                                    "{}_{}",
                                    mca.profile_id.get_string_repr(),
                                    mca.connector_name
                                )
                            }
                            _ => mca.merchant_connector_id.get_string_repr().to_string(),
                        };

                        updated_connector_customer_data.insert(
                            key,
                            serde_json::Value::String(connector_customer_id.clone()),
                        );
                    }
                }

                let customer_update = CustomerUpdate::ConnectorCustomer {
                    connector_customer: Some(pii::SecretSerdeValue::new(
                        serde_json::to_value(updated_connector_customer_data)
                            .change_context(errors::ApiErrorResponse::InternalServerError)
                            .attach_printable("Failed to serialize connector customer data")?,
                    )),
                };

                (Some(customer), Some(customer_update))
            }
            _ => (None, None),
        };

    let pm_update = match (&req.payment_instrument_id, &mca_data_cache) {
        (Some(payment_instrument_id), Some(cache)) => {
            let mandate_details = payment_method
                .get_common_mandate_reference()
                .change_context(errors::ApiErrorResponse::InternalServerError)
                .attach_printable("Failed to deserialize to Payment Mandate Reference ")?;

Review comment (suggestion):
We have an existing method for this `payments_core::customers::update_connector_customer_in_customers`

Code after:

            let customer_update =
                build_connector_customer_update(&customer, connector_customer_id, cache)?;

            (Some(customer), Some(customer_update))
        }
        _ => (None, None),
    };

    let pm_update = match (&req.payment_instrument_id, &mca_data_cache) {
        (Some(payment_instrument_id), Some(cache)) => {
            let mandate_details = payment_method
                .get_common_mandate_reference()
                .change_context(errors::ApiErrorResponse::InternalServerError)
                .attach_printable("Failed to deserialize to Payment Mandate Reference ")?;

            let mut existing_payments_mandate = mandate_details
                .payments
                .clone()
                .unwrap_or(PaymentsMandateReference(HashMap::new()));
            let mut existing_payouts_mandate = mandate_details
                .payouts
                .clone()
                .unwrap_or(PayoutsMandateReference(HashMap::new()));

            for (merchant_connector_id, mca) in cache.iter() {
                match mca.connector_type {
                    enums::ConnectorType::PayoutProcessor => {
                        let new_payout_record = PayoutsMandateReferenceRecord {
                            transfer_method_id: Some(payment_instrument_id.peek().to_string()),
                        };

                        if let Some(existing_record) =
                            existing_payouts_mandate.0.get_mut(merchant_connector_id)
                        {
                            if let Some(transfer_method_id) = &new_payout_record.transfer_method_id
                            {
                                existing_record.transfer_method_id =
                                    Some(transfer_method_id.clone());
                            }
                        } else {
                            existing_payouts_mandate
                                .0
                                .insert(merchant_connector_id.clone(), new_payout_record);
                        }
                    }
                    _ => {
                        if let Some(existing_record) =
                            existing_payments_mandate.0.get_mut(merchant_connector_id)
                        {
                            existing_record.connector_mandate_id =