lago_types/requests/
event.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4/// Request to retrieve a specific event by transaction ID.
5#[derive(Debug, Clone)]
6pub struct GetEventRequest {
7    /// The transaction ID of the event to retrieve (must be URL encoded)
8    pub transaction_id: String,
9}
10
11impl GetEventRequest {
12    /// Creates a new get event request.
13    ///
14    /// # Arguments
15    /// * `transaction_id` - The transaction ID of the event to retrieve
16    ///
17    /// # Returns
18    /// A new `GetEventRequest` instance
19    pub fn new(transaction_id: String) -> Self {
20        Self { transaction_id }
21    }
22}
23
24/// Input data for creating a usage event.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct CreateEventInput {
27    /// Unique identifier for this event (used for idempotency and retrieval)
28    pub transaction_id: String,
29    /// External customer ID - required if external_subscription_id is not provided
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub external_customer_id: Option<String>,
32    /// External subscription ID - required if external_customer_id is not provided
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub external_subscription_id: Option<String>,
35    /// Billable metric code
36    pub code: String,
37    /// Event timestamp (Unix timestamp in seconds)
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub timestamp: Option<i64>,
40    /// Custom properties/metadata for the event
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub properties: Option<Value>,
43    /// Precise total amount in cents
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub precise_total_amount_cents: Option<i64>,
46}
47
48impl CreateEventInput {
49    /// Creates a new event input for a customer.
50    ///
51    /// # Arguments
52    /// * `transaction_id` - Unique identifier for the event
53    /// * `external_customer_id` - The external ID of the customer
54    /// * `code` - The billable metric code
55    ///
56    /// # Returns
57    /// A new `CreateEventInput` instance
58    pub fn for_customer(
59        transaction_id: String,
60        external_customer_id: String,
61        code: String,
62    ) -> Self {
63        Self {
64            transaction_id,
65            external_customer_id: Some(external_customer_id),
66            external_subscription_id: None,
67            code,
68            timestamp: None,
69            properties: None,
70            precise_total_amount_cents: None,
71        }
72    }
73
74    /// Creates a new event input for a subscription.
75    ///
76    /// # Arguments
77    /// * `transaction_id` - Unique identifier for the event
78    /// * `external_subscription_id` - The external ID of the subscription
79    /// * `code` - The billable metric code
80    ///
81    /// # Returns
82    /// A new `CreateEventInput` instance
83    pub fn for_subscription(
84        transaction_id: String,
85        external_subscription_id: String,
86        code: String,
87    ) -> Self {
88        Self {
89            transaction_id,
90            external_customer_id: None,
91            external_subscription_id: Some(external_subscription_id),
92            code,
93            timestamp: None,
94            properties: None,
95            precise_total_amount_cents: None,
96        }
97    }
98
99    /// Sets the timestamp for the event.
100    ///
101    /// # Arguments
102    /// * `timestamp` - Unix timestamp in seconds
103    ///
104    /// # Returns
105    /// The modified input instance for method chaining
106    pub fn with_timestamp(mut self, timestamp: i64) -> Self {
107        self.timestamp = Some(timestamp);
108        self
109    }
110
111    /// Sets custom properties for the event.
112    ///
113    /// # Arguments
114    /// * `properties` - JSON object containing event properties
115    ///
116    /// # Returns
117    /// The modified input instance for method chaining
118    pub fn with_properties(mut self, properties: Value) -> Self {
119        self.properties = Some(properties);
120        self
121    }
122
123    /// Sets the precise total amount in cents.
124    ///
125    /// # Arguments
126    /// * `amount` - The precise amount in cents
127    ///
128    /// # Returns
129    /// The modified input instance for method chaining
130    pub fn with_precise_total_amount_cents(mut self, amount: i64) -> Self {
131        self.precise_total_amount_cents = Some(amount);
132        self
133    }
134}
135
136/// Request wrapper for creating a usage event.
137#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct CreateEventRequest {
139    /// The event input data
140    pub event: CreateEventInput,
141}
142
143impl CreateEventRequest {
144    /// Creates a new create event request.
145    ///
146    /// # Arguments
147    /// * `event` - The event input data
148    ///
149    /// # Returns
150    /// A new `CreateEventRequest` instance
151    pub fn new(event: CreateEventInput) -> Self {
152        Self { event }
153    }
154}