Skip to main content

lago_types/requests/
event.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::models::PaginationParams;
5
6/// Request parameters for listing events.
7///
8/// This struct combines pagination parameters and optional filters
9/// to build a comprehensive request for retrieving event lists.
10#[derive(Debug, Clone, Default)]
11pub struct ListEventsRequest {
12    /// Pagination parameters.
13    pub pagination: PaginationParams,
14    /// Filter by external subscription ID.
15    pub external_subscription_id: Option<String>,
16    /// Filter by billable metric code.
17    pub code: Option<String>,
18    /// Requires `external_subscription_id` to be set.
19    /// Filter events by timestamp after the subscription started at datetime.
20    pub timestamp_from_started_at: Option<bool>,
21    /// Filter events by timestamp starting from a specific date (ISO 8601 format).
22    pub timestamp_from: Option<String>,
23    /// Filter events by timestamp up to a specific date (ISO 8601 format).
24    pub timestamp_to: Option<String>,
25}
26
27impl ListEventsRequest {
28    /// Creates a new empty list events request.
29    ///
30    /// # Returns
31    /// A new `ListEventsRequest` instance with default pagination and no filters.
32    pub fn new() -> Self {
33        Self::default()
34    }
35
36    /// Sets the pagination parameters for the request.
37    ///
38    /// # Arguments
39    /// * `pagination` - The pagination parameters to use
40    ///
41    /// # Returns
42    /// The modified request instance for method chaining.
43    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
44        self.pagination = pagination;
45        self
46    }
47
48    /// Sets the external subscription ID filter.
49    ///
50    /// # Arguments
51    /// * `external_subscription_id` - The external subscription ID to filter by
52    ///
53    /// # Returns
54    /// The modified request instance for method chaining.
55    pub fn with_external_subscription_id(mut self, external_subscription_id: String) -> Self {
56        self.external_subscription_id = Some(external_subscription_id);
57        self
58    }
59
60    /// Sets the billable metric code filter.
61    ///
62    /// # Arguments
63    /// * `code` - The billable metric code to filter by
64    ///
65    /// # Returns
66    /// The modified request instance for method chaining.
67    pub fn with_code(mut self, code: String) -> Self {
68        self.code = Some(code);
69        self
70    }
71
72    /// Sets whether to filter events by timestamp after the subscription started at datetime.
73    /// Requires `external_subscription_id` to be set.
74    ///
75    /// # Arguments
76    /// * `timestamp_from_started_at` - Whether to filter from subscription start
77    ///
78    /// # Returns
79    /// The modified request instance for method chaining.
80    pub fn with_timestamp_from_started_at(mut self, timestamp_from_started_at: bool) -> Self {
81        self.timestamp_from_started_at = Some(timestamp_from_started_at);
82        self
83    }
84
85    /// Sets the timestamp from filter (events with timestamp >= this date).
86    ///
87    /// # Arguments
88    /// * `timestamp_from` - The start date in ISO 8601 format
89    ///
90    /// # Returns
91    /// The modified request instance for method chaining.
92    pub fn with_timestamp_from(mut self, timestamp_from: String) -> Self {
93        self.timestamp_from = Some(timestamp_from);
94        self
95    }
96
97    /// Sets the timestamp to filter (events with timestamp <= this date).
98    ///
99    /// # Arguments
100    /// * `timestamp_to` - The end date in ISO 8601 format
101    ///
102    /// # Returns
103    /// The modified request instance for method chaining.
104    pub fn with_timestamp_to(mut self, timestamp_to: String) -> Self {
105        self.timestamp_to = Some(timestamp_to);
106        self
107    }
108
109    /// Sets the timestamp range filter.
110    ///
111    /// # Arguments
112    /// * `from` - The start date in ISO 8601 format
113    /// * `to` - The end date in ISO 8601 format
114    ///
115    /// # Returns
116    /// The modified request instance for method chaining.
117    pub fn with_timestamp_range(mut self, from: String, to: String) -> Self {
118        self.timestamp_from = Some(from);
119        self.timestamp_to = Some(to);
120        self
121    }
122
123    /// Converts the request parameters into HTTP query parameters.
124    ///
125    /// # Returns
126    /// A vector of query parameter tuples containing both pagination and filter criteria.
127    pub fn to_query_params(&self) -> Vec<(&str, String)> {
128        let mut params = self.pagination.to_query_params();
129
130        if let Some(external_subscription_id) = &self.external_subscription_id {
131            params.push(("external_subscription_id", external_subscription_id.clone()));
132        }
133
134        if let Some(code) = &self.code {
135            params.push(("code", code.clone()));
136        }
137
138        if let Some(timestamp_from_started_at) = &self.timestamp_from_started_at {
139            params.push((
140                "timestamp_from_started_at",
141                timestamp_from_started_at.to_string(),
142            ));
143        }
144
145        if let Some(timestamp_from) = &self.timestamp_from {
146            params.push(("timestamp_from", timestamp_from.clone()));
147        }
148
149        if let Some(timestamp_to) = &self.timestamp_to {
150            params.push(("timestamp_to", timestamp_to.clone()));
151        }
152
153        params
154    }
155}
156
157/// Request to retrieve a specific event by transaction ID.
158#[derive(Debug, Clone)]
159pub struct GetEventRequest {
160    /// The transaction ID of the event to retrieve (must be URL encoded)
161    pub transaction_id: String,
162}
163
164impl GetEventRequest {
165    /// Creates a new get event request.
166    ///
167    /// # Arguments
168    /// * `transaction_id` - The transaction ID of the event to retrieve
169    ///
170    /// # Returns
171    /// A new `GetEventRequest` instance
172    pub fn new(transaction_id: String) -> Self {
173        Self { transaction_id }
174    }
175}
176
177/// Input data for creating a usage event.
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct CreateEventInput {
180    /// Unique identifier for this event (used for idempotency and retrieval)
181    pub transaction_id: String,
182    /// External customer ID - required if external_subscription_id is not provided
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub external_customer_id: Option<String>,
185    /// External subscription ID - required if external_customer_id is not provided
186    #[serde(skip_serializing_if = "Option::is_none")]
187    pub external_subscription_id: Option<String>,
188    /// Billable metric code
189    pub code: String,
190    /// Event timestamp (Unix timestamp in seconds)
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub timestamp: Option<i64>,
193    /// Custom properties/metadata for the event
194    #[serde(skip_serializing_if = "Option::is_none")]
195    pub properties: Option<Value>,
196    /// Precise total amount in cents
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub precise_total_amount_cents: Option<i64>,
199}
200
201impl CreateEventInput {
202    /// Creates a new event input for a customer.
203    ///
204    /// # Arguments
205    /// * `transaction_id` - Unique identifier for the event
206    /// * `external_customer_id` - The external ID of the customer
207    /// * `code` - The billable metric code
208    ///
209    /// # Returns
210    /// A new `CreateEventInput` instance
211    pub fn for_customer(
212        transaction_id: String,
213        external_customer_id: String,
214        code: String,
215    ) -> Self {
216        Self {
217            transaction_id,
218            external_customer_id: Some(external_customer_id),
219            external_subscription_id: None,
220            code,
221            timestamp: None,
222            properties: None,
223            precise_total_amount_cents: None,
224        }
225    }
226
227    /// Creates a new event input for a subscription.
228    ///
229    /// # Arguments
230    /// * `transaction_id` - Unique identifier for the event
231    /// * `external_subscription_id` - The external ID of the subscription
232    /// * `code` - The billable metric code
233    ///
234    /// # Returns
235    /// A new `CreateEventInput` instance
236    pub fn for_subscription(
237        transaction_id: String,
238        external_subscription_id: String,
239        code: String,
240    ) -> Self {
241        Self {
242            transaction_id,
243            external_customer_id: None,
244            external_subscription_id: Some(external_subscription_id),
245            code,
246            timestamp: None,
247            properties: None,
248            precise_total_amount_cents: None,
249        }
250    }
251
252    /// Sets the timestamp for the event.
253    ///
254    /// # Arguments
255    /// * `timestamp` - Unix timestamp in seconds
256    ///
257    /// # Returns
258    /// The modified input instance for method chaining
259    pub fn with_timestamp(mut self, timestamp: i64) -> Self {
260        self.timestamp = Some(timestamp);
261        self
262    }
263
264    /// Sets custom properties for the event.
265    ///
266    /// # Arguments
267    /// * `properties` - JSON object containing event properties
268    ///
269    /// # Returns
270    /// The modified input instance for method chaining
271    pub fn with_properties(mut self, properties: Value) -> Self {
272        self.properties = Some(properties);
273        self
274    }
275
276    /// Sets the precise total amount in cents.
277    ///
278    /// # Arguments
279    /// * `amount` - The precise amount in cents
280    ///
281    /// # Returns
282    /// The modified input instance for method chaining
283    pub fn with_precise_total_amount_cents(mut self, amount: i64) -> Self {
284        self.precise_total_amount_cents = Some(amount);
285        self
286    }
287}
288
289/// Request wrapper for creating a usage event.
290#[derive(Debug, Clone, Serialize, Deserialize)]
291pub struct CreateEventRequest {
292    /// The event input data
293    pub event: CreateEventInput,
294}
295
296impl CreateEventRequest {
297    /// Creates a new create event request.
298    ///
299    /// # Arguments
300    /// * `event` - The event input data
301    ///
302    /// # Returns
303    /// A new `CreateEventRequest` instance
304    pub fn new(event: CreateEventInput) -> Self {
305        Self { event }
306    }
307}