use crate::automation::callable::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EventHookEventType {
BeforeInsert,
AfterInsert,
BeforeUpdate,
AfterUpdate,
BeforeDelete,
AfterDelete,
Scheduled,
}
#[derive(Debug, Clone)]
pub struct EventHookEvent {
pub event_type: EventHookEventType,
pub table: String,
pub data: HashMap<String, Value>,
pub timestamp: u64,
}
impl EventHookEvent {
pub fn new(event_type: EventHookEventType, table: impl Into<String>) -> Self {
Self {
event_type,
table: table.into(),
data: HashMap::new(),
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
}
}
pub fn with_data(mut self, key: impl Into<String>, value: Value) -> Self {
self.data.insert(key.into(), value);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_event_hook_event_creation() {
let event = EventHookEvent::new(EventHookEventType::AfterInsert, "users")
.with_data("id", Value::Int(1))
.with_data("name", Value::String("Alice".to_string()));
assert_eq!(event.event_type, EventHookEventType::AfterInsert);
assert_eq!(event.table, "users");
assert_eq!(event.data.len(), 2);
}
}