use std::borrow::Cow;
use derive_builder::Builder;
use serde::Serialize;
use crate::{
data::{
invoice::{CancelReason, Invoice, InvoiceList, InvoicePayload, SendInvoicePayload},
orders::InvoiceNumber,
},
endpoint::Endpoint,
Query,
};
#[derive(Debug, Default, Clone)]
pub struct GenerateInvoiceNumber {
pub invoice_number: Option<InvoiceNumber>,
}
impl GenerateInvoiceNumber {
pub fn new(invoice_number: Option<InvoiceNumber>) -> Self {
Self { invoice_number }
}
}
impl Endpoint for GenerateInvoiceNumber {
type Query = ();
type Body = Option<InvoiceNumber>;
type Response = InvoiceNumber;
fn relative_path(&self) -> Cow<str> {
Cow::Borrowed("/v2/invoicing/generate-next-invoice-number")
}
fn method(&self) -> reqwest::Method {
reqwest::Method::POST
}
fn body(&self) -> Option<Self::Body> {
Some(self.invoice_number.clone())
}
}
#[derive(Debug, Clone)]
pub struct CreateDraftInvoice {
pub invoice: InvoicePayload,
}
impl CreateDraftInvoice {
pub fn new(invoice: InvoicePayload) -> Self {
Self { invoice }
}
}
impl Endpoint for CreateDraftInvoice {
type Query = ();
type Body = InvoicePayload;
type Response = Invoice;
fn relative_path(&self) -> Cow<str> {
Cow::Borrowed("/v2/invoicing/invoices")
}
fn method(&self) -> reqwest::Method {
reqwest::Method::POST
}
fn body(&self) -> Option<Self::Body> {
Some(self.invoice.clone())
}
}
#[derive(Debug, Clone)]
pub struct GetInvoice {
pub invoice_id: String,
}
impl GetInvoice {
pub fn new(invoice_id: impl ToString) -> Self {
Self {
invoice_id: invoice_id.to_string(),
}
}
}
impl Endpoint for GetInvoice {
type Query = ();
type Body = ();
type Response = Invoice;
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/invoicing/invoices/{}", self.invoice_id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::GET
}
}
#[derive(Debug, Clone)]
pub struct ListInvoices {
pub query: Query,
}
impl ListInvoices {
pub fn new(query: Query) -> Self {
Self { query }
}
}
impl Endpoint for ListInvoices {
type Query = Query;
type Body = ();
type Response = InvoiceList;
fn relative_path(&self) -> Cow<str> {
Cow::Borrowed("/v2/invoicing/invoices")
}
fn method(&self) -> reqwest::Method {
reqwest::Method::GET
}
fn query(&self) -> Option<Self::Query> {
Some(self.query.clone())
}
}
#[derive(Debug, Clone)]
pub struct DeleteInvoice {
pub invoice_id: String,
}
impl DeleteInvoice {
pub fn new(invoice_id: impl ToString) -> Self {
Self {
invoice_id: invoice_id.to_string(),
}
}
}
impl Endpoint for DeleteInvoice {
type Query = Query;
type Body = ();
type Response = ();
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/invoicing/invoices/{}", self.invoice_id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::DELETE
}
}
#[derive(Debug, Clone, Serialize, Builder)]
pub struct UpdateInvoiceQuery {
pub send_to_recipient: bool,
pub send_to_invoicer: bool,
}
#[derive(Debug, Clone)]
pub struct UpdateInvoice {
pub invoice: Invoice,
pub query: UpdateInvoiceQuery,
}
impl UpdateInvoice {
pub fn new(invoice: Invoice, query: UpdateInvoiceQuery) -> Self {
Self { invoice, query }
}
}
impl Endpoint for UpdateInvoice {
type Query = UpdateInvoiceQuery;
type Body = Invoice;
type Response = Invoice;
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/invoicing/invoices/{}", self.invoice.id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::PUT
}
fn body(&self) -> Option<Self::Body> {
Some(self.invoice.clone())
}
fn query(&self) -> Option<Self::Query> {
Some(self.query.clone())
}
}
#[derive(Debug, Clone)]
pub struct CancelInvoice {
pub invoice_id: String,
pub reason: CancelReason,
}
impl CancelInvoice {
pub fn new(invoice_id: impl ToString, reason: CancelReason) -> Self {
Self {
invoice_id: invoice_id.to_string(),
reason,
}
}
}
impl Endpoint for CancelInvoice {
type Query = ();
type Body = CancelReason;
type Response = ();
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/invoicing/invoices/{}/cancel", self.invoice_id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::POST
}
fn body(&self) -> Option<Self::Body> {
Some(self.reason.clone())
}
}
#[derive(Debug, Clone)]
pub struct SendInvoice {
pub invoice_id: String,
pub payload: SendInvoicePayload,
}
impl SendInvoice {
pub fn new(invoice_id: impl ToString, payload: SendInvoicePayload) -> Self {
Self {
invoice_id: invoice_id.to_string(),
payload,
}
}
}
impl Endpoint for SendInvoice {
type Query = ();
type Body = SendInvoicePayload;
type Response = ();
fn relative_path(&self) -> Cow<str> {
Cow::Owned(format!("/v2/invoicing/invoices/{}/send", self.invoice_id))
}
fn method(&self) -> reqwest::Method {
reqwest::Method::POST
}
fn body(&self) -> Option<Self::Body> {
Some(self.payload.clone())
}
}