allsource_core/application/dto/
event_dto.rs1use crate::domain::entities::Event;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6#[derive(Debug, Deserialize)]
8pub struct IngestEventRequest {
9 pub event_type: String,
10 pub entity_id: String,
11 pub tenant_id: Option<String>, pub payload: serde_json::Value,
13 pub metadata: Option<serde_json::Value>,
14 pub expected_version: Option<u64>,
17 #[serde(default)]
19 pub valid_from: Option<DateTime<Utc>>,
20 #[serde(default)]
22 pub valid_to: Option<DateTime<Utc>>,
23}
24
25#[derive(Debug, Serialize)]
27pub struct IngestEventResponse {
28 pub event_id: Uuid,
29 pub timestamp: DateTime<Utc>,
30 pub valid_from: DateTime<Utc>,
32 pub recorded_at: DateTime<Utc>,
34 #[serde(skip_serializing_if = "Option::is_none")]
36 pub version: Option<u64>,
37}
38
39impl IngestEventResponse {
40 pub fn from_event(event: &Event) -> Self {
41 Self {
42 event_id: event.id(),
43 timestamp: event.timestamp(),
44 valid_from: event.effective_valid_from(),
45 recorded_at: event.timestamp(),
46 version: None,
47 }
48 }
49}
50
51#[derive(Debug, Default, Deserialize)]
53pub struct QueryEventsRequest {
54 pub entity_id: Option<String>,
56
57 pub event_type: Option<String>,
59
60 pub tenant_id: Option<String>,
62
63 pub as_of: Option<DateTime<Utc>>,
65
66 pub since: Option<DateTime<Utc>>,
68
69 pub until: Option<DateTime<Utc>>,
71
72 pub as_of_valid: Option<DateTime<Utc>>,
76
77 pub since_valid: Option<DateTime<Utc>>,
79
80 pub until_valid: Option<DateTime<Utc>>,
82
83 pub limit: Option<usize>,
85
86 pub event_type_prefix: Option<String>,
88
89 pub exclude_event_type_prefix: Option<String>,
94
95 pub payload_filter: Option<String>,
98}
99
100#[derive(Debug, Serialize)]
102pub struct QueryEventsResponse {
103 pub events: Vec<EventDto>,
104 pub count: usize,
105 pub total_count: usize,
106 pub has_more: bool,
107 #[serde(skip_serializing_if = "Option::is_none")]
109 pub entity_version: Option<u64>,
110 #[serde(skip_serializing_if = "Option::is_none")]
112 pub archive_integrity: Option<RetainedEntityIntegrity>,
113}
114
115#[derive(Debug, Serialize)]
116pub struct RetainedEntityIntegrity {
117 pub protocol: &'static str,
118 pub tenant_id: String,
119 pub entity_id: String,
120}
121
122#[derive(Debug, Serialize, Deserialize, Clone)]
124pub struct EventDto {
125 pub id: Uuid,
126 pub event_type: String,
127 pub entity_id: String,
128 pub tenant_id: String,
129 pub payload: serde_json::Value,
130 pub timestamp: DateTime<Utc>,
131 pub metadata: Option<serde_json::Value>,
132 pub version: i64,
133}
134
135impl From<&Event> for EventDto {
136 fn from(event: &Event) -> Self {
137 Self {
138 id: event.id(),
139 event_type: event.event_type().to_string(),
140 entity_id: event.entity_id().to_string(),
141 tenant_id: event.tenant_id().to_string(),
142 payload: event.payload().clone(),
143 timestamp: event.timestamp(),
144 metadata: event.metadata().cloned(),
145 version: event.version(),
146 }
147 }
148}
149
150impl From<Event> for EventDto {
151 fn from(event: Event) -> Self {
152 EventDto::from(&event)
153 }
154}
155
156#[derive(Debug, Default, Deserialize)]
158pub struct ListEntitiesRequest {
159 pub event_type_prefix: Option<String>,
161 pub payload_filter: Option<String>,
163 pub limit: Option<usize>,
165 pub offset: Option<usize>,
167 pub order: Option<String>,
172}
173
174#[derive(Debug, Serialize)]
176pub struct EntitySummary {
177 pub entity_id: String,
178 pub event_count: usize,
179 pub last_event_type: String,
180 pub last_event_at: DateTime<Utc>,
181}
182
183#[derive(Debug, Serialize)]
185pub struct ListEntitiesResponse {
186 pub entities: Vec<EntitySummary>,
187 pub total: usize,
188 pub has_more: bool,
189}
190
191#[derive(Debug, Deserialize)]
193pub struct DetectDuplicatesRequest {
194 pub event_type_prefix: String,
196 pub group_by: String,
198 pub limit: Option<usize>,
200 pub offset: Option<usize>,
202}
203
204#[derive(Debug, Serialize)]
206pub struct DuplicateGroup {
207 pub key: serde_json::Value,
209 pub entity_ids: Vec<String>,
211 pub count: usize,
213}
214
215#[derive(Debug, Serialize)]
217pub struct DetectDuplicatesResponse {
218 pub duplicates: Vec<DuplicateGroup>,
220 pub total: usize,
222 pub has_more: bool,
224}
225
226#[derive(Debug, Deserialize)]
228pub struct IngestEventsBatchRequest {
229 pub events: Vec<IngestEventRequest>,
230}
231
232#[derive(Debug, Serialize)]
234pub struct IngestEventsBatchResponse {
235 pub total: usize,
237 pub ingested: usize,
239 pub events: Vec<IngestEventResponse>,
241}
242
243#[cfg(test)]
244mod tests {
245 use super::*;
246
247 #[test]
248 fn test_ingest_request_without_valid_fields_deserialises() {
249 let req: IngestEventRequest =
250 serde_json::from_str(r#"{"event_type":"user.created","entity_id":"u1","payload":{}}"#)
251 .unwrap();
252 assert_eq!(req.valid_from, None);
253 assert_eq!(req.valid_to, None);
254 }
255
256 #[test]
257 fn test_ingest_request_reads_valid_fields() {
258 let req: IngestEventRequest = serde_json::from_str(
259 r#"{"event_type":"user.created","entity_id":"u1","payload":{},"valid_from":"2026-01-15T00:00:00Z"}"#,
260 )
261 .unwrap();
262 assert_eq!(
263 req.valid_from.unwrap().to_rfc3339(),
264 "2026-01-15T00:00:00+00:00"
265 );
266 assert_eq!(req.valid_to, None);
267 }
268}