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

Code before:
        .switch()?;

    #[cfg(feature = "v1")]
    let customers = domain_customers
        .into_iter()
        .map(|domain_customer| customers::CustomerResponse::foreign_from((domain_customer, None)))
        .collect();

    #[cfg(feature = "v2")]
    let customers = domain_customers
        .into_iter()
        .map(customers::CustomerResponse::foreign_from)
        .collect();

    Ok(services::ApplicationResponse::Json(customers))
}

#[instrument(skip(state))]
pub async fn list_customers_with_count(
    state: SessionState,
    merchant_id: id_type::MerchantId,
    key_store: domain::MerchantKeyStore,
    request: customers::CustomerListRequestWithConstraints,
) -> errors::CustomerResponse<customers::CustomerListResponse> {
    let db = state.store.as_ref();
    let limit = utils::customer_validation::validate_customer_list_limit(request.limit)
        .change_context(errors::CustomersErrorResponse::InvalidRequestData {
            message: format!(
            " limit should be between {CUSTOMER_LIST_LOWER_LIMIT} and {CUSTOMER_LIST_UPPER_LIMIT}"
        ),
        })?;

    let customer_list_constraints = crate::db::customers::CustomerListConstraints {
        limit: request
            .limit
            .unwrap_or_else(|| limit.get_value().unwrap_or(20)),
        offset: request.offset,
        customer_id: request.customer_id,
        time_range: request.time_range,
    };

    let domain_customers = db
        .list_customers_by_merchant_id_with_count(
            &(&state).into(),
            &merchant_id,
            &key_store,
            customer_list_constraints,
        )
        .await
        .switch()?;


Review comment (suggestion):
CustomerListRequestWithConstraints::limit should be of type `CustomerListLimit`

Code after:

    #[cfg(feature = "v1")]
    let customers = domain_customers
        .into_iter()
        .map(|domain_customer| customers::CustomerResponse::foreign_from((domain_customer, None)))
        .collect();

    #[cfg(feature = "v2")]
    let customers = domain_customers
        .into_iter()
        .map(customers::CustomerResponse::foreign_from)
        .collect();

    Ok(services::ApplicationResponse::Json(customers))
}

#[instrument(skip(state))]
pub async fn list_customers_with_count(
    state: SessionState,
    merchant_id: id_type::MerchantId,
    key_store: domain::MerchantKeyStore,
    request: customers::CustomerListRequestWithConstraints,
) -> errors::CustomerResponse<customers::CustomerListResponse> {
    let db = state.store.as_ref();
    let customer_list_constraints = crate::db::customers::CustomerListConstraints {
        limit: request
            .limit
            .map(|l| *l)
            .unwrap_or_else(|| *CustomerListLimit::default()),
        offset: request.offset,
        customer_id: request.customer_id,
        time_range: request.time_range,
    };

    let domain_customers = db
        .list_customers_by_merchant_id_with_count(
            &(&state).into(),
            &merchant_id,
            &key_store,
            customer_list_constraints,
        )
        .await
        .switch()?;

    #[cfg(feature = "v1")]
    let customers: Vec<customers::CustomerResponse> = domain_customers
        .0
        .into_iter()
        .map(|domain_customer| customers::CustomerResponse::foreign_from((domain_customer, None)))
        .collect();