allsource_core/application/dto/
event_dto.rs

1use crate::domain::entities::Event;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6/// DTO for ingesting a new event
7#[derive(Debug, Deserialize)]
8pub struct IngestEventRequest {
9    pub event_type: String,
10    pub entity_id: String,
11    pub tenant_id: Option<String>, // Optional, defaults to "default"
12    pub payload: serde_json::Value,
13    pub metadata: Option<serde_json::Value>,
14}
15
16/// DTO for event ingestion response
17#[derive(Debug, Serialize)]
18pub struct IngestEventResponse {
19    pub event_id: Uuid,
20    pub timestamp: DateTime<Utc>,
21}
22
23impl IngestEventResponse {
24    pub fn from_event(event: &Event) -> Self {
25        Self {
26            event_id: event.id(),
27            timestamp: event.timestamp(),
28        }
29    }
30}
31
32/// DTO for querying events
33#[derive(Debug, Deserialize)]
34pub struct QueryEventsRequest {
35    /// Filter by entity ID
36    pub entity_id: Option<String>,
37
38    /// Filter by event type
39    pub event_type: Option<String>,
40
41    /// Tenant ID (required for multi-tenancy)
42    pub tenant_id: Option<String>,
43
44    /// Time-travel: get events as of this timestamp
45    pub as_of: Option<DateTime<Utc>>,
46
47    /// Get events since this timestamp
48    pub since: Option<DateTime<Utc>>,
49
50    /// Get events until this timestamp
51    pub until: Option<DateTime<Utc>>,
52
53    /// Limit number of results
54    pub limit: Option<usize>,
55}
56
57/// DTO for query response
58#[derive(Debug, Serialize)]
59pub struct QueryEventsResponse {
60    pub events: Vec<EventDto>,
61    pub count: usize,
62}
63
64/// DTO for a single event in responses
65#[derive(Debug, Serialize, Clone)]
66pub struct EventDto {
67    pub id: Uuid,
68    pub event_type: String,
69    pub entity_id: String,
70    pub tenant_id: String,
71    pub payload: serde_json::Value,
72    pub timestamp: DateTime<Utc>,
73    pub metadata: Option<serde_json::Value>,
74    pub version: i64,
75}
76
77impl From<&Event> for EventDto {
78    fn from(event: &Event) -> Self {
79        Self {
80            id: event.id(),
81            event_type: event.event_type().to_string(),
82            entity_id: event.entity_id().to_string(),
83            tenant_id: event.tenant_id().to_string(),
84            payload: event.payload().clone(),
85            timestamp: event.timestamp(),
86            metadata: event.metadata().cloned(),
87            version: event.version(),
88        }
89    }
90}
91
92impl From<Event> for EventDto {
93    fn from(event: Event) -> Self {
94        EventDto::from(&event)
95    }
96}