use serde::{Deserialize, Serialize};
use crate::filters::common::ListFilters;
use crate::filters::plan::PlanFilters;
use crate::models::{ChargeModel, PaginationParams, PlanInterval};
#[derive(Debug, Clone)]
pub struct ListPlansRequest {
pub pagination: PaginationParams,
pub filters: PlanFilters,
}
impl ListPlansRequest {
pub fn new() -> Self {
Self {
pagination: PaginationParams::default(),
filters: PlanFilters::default(),
}
}
pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
self.pagination = pagination;
self
}
pub fn with_filters(mut self, filters: PlanFilters) -> Self {
self.filters = filters;
self
}
pub fn to_query_params(&self) -> Vec<(&str, String)> {
let mut params = self.pagination.to_query_params();
params.extend(self.filters.to_query_params());
params
}
}
impl Default for ListPlansRequest {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct GetPlanRequest {
pub code: String,
}
impl GetPlanRequest {
pub fn new(code: String) -> Self {
Self { code }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreatePlanChargeInput {
pub billable_metric_id: String,
pub charge_model: ChargeModel,
#[serde(skip_serializing_if = "Option::is_none")]
pub invoiceable: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub invoice_display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pay_in_advance: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prorated: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub min_amount_cents: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_codes: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub filters: Option<Vec<CreateChargeFilterInput>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub regroup_paid_fees: Option<String>,
}
impl CreatePlanChargeInput {
pub fn new(billable_metric_id: String, charge_model: ChargeModel) -> Self {
Self {
billable_metric_id,
charge_model,
invoiceable: None,
invoice_display_name: None,
pay_in_advance: None,
prorated: None,
min_amount_cents: None,
properties: None,
tax_codes: None,
filters: None,
regroup_paid_fees: None,
}
}
pub fn with_invoiceable(mut self, invoiceable: bool) -> Self {
self.invoiceable = Some(invoiceable);
self
}
pub fn with_invoice_display_name(mut self, name: String) -> Self {
self.invoice_display_name = Some(name);
self
}
pub fn with_pay_in_advance(mut self, pay_in_advance: bool) -> Self {
self.pay_in_advance = Some(pay_in_advance);
self
}
pub fn with_prorated(mut self, prorated: bool) -> Self {
self.prorated = Some(prorated);
self
}
pub fn with_min_amount_cents(mut self, min_amount_cents: i64) -> Self {
self.min_amount_cents = Some(min_amount_cents);
self
}
pub fn with_properties(mut self, properties: serde_json::Value) -> Self {
self.properties = Some(properties);
self
}
pub fn with_tax_codes(mut self, tax_codes: Vec<String>) -> Self {
self.tax_codes = Some(tax_codes);
self
}
pub fn with_filters(mut self, filters: Vec<CreateChargeFilterInput>) -> Self {
self.filters = Some(filters);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateChargeFilterInput {
#[serde(skip_serializing_if = "Option::is_none")]
pub invoice_display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub values: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateMinimumCommitmentInput {
pub amount_cents: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub invoice_display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_codes: Option<Vec<String>>,
}
impl CreateMinimumCommitmentInput {
pub fn new(amount_cents: i64) -> Self {
Self {
amount_cents,
invoice_display_name: None,
tax_codes: None,
}
}
pub fn with_invoice_display_name(mut self, name: String) -> Self {
self.invoice_display_name = Some(name);
self
}
pub fn with_tax_codes(mut self, tax_codes: Vec<String>) -> Self {
self.tax_codes = Some(tax_codes);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateUsageThresholdInput {
pub amount_cents: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub threshold_display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub recurring: Option<bool>,
}
impl CreateUsageThresholdInput {
pub fn new(amount_cents: i64) -> Self {
Self {
amount_cents,
threshold_display_name: None,
recurring: None,
}
}
pub fn with_threshold_display_name(mut self, name: String) -> Self {
self.threshold_display_name = Some(name);
self
}
pub fn with_recurring(mut self, recurring: bool) -> Self {
self.recurring = Some(recurring);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreatePlanInput {
pub name: String,
pub code: String,
pub interval: PlanInterval,
pub amount_cents: i64,
pub amount_currency: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub invoice_display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub trial_period: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pay_in_advance: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bill_charges_monthly: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_codes: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub charges: Option<Vec<CreatePlanChargeInput>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub minimum_commitment: Option<CreateMinimumCommitmentInput>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage_thresholds: Option<Vec<CreateUsageThresholdInput>>,
}
impl CreatePlanInput {
pub fn new(
name: String,
code: String,
interval: PlanInterval,
amount_cents: i64,
amount_currency: String,
) -> Self {
Self {
name,
code,
interval,
amount_cents,
amount_currency,
invoice_display_name: None,
description: None,
trial_period: None,
pay_in_advance: None,
bill_charges_monthly: None,
tax_codes: None,
charges: None,
minimum_commitment: None,
usage_thresholds: None,
}
}
pub fn with_invoice_display_name(mut self, name: String) -> Self {
self.invoice_display_name = Some(name);
self
}
pub fn with_description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
pub fn with_trial_period(mut self, days: f64) -> Self {
self.trial_period = Some(days);
self
}
pub fn with_pay_in_advance(mut self, pay_in_advance: bool) -> Self {
self.pay_in_advance = Some(pay_in_advance);
self
}
pub fn with_bill_charges_monthly(mut self, bill_charges_monthly: bool) -> Self {
self.bill_charges_monthly = Some(bill_charges_monthly);
self
}
pub fn with_tax_codes(mut self, tax_codes: Vec<String>) -> Self {
self.tax_codes = Some(tax_codes);
self
}
pub fn with_charges(mut self, charges: Vec<CreatePlanChargeInput>) -> Self {
self.charges = Some(charges);
self
}
pub fn with_minimum_commitment(
mut self,
minimum_commitment: CreateMinimumCommitmentInput,
) -> Self {
self.minimum_commitment = Some(minimum_commitment);
self
}
pub fn with_usage_thresholds(
mut self,
usage_thresholds: Vec<CreateUsageThresholdInput>,
) -> Self {
self.usage_thresholds = Some(usage_thresholds);
self
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CreatePlanRequest {
pub plan: CreatePlanInput,
}
impl CreatePlanRequest {
pub fn new(input: CreatePlanInput) -> Self {
Self { plan: input }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdatePlanInput {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub interval: Option<PlanInterval>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount_cents: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount_currency: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub invoice_display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub trial_period: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pay_in_advance: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bill_charges_monthly: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_codes: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub charges: Option<Vec<CreatePlanChargeInput>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub minimum_commitment: Option<CreateMinimumCommitmentInput>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage_thresholds: Option<Vec<CreateUsageThresholdInput>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cascade_updates: Option<bool>,
}
impl UpdatePlanInput {
pub fn new() -> Self {
Self {
name: None,
code: None,
interval: None,
amount_cents: None,
amount_currency: None,
invoice_display_name: None,
description: None,
trial_period: None,
pay_in_advance: None,
bill_charges_monthly: None,
tax_codes: None,
charges: None,
minimum_commitment: None,
usage_thresholds: None,
cascade_updates: None,
}
}
pub fn with_name(mut self, name: String) -> Self {
self.name = Some(name);
self
}
pub fn with_code(mut self, code: String) -> Self {
self.code = Some(code);
self
}
pub fn with_interval(mut self, interval: PlanInterval) -> Self {
self.interval = Some(interval);
self
}
pub fn with_amount_cents(mut self, amount_cents: i64) -> Self {
self.amount_cents = Some(amount_cents);
self
}
pub fn with_amount_currency(mut self, currency: String) -> Self {
self.amount_currency = Some(currency);
self
}
pub fn with_invoice_display_name(mut self, name: String) -> Self {
self.invoice_display_name = Some(name);
self
}
pub fn with_description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
pub fn with_trial_period(mut self, days: f64) -> Self {
self.trial_period = Some(days);
self
}
pub fn with_pay_in_advance(mut self, pay_in_advance: bool) -> Self {
self.pay_in_advance = Some(pay_in_advance);
self
}
pub fn with_bill_charges_monthly(mut self, bill_charges_monthly: bool) -> Self {
self.bill_charges_monthly = Some(bill_charges_monthly);
self
}
pub fn with_tax_codes(mut self, tax_codes: Vec<String>) -> Self {
self.tax_codes = Some(tax_codes);
self
}
pub fn with_charges(mut self, charges: Vec<CreatePlanChargeInput>) -> Self {
self.charges = Some(charges);
self
}
pub fn with_minimum_commitment(
mut self,
minimum_commitment: CreateMinimumCommitmentInput,
) -> Self {
self.minimum_commitment = Some(minimum_commitment);
self
}
pub fn with_usage_thresholds(
mut self,
usage_thresholds: Vec<CreateUsageThresholdInput>,
) -> Self {
self.usage_thresholds = Some(usage_thresholds);
self
}
pub fn with_cascade_updates(mut self, cascade_updates: bool) -> Self {
self.cascade_updates = Some(cascade_updates);
self
}
}
impl Default for UpdatePlanInput {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize)]
pub struct UpdatePlanRequest {
#[serde(skip)]
pub code: String,
pub plan: UpdatePlanInput,
}
impl UpdatePlanRequest {
pub fn new(code: String, input: UpdatePlanInput) -> Self {
Self { code, plan: input }
}
}
#[derive(Debug, Clone)]
pub struct DeletePlanRequest {
pub code: String,
}
impl DeletePlanRequest {
pub fn new(code: String) -> Self {
Self { code }
}
}