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

Code before:
        .store
        .find_business_profile_by_profile_id(key_manager_state, merchant_key_store, &profile_id)
        .await
        .change_context(errors::ApiErrorResponse::ProfileNotFound {
            id: profile_id.get_string_repr().to_string(),
        })?;

    let subscription_handler = subscription_utils::SubscriptionHandler::new(
        state.clone(),
        merchant_context.clone(),
        profile,
    );

    if let Some(client_secret) = query.client_secret {
        subscription_handler
            .find_and_validate_subscription(&client_secret.into())
            .await?
    };

    let subscription_with_handler =
        subscription_utils::SubscriptionWithHandler::new(&subscription_handler, None);

    let billing_handler = subscription_with_handler
        .get_billing_handler(None, None)
        .await?;
    let get_plans_response = billing_handler.get_subscription_plans(&state).await?;

    let mut response = Vec::new();

    for plan in &get_plans_response.list {
        let plan_price_response = billing_handler
            .get_subscription_plan_prices(&state, plan.subscription_provider_plan_id.clone())
            .await?;

        response.push(subscription_types::GetPlansResponse {
            plan_id: plan.subscription_provider_plan_id.clone(),
            name: plan.name.clone(),
            description: plan.description.clone(),
            price_id: plan_price_response
                .list
                .into_iter()
                .map(subscription_types::SubscriptionPlanPrices::from)
                .collect::<Vec<_>>(),
        })
    }

    Ok(ApplicationResponse::Json(response))
}

pub async fn confirm_subscription(
    state: SessionState,

Review comment (refactor):
Let us not try to get all plans at once,  accept limit and offset in the query_param and filter only based on the limit. 
if not provided use the default limit of 10 as done in `get_chargebee_plans_query_params ` move this to const file, 

Code after:

pub async fn get_subscription_plans(
    state: SessionState,
    merchant_context: MerchantContext,
    profile_id: common_utils::id_type::ProfileId,
    query: subscription_types::GetPlansQuery,
) -> RouterResponse<Vec<subscription_types::GetPlansResponse>> {
    let profile =
        SubscriptionHandler::find_business_profile(&state, &merchant_context, &profile_id)
            .await
            .attach_printable("subscriptions: failed to find business profile")?;

    let subscription_handler = SubscriptionHandler::new(&state, &merchant_context);

    if let Some(client_secret) = query.client_secret {
        subscription_handler
            .find_and_validate_subscription(&client_secret.into())
            .await?
    };

    let billing_handler = BillingHandler::create(
        &state,
        merchant_context.get_merchant_account(),
        merchant_context.get_merchant_key_store(),
        None,
        profile.clone(),
    )
    .await?;

    let get_plans_response = billing_handler
        .get_subscription_plans(&state, query.limit, query.offset)
        .await?;

    let mut response = Vec::new();

    for plan in &get_plans_response.list {
        let plan_price_response = billing_handler
            .get_subscription_plan_prices(&state, plan.subscription_provider_plan_id.clone())
            .await?;

        response.push(subscription_types::GetPlansResponse {
            plan_id: plan.subscription_provider_plan_id.clone(),
            name: plan.name.clone(),
            description: plan.description.clone(),
            price_id: plan_price_response
                .list
                .into_iter()
                .map(subscription_types::SubscriptionPlanPrices::from)
                .collect::<Vec<_>>(),
        })
    }