use std::collections::HashMap;
use reqwest::Method;
use serde::Serialize;
use serde_with::skip_serializing_none;
use crate::entities::{Business, Contact};
use crate::enums::Status;
use crate::ids::{BusinessID, CustomerID};
use crate::paginated::Paginated;
use crate::{Paddle, Result};
#[skip_serializing_none]
#[derive(Serialize)]
pub struct BusinessesList<'a> {
#[serde(skip)]
client: &'a Paddle,
#[serde(skip)]
customer_id: CustomerID,
after: Option<BusinessID>,
#[serde(serialize_with = "crate::comma_separated")]
id: Option<Vec<BusinessID>>,
order_by: Option<String>,
per_page: Option<usize>,
search: Option<String>,
status: Option<Status>,
}
impl<'a> BusinessesList<'a> {
pub fn new(client: &'a Paddle, customer_id: impl Into<CustomerID>) -> Self {
Self {
client,
customer_id: customer_id.into(),
after: None,
id: None,
order_by: None,
per_page: None,
search: None,
status: None,
}
}
pub fn after(&mut self, business_id: impl Into<BusinessID>) -> &mut Self {
self.after = Some(business_id.into());
self
}
pub fn ids(
&mut self,
business_ids: impl IntoIterator<Item = impl Into<BusinessID>>,
) -> &mut Self {
self.id = Some(business_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 search(&mut self, term: impl Into<String>) -> &mut Self {
self.search = Some(term.into());
self
}
pub fn status(&mut self, status: Status) -> &mut Self {
self.status = Some(status);
self
}
pub fn send(&self) -> Paginated<'_, Vec<Business>> {
let url = format!("/customers/{}/businesses", self.customer_id.as_ref());
Paginated::new(self.client, &url, self)
}
}
#[skip_serializing_none]
#[derive(Serialize)]
pub struct BusinessCreate<'a> {
#[serde(skip)]
client: &'a Paddle,
#[serde(skip)]
customer_id: CustomerID,
name: String,
company_number: Option<String>,
tax_identifier: Option<String>,
contacts: Option<Vec<Contact>>,
custom_data: Option<HashMap<String, String>>,
}
impl<'a> BusinessCreate<'a> {
pub fn new(
client: &'a Paddle,
customer_id: impl Into<CustomerID>,
name: impl Into<String>,
) -> Self {
Self {
client,
customer_id: customer_id.into(),
name: name.into(),
company_number: None,
tax_identifier: None,
contacts: None,
custom_data: None,
}
}
pub fn company_number(&mut self, company_number: impl Into<String>) -> &mut Self {
self.company_number = Some(company_number.into());
self
}
pub fn tax_identifier(&mut self, tax_identifier: impl Into<String>) -> &mut Self {
self.tax_identifier = Some(tax_identifier.into());
self
}
pub fn contacts(&mut self, contacts: impl IntoIterator<Item = Contact>) -> &mut Self {
self.contacts = Some(contacts.into_iter().collect());
self
}
pub fn custom_data(&mut self, custom_data: HashMap<String, String>) -> &mut Self {
self.custom_data = Some(custom_data);
self
}
pub async fn send(&self) -> Result<Business> {
self.client
.send(
self,
Method::POST,
&format!("/customers/{}/businesses", self.customer_id.as_ref()),
)
.await
}
}
#[skip_serializing_none]
#[derive(Serialize)]
pub struct BusinessGet<'a> {
#[serde(skip)]
client: &'a Paddle,
#[serde(skip)]
customer_id: CustomerID,
#[serde(skip)]
business_id: BusinessID,
}
impl<'a> BusinessGet<'a> {
pub fn new(
client: &'a Paddle,
customer_id: impl Into<CustomerID>,
business_id: impl Into<BusinessID>,
) -> Self {
Self {
client,
customer_id: customer_id.into(),
business_id: business_id.into(),
}
}
pub async fn send(&self) -> Result<Business> {
self.client
.send(
self,
Method::GET,
&format!(
"/customers/{}/businesses/{}",
self.customer_id.as_ref(),
self.business_id.as_ref()
),
)
.await
}
}
#[skip_serializing_none]
#[derive(Serialize)]
pub struct BusinessUpdate<'a> {
#[serde(skip)]
client: &'a Paddle,
#[serde(skip)]
customer_id: CustomerID,
#[serde(skip)]
business_id: BusinessID,
name: Option<String>,
company_number: Option<String>,
tax_identifier: Option<String>,
contacts: Option<Vec<Contact>>,
custom_data: Option<HashMap<String, String>>,
}
impl<'a> BusinessUpdate<'a> {
pub fn new(
client: &'a Paddle,
customer_id: impl Into<CustomerID>,
business_id: impl Into<BusinessID>,
) -> Self {
Self {
client,
customer_id: customer_id.into(),
business_id: business_id.into(),
name: None,
company_number: None,
tax_identifier: None,
contacts: None,
custom_data: None,
}
}
pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
self.name = Some(name.into());
self
}
pub fn company_number(&mut self, company_number: impl Into<String>) -> &mut Self {
self.company_number = Some(company_number.into());
self
}
pub fn tax_identifier(&mut self, tax_identifier: impl Into<String>) -> &mut Self {
self.tax_identifier = Some(tax_identifier.into());
self
}
pub fn contacts(&mut self, contacts: impl IntoIterator<Item = Contact>) -> &mut Self {
self.contacts = Some(contacts.into_iter().collect());
self
}
pub fn custom_data(&mut self, custom_data: HashMap<String, String>) -> &mut Self {
self.custom_data = Some(custom_data);
self
}
pub async fn send(&self) -> Result<Business> {
self.client
.send(
self,
Method::PATCH,
&format!(
"/customers/{}/businesses/{}",
self.customer_id.as_ref(),
self.business_id.as_ref()
),
)
.await
}
}