paddle_rust_sdk/
reports.rs1use paddle_rust_sdk_types::reports::ReportType;
6use reqwest::Method;
7use serde::de::DeserializeOwned;
8use serde::Serialize;
9use serde_with::skip_serializing_none;
10
11use crate::entities::{ReportBase, ReportFilter, ReportFilterValue};
12use crate::enums::{FilterOperator, ReportStatus};
13use crate::ids::PaddleID;
14use crate::paginated::Paginated;
15use crate::{Paddle, Result};
16
17#[skip_serializing_none]
19#[derive(Serialize)]
20pub struct ReportsList<'a> {
21 #[serde(skip)]
22 client: &'a Paddle,
23 after: Option<PaddleID>,
24 order_by: Option<String>,
25 per_page: Option<usize>,
26 #[serde(serialize_with = "crate::comma_separated_enum")]
27 status: Option<Vec<ReportStatus>>,
28}
29
30impl<'a> ReportsList<'a> {
31 pub fn new(client: &'a Paddle) -> Self {
32 Self {
33 client,
34 after: None,
35 order_by: None,
36 per_page: None,
37 status: None,
38 }
39 }
40
41 pub fn after(&mut self, report_id: impl Into<PaddleID>) -> &mut Self {
43 self.after = Some(report_id.into());
44 self
45 }
46
47 pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
49 self.order_by = Some(format!("{}[ASC]", field));
50 self
51 }
52
53 pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
55 self.order_by = Some(format!("{}[DESC]", field));
56 self
57 }
58
59 pub fn per_page(&mut self, entities_per_page: usize) -> &mut Self {
64 self.per_page = Some(entities_per_page);
65 self
66 }
67
68 pub fn status(&mut self, statuses: impl IntoIterator<Item = ReportStatus>) -> &mut Self {
70 self.status = Some(statuses.into_iter().collect());
71 self
72 }
73
74 pub fn send(&self) -> Paginated<'_, Vec<ReportBase>> {
76 Paginated::new(self.client, "/reports", self)
77 }
78}
79
80#[skip_serializing_none]
82#[derive(Serialize)]
83pub struct ReportCreate<'a, T: ReportType> {
84 #[serde(skip)]
85 client: &'a Paddle,
86 r#type: T,
87 filters: Vec<ReportFilter<T::FilterName>>,
88}
89
90impl<'a, T: ReportType + DeserializeOwned> ReportCreate<'a, T> {
91 pub fn new(client: &'a Paddle, r#type: T) -> Self {
92 Self {
93 client,
94 r#type,
95 filters: Vec::new(),
96 }
97 }
98
99 pub fn append_filter(
101 &mut self,
102 name: T::FilterName,
103 operator: Option<FilterOperator>,
104 value: impl Into<ReportFilterValue>,
105 ) -> &mut Self {
106 self.filters.push(ReportFilter {
107 name,
108 operator,
109 value: value.into(),
110 });
111
112 self
113 }
114
115 pub fn clear_filters(&mut self) {
117 self.filters.clear();
118 }
119
120 pub fn set_filters(
122 &mut self,
123 filters: impl IntoIterator<Item = (T::FilterName, Option<FilterOperator>, ReportFilterValue)>,
124 ) -> &mut Self {
125 self.filters = filters
126 .into_iter()
127 .map(|(name, operator, value)| ReportFilter {
128 name,
129 operator,
130 value,
131 })
132 .collect();
133 self
134 }
135
136 pub async fn send(&self) -> Result<ReportBase> {
138 self.client.send(self, Method::POST, "/reports").await
139 }
140}