Skip to main content

helios_subscriptions/
event.rs

1//! Resource event types emitted after write operations.
2
3use chrono::{DateTime, Utc};
4use helios_fhir::FhirVersion;
5use helios_persistence::tenant::TenantId;
6
7/// An event emitted after a resource write operation (create, update, or delete).
8///
9/// This is the input to the subscription engine's evaluation pipeline.
10#[derive(Debug, Clone)]
11pub struct ResourceEvent {
12    /// The tenant that owns the resource.
13    pub tenant_id: TenantId,
14
15    /// The FHIR version of the resource.
16    pub fhir_version: FhirVersion,
17
18    /// The resource type (e.g., "Patient", "Observation").
19    pub resource_type: String,
20
21    /// The resource ID.
22    pub resource_id: String,
23
24    /// The version ID after the write operation.
25    pub version_id: String,
26
27    /// The type of interaction that produced this event.
28    pub event_type: ResourceEventType,
29
30    /// The resource content after the interaction (`None` for delete).
31    pub resource: Option<serde_json::Value>,
32
33    /// The resource content before the interaction (`None` for create).
34    pub previous_resource: Option<serde_json::Value>,
35
36    /// When the event occurred.
37    pub timestamp: DateTime<Utc>,
38}
39
40/// The type of interaction that produced a resource event.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum ResourceEventType {
43    /// A new resource was created.
44    Create,
45    /// An existing resource was updated (including patch).
46    Update,
47    /// A resource was deleted.
48    Delete,
49}
50
51impl std::fmt::Display for ResourceEventType {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        match self {
54            Self::Create => write!(f, "create"),
55            Self::Update => write!(f, "update"),
56            Self::Delete => write!(f, "delete"),
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_event_type_display() {
67        assert_eq!(ResourceEventType::Create.to_string(), "create");
68        assert_eq!(ResourceEventType::Update.to_string(), "update");
69        assert_eq!(ResourceEventType::Delete.to_string(), "delete");
70    }
71}