use reqwest::Method;
use serde::Serialize;
use serde_with::skip_serializing_none;
use crate::entities::{Adjustment, AdjustmentItemInput};
use crate::enums::{AdjustmentAction, AdjustmentStatus, AdjustmentType, TaxMode};
use crate::ids::{AdjustmentID, CustomerID, SubscriptionID, TransactionID};
use crate::paginated::Paginated;
use crate::{Paddle, Result};
#[skip_serializing_none]
#[derive(Serialize)]
pub struct AdjustmentsList<'a> {
#[serde(skip)]
client: &'a Paddle,
action: Option<AdjustmentAction>,
after: Option<AdjustmentID>,
#[serde(serialize_with = "crate::comma_separated")]
customer_id: Option<Vec<CustomerID>>,
order_by: Option<String>,
per_page: Option<usize>,
#[serde(serialize_with = "crate::comma_separated_enum")]
status: Option<Vec<AdjustmentStatus>>,
#[serde(serialize_with = "crate::comma_separated")]
subscription_id: Option<Vec<SubscriptionID>>,
#[serde(serialize_with = "crate::comma_separated")]
transaction_id: Option<Vec<TransactionID>>,
#[serde(serialize_with = "crate::comma_separated")]
id: Option<Vec<AdjustmentID>>,
}
impl<'a> AdjustmentsList<'a> {
pub fn new(client: &'a Paddle) -> Self {
Self {
client,
action: None,
after: None,
customer_id: None,
order_by: None,
per_page: None,
status: None,
subscription_id: None,
transaction_id: None,
id: None,
}
}
pub fn action(&mut self, adjustment_action: AdjustmentAction) -> &mut Self {
self.action = Some(adjustment_action);
self
}
pub fn after(&mut self, id: impl Into<AdjustmentID>) -> &mut Self {
self.after = Some(id.into());
self
}
pub fn customer_id(
&mut self,
customer_ids: impl IntoIterator<Item = impl Into<CustomerID>>,
) -> &mut Self {
self.customer_id = Some(customer_ids.into_iter().map(Into::into).collect());
self
}
pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
self.order_by = Some(format!("{}[ASC]", field));
self
}
pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
self.order_by = Some(format!("{}[DESC]", field));
self
}
pub fn per_page(&mut self, entities_per_page: usize) -> &mut Self {
self.per_page = Some(entities_per_page);
self
}
pub fn status(&mut self, statuses: impl IntoIterator<Item = AdjustmentStatus>) -> &mut Self {
self.status = Some(statuses.into_iter().collect());
self
}
pub fn subscription_ids(
&mut self,
subscription_ids: impl IntoIterator<Item = impl Into<SubscriptionID>>,
) -> &mut Self {
self.subscription_id = Some(subscription_ids.into_iter().map(Into::into).collect());
self
}
pub fn transaction_ids(
&mut self,
transaction_ids: impl IntoIterator<Item = impl Into<TransactionID>>,
) -> &mut Self {
self.transaction_id = Some(transaction_ids.into_iter().map(Into::into).collect());
self
}
pub fn id(&mut self, ids: impl IntoIterator<Item = impl Into<AdjustmentID>>) -> &mut Self {
self.id = Some(ids.into_iter().map(Into::into).collect());
self
}
pub fn send(&self) -> Paginated<'_, Vec<Adjustment>> {
Paginated::new(self.client, "/adjustments", self)
}
}
#[skip_serializing_none]
#[derive(Serialize)]
pub struct AdjustmentCreate<'a> {
#[serde(skip)]
client: &'a Paddle,
transaction_id: TransactionID,
action: AdjustmentAction,
reason: String,
r#type: Option<AdjustmentType>,
tax_mode: Option<TaxMode>,
items: Option<Vec<AdjustmentItemInput>>,
}
impl<'a> AdjustmentCreate<'a> {
pub fn new(
client: &'a Paddle,
transaction_id: impl Into<TransactionID>,
action: AdjustmentAction,
reason: impl Into<String>,
) -> Self {
Self {
client,
transaction_id: transaction_id.into(),
action,
reason: reason.into(),
r#type: None,
tax_mode: None,
items: None,
}
}
pub fn r#type(&mut self, adjustment_type: AdjustmentType) -> &mut Self {
self.r#type = Some(adjustment_type);
self
}
pub fn items(&mut self, items: impl IntoIterator<Item = AdjustmentItemInput>) -> &mut Self {
self.items = Some(items.into_iter().collect());
self
}
pub fn tax_mode(&mut self, mode: TaxMode) -> &mut Self {
self.tax_mode = Some(mode);
self
}
pub async fn send(&self) -> Result<Adjustment> {
self.client.send(self, Method::POST, "/adjustments").await
}
}