use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use super::IntegrationStatus;
use super::AuditMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct IntegrationEventId(pub Uuid);
impl IntegrationEventId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for IntegrationEventId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for IntegrationEventId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for IntegrationEventId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<IntegrationEventId> for Uuid {
fn from(id: IntegrationEventId) -> Self { id.0 }
}
impl AsRef<Uuid> for IntegrationEventId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for IntegrationEventId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct IntegrationEvent {
pub id: Uuid,
pub connector_id: Uuid,
pub event_type: String,
pub external_id: String,
pub business_key: String,
pub status: IntegrationStatus,
pub payload: String,
pub mapped_ref_type: Option<String>,
pub mapped_ref_id: Option<Uuid>,
pub error_detail: Option<String>,
#[serde(default)]
#[sqlx(json)]
pub metadata: AuditMetadata,
}
impl IntegrationEvent {
pub fn builder() -> IntegrationEventBuilder {
<IntegrationEventBuilder as Default>::default()
}
pub fn new(connector_id: Uuid, event_type: String, external_id: String, business_key: String, status: IntegrationStatus, payload: String) -> Self {
Self {
id: Uuid::new_v4(),
connector_id,
event_type,
external_id,
business_key,
status,
payload,
mapped_ref_type: None,
mapped_ref_id: None,
error_detail: None,
metadata: AuditMetadata::default(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> IntegrationEventId {
IntegrationEventId(self.id)
}
pub fn created_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.created_at.as_ref()
}
pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.updated_at.as_ref()
}
pub fn is_deleted(&self) -> bool {
self.metadata.deleted_at.is_some()
}
pub fn is_active(&self) -> bool {
self.metadata.deleted_at.is_none()
}
pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.deleted_at.as_ref()
}
pub fn created_by(&self) -> Option<&Uuid> {
self.metadata.created_by.as_ref()
}
pub fn updated_by(&self) -> Option<&Uuid> {
self.metadata.updated_by.as_ref()
}
pub fn deleted_by(&self) -> Option<&Uuid> {
self.metadata.deleted_by.as_ref()
}
pub fn status(&self) -> &IntegrationStatus {
&self.status
}
pub fn with_mapped_ref_type(mut self, value: String) -> Self {
self.mapped_ref_type = Some(value);
self
}
pub fn with_mapped_ref_id(mut self, value: Uuid) -> Self {
self.mapped_ref_id = Some(value);
self
}
pub fn with_error_detail(mut self, value: String) -> Self {
self.error_detail = Some(value);
self
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"connector_id" => {
if let Ok(v) = serde_json::from_value(value) { self.connector_id = v; }
}
"event_type" => {
if let Ok(v) = serde_json::from_value(value) { self.event_type = v; }
}
"external_id" => {
if let Ok(v) = serde_json::from_value(value) { self.external_id = v; }
}
"business_key" => {
if let Ok(v) = serde_json::from_value(value) { self.business_key = v; }
}
"status" => {
if let Ok(v) = serde_json::from_value(value) { self.status = v; }
}
"payload" => {
if let Ok(v) = serde_json::from_value(value) { self.payload = v; }
}
"mapped_ref_type" => {
if let Ok(v) = serde_json::from_value(value) { self.mapped_ref_type = v; }
}
"mapped_ref_id" => {
if let Ok(v) = serde_json::from_value(value) { self.mapped_ref_id = v; }
}
"error_detail" => {
if let Ok(v) = serde_json::from_value(value) { self.error_detail = v; }
}
_ => {} }
}
}
}
impl super::Entity for IntegrationEvent {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"IntegrationEvent"
}
}
impl backbone_core::PersistentEntity for IntegrationEvent {
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.created_at
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.created_at = Some(ts);
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.updated_at
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.updated_at = Some(ts);
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.deleted_at
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
self.metadata.deleted_at = ts;
}
}
impl backbone_orm::EntityRepoMeta for IntegrationEvent {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("connector_id".to_string(), "uuid".to_string());
m.insert("mapped_ref_id".to_string(), "uuid".to_string());
m.insert("status".to_string(), "integration_status".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&["event_type", "external_id", "business_key", "payload"]
}
}
#[derive(Debug, Clone, Default)]
pub struct IntegrationEventBuilder {
connector_id: Option<Uuid>,
event_type: Option<String>,
external_id: Option<String>,
business_key: Option<String>,
status: Option<IntegrationStatus>,
payload: Option<String>,
mapped_ref_type: Option<String>,
mapped_ref_id: Option<Uuid>,
error_detail: Option<String>,
}
impl IntegrationEventBuilder {
pub fn connector_id(mut self, value: Uuid) -> Self {
self.connector_id = Some(value);
self
}
pub fn event_type(mut self, value: String) -> Self {
self.event_type = Some(value);
self
}
pub fn external_id(mut self, value: String) -> Self {
self.external_id = Some(value);
self
}
pub fn business_key(mut self, value: String) -> Self {
self.business_key = Some(value);
self
}
pub fn status(mut self, value: IntegrationStatus) -> Self {
self.status = Some(value);
self
}
pub fn payload(mut self, value: String) -> Self {
self.payload = Some(value);
self
}
pub fn mapped_ref_type(mut self, value: String) -> Self {
self.mapped_ref_type = Some(value);
self
}
pub fn mapped_ref_id(mut self, value: Uuid) -> Self {
self.mapped_ref_id = Some(value);
self
}
pub fn error_detail(mut self, value: String) -> Self {
self.error_detail = Some(value);
self
}
pub fn build(self) -> Result<IntegrationEvent, String> {
let connector_id = self.connector_id.ok_or_else(|| "connector_id is required".to_string())?;
let event_type = self.event_type.ok_or_else(|| "event_type is required".to_string())?;
let external_id = self.external_id.ok_or_else(|| "external_id is required".to_string())?;
let business_key = self.business_key.ok_or_else(|| "business_key is required".to_string())?;
let payload = self.payload.ok_or_else(|| "payload is required".to_string())?;
Ok(IntegrationEvent {
id: Uuid::new_v4(),
connector_id,
event_type,
external_id,
business_key,
status: self.status.unwrap_or_default(),
payload,
mapped_ref_type: self.mapped_ref_type,
mapped_ref_id: self.mapped_ref_id,
error_detail: self.error_detail,
metadata: AuditMetadata::default(),
})
}
}