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

Code before:

impl ConfirmSubscriptionRequest {
    pub fn get_item_price_id(&self) -> Result<String, error_stack::Report<ValidationError>> {
        self.item_price_id.clone().ok_or(error_stack::report!(
            ValidationError::MissingRequiredField {
                field_name: "item_price_id".to_string()
            }
        ))
    }

    pub fn get_billing_address(&self) -> Result<Address, error_stack::Report<ValidationError>> {
        let billing = self.payment_details.payment_method_data.billing.clone();

        if let Some(billing) = billing {
            Ok(billing)
        } else {
            self.payment_details
                .billing
                .clone()
                .ok_or(error_stack::report!(
                    ValidationError::MissingRequiredField {
                        field_name: "billing".to_string()
                    }
                ))
        }
    }
}

impl ApiEventMetric for ConfirmSubscriptionRequest {}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct CreateAndConfirmSubscriptionRequest {
    /// Amount to be charged for the invoice.
    pub amount: Option<MinorUnit>,

    /// Currency for the amount.
    pub currency: Option<api_enums::Currency>,

    /// Identifier for the associated plan_id.
    pub plan_id: Option<String>,

    /// Identifier for the associated item_price_id for the subscription.
    pub item_price_id: Option<String>,

    /// Idenctifier for the coupon code for the subscription.
    pub coupon_code: Option<String>,

    /// Identifier for customer.
    pub customer_id: common_utils::id_type::CustomerId,

    /// Billing address for the subscription.

Review comment (suggestion):
```suggestion
self.payment_details
    .payment_method_data
    .billing
    .as_ref()
    .or(self.payment_details.billing.as_ref())
    .cloned()
    .ok_or_else(|| {
        error_stack::report!(ValidationError::MissingRequiredField {
            field_name: "billing".to_string()
        })
    })
```

Code after:

    /// Identifier for the associated plan_id.
    pub plan_id: Option<String>,

    /// Identifier for the associated item_price_id for the subscription.
    pub item_price_id: Option<String>,

    /// Idenctifier for the coupon code for the subscription.
    pub coupon_code: Option<String>,

    /// Payment details for the invoice.
    pub payment_details: ConfirmSubscriptionPaymentDetails,
}

impl ConfirmSubscriptionRequest {
    pub fn get_item_price_id(&self) -> Result<String, error_stack::Report<ValidationError>> {
        self.item_price_id.clone().ok_or(error_stack::report!(
            ValidationError::MissingRequiredField {
                field_name: "item_price_id".to_string()
            }
        ))
    }

    pub fn get_billing_address(&self) -> Option<Address> {
        self.payment_details
            .payment_method_data
            .billing
            .as_ref()
            .or(self.payment_details.billing.as_ref())
            .cloned()
    }
}

impl ApiEventMetric for ConfirmSubscriptionRequest {}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct CreateAndConfirmSubscriptionRequest {
    /// Amount to be charged for the invoice.
    pub amount: Option<MinorUnit>,

    /// Currency for the amount.
    pub currency: Option<api_enums::Currency>,

    /// Identifier for the associated plan_id.
    pub plan_id: Option<String>,

    /// Identifier for the associated item_price_id for the subscription.
    pub item_price_id: Option<String>,

    /// Idenctifier for the coupon code for the subscription.
    pub coupon_code: Option<String>,