use crate::domain::entities::Event;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Deserialize)]
pub struct IngestEventRequest {
pub event_type: String,
pub entity_id: String,
pub tenant_id: Option<String>, pub payload: serde_json::Value,
pub metadata: Option<serde_json::Value>,
pub expected_version: Option<u64>,
}
#[derive(Debug, Serialize)]
pub struct IngestEventResponse {
pub event_id: Uuid,
pub timestamp: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<u64>,
}
impl IngestEventResponse {
pub fn from_event(event: &Event) -> Self {
Self {
event_id: event.id(),
timestamp: event.timestamp(),
version: None,
}
}
}
#[derive(Debug, Default, Deserialize)]
pub struct QueryEventsRequest {
pub entity_id: Option<String>,
pub event_type: Option<String>,
pub tenant_id: Option<String>,
pub as_of: Option<DateTime<Utc>>,
pub since: Option<DateTime<Utc>>,
pub until: Option<DateTime<Utc>>,
pub limit: Option<usize>,
pub event_type_prefix: Option<String>,
pub payload_filter: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct QueryEventsResponse {
pub events: Vec<EventDto>,
pub count: usize,
pub total_count: usize,
pub has_more: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub entity_version: Option<u64>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct EventDto {
pub id: Uuid,
pub event_type: String,
pub entity_id: String,
pub tenant_id: String,
pub payload: serde_json::Value,
pub timestamp: DateTime<Utc>,
pub metadata: Option<serde_json::Value>,
pub version: i64,
}
impl From<&Event> for EventDto {
fn from(event: &Event) -> Self {
Self {
id: event.id(),
event_type: event.event_type().to_string(),
entity_id: event.entity_id().to_string(),
tenant_id: event.tenant_id().to_string(),
payload: event.payload().clone(),
timestamp: event.timestamp(),
metadata: event.metadata().cloned(),
version: event.version(),
}
}
}
impl From<Event> for EventDto {
fn from(event: Event) -> Self {
EventDto::from(&event)
}
}
#[derive(Debug, Deserialize)]
pub struct ListEntitiesRequest {
pub event_type_prefix: Option<String>,
pub payload_filter: Option<String>,
pub limit: Option<usize>,
pub offset: Option<usize>,
}
#[derive(Debug, Serialize)]
pub struct EntitySummary {
pub entity_id: String,
pub event_count: usize,
pub last_event_type: String,
pub last_event_at: DateTime<Utc>,
}
#[derive(Debug, Serialize)]
pub struct ListEntitiesResponse {
pub entities: Vec<EntitySummary>,
pub total: usize,
pub has_more: bool,
}
#[derive(Debug, Deserialize)]
pub struct DetectDuplicatesRequest {
pub event_type_prefix: String,
pub group_by: String,
pub limit: Option<usize>,
pub offset: Option<usize>,
}
#[derive(Debug, Serialize)]
pub struct DuplicateGroup {
pub key: serde_json::Value,
pub entity_ids: Vec<String>,
pub count: usize,
}
#[derive(Debug, Serialize)]
pub struct DetectDuplicatesResponse {
pub duplicates: Vec<DuplicateGroup>,
pub total: usize,
pub has_more: bool,
}
#[derive(Debug, Deserialize)]
pub struct IngestEventsBatchRequest {
pub events: Vec<IngestEventRequest>,
}
#[derive(Debug, Serialize)]
pub struct IngestEventsBatchResponse {
pub total: usize,
pub ingested: usize,
pub events: Vec<IngestEventResponse>,
}