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

Code before:
                        )
                    } else {
                        None
                    }
                }
                _ => None,
            }
        }
        None => None,
    }
    .transpose()?;

    let mca_ids = parse_merchant_connector_ids(req.merchant_connector_ids.clone());

    // Process mandate details when both payment_instrument_id and merchant_connector_ids are present
    let pm_update = match (&req.payment_instrument_id, &mca_ids) {
        (Some(payment_instrument_id), Some(merchant_connector_ids)) => {
            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_else(|| PaymentsMandateReference(HashMap::new()));
            let mut existing_payouts_mandate = mandate_details
                .payouts
                .clone()
                .unwrap_or_else(|| PayoutsMandateReference(HashMap::new()));

            for merchant_connector_id in merchant_connector_ids {
                let mca = db
                    .find_by_merchant_connector_account_merchant_id_merchant_connector_id(
                        &state.into(),
                        merchant_context.get_merchant_account().get_id(),
                        merchant_connector_id,
                        merchant_context.get_merchant_key_store(),
                    )
                    .await
                    .to_not_found_response(
                        errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
                            id: merchant_connector_id.get_string_repr().to_string(),
                        },
                    )?;

                match mca.connector_type {
                    enums::ConnectorType::PayoutProcessor => {
                        // Handle PayoutsMandateReference
                        let new_payout_record = PayoutsMandateReferenceRecord {
                            transfer_method_id: Some(payment_instrument_id.peek().to_string()),

Review comment (suggestion):
```suggestion
                .unwrap_or(PaymentsMandateReference(HashMap::new()));
```

Code after:
        &req.payment_instrument_id,
        &req.merchant_connector_ids.clone(),
    ) {
        (Some(payment_instrument_id), Some(merchant_connector_ids)) => {
            let parsed_mca_ids =
                MerchantConnectorValidator::parse_comma_separated_ids(merchant_connector_ids)?;
            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 in parsed_mca_ids {
                let mca = db
                    .find_by_merchant_connector_account_merchant_id_merchant_connector_id(
                        &state.into(),
                        merchant_context.get_merchant_account().get_id(),
                        &merchant_connector_id,
                        merchant_context.get_merchant_key_store(),
                    )
                    .await
                    .to_not_found_response(
                        errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
                            id: merchant_connector_id.get_string_repr().to_string(),
                        },
                    )?;

                match mca.connector_type {
                    enums::ConnectorType::PayoutProcessor => {
                        // Handle PayoutsMandateReference
                        let new_payout_record = PayoutsMandateReferenceRecord {
                            transfer_method_id: Some(payment_instrument_id.peek().to_string()),
                        };

                        // Check if record exists for this merchant_connector_id
                        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());
                            }