Skip to main content

backbone_integrations/domain/entity/
mod.rs

1//! Domain Entities
2//!
3//! Generated by metaphor-schema. Do not edit manually.
4
5pub mod integration_connector;
6pub mod integration_account;
7pub mod integration_event;
8pub mod connector_kind;
9pub mod connector_direction;
10pub mod connector_status;
11pub mod o_auth_provider;
12pub mod integration_account_status;
13pub mod integration_status;
14
15// Re-exports
16pub use integration_connector::IntegrationConnector;
17pub use integration_connector::IntegrationConnectorBuilder;
18pub use integration_connector::IntegrationConnectorId;
19pub use integration_account::IntegrationAccount;
20pub use integration_account::IntegrationAccountBuilder;
21pub use integration_account::IntegrationAccountId;
22pub use integration_event::IntegrationEvent;
23pub use integration_event::IntegrationEventBuilder;
24pub use integration_event::IntegrationEventId;
25pub use connector_kind::ConnectorKind;
26pub use connector_direction::ConnectorDirection;
27pub use connector_status::ConnectorStatus;
28pub use o_auth_provider::OAuthProvider;
29pub use integration_account_status::IntegrationAccountStatus;
30pub use integration_status::IntegrationStatus;
31
32// ==========================================================================
33// Entity Trait
34// ==========================================================================
35
36use std::fmt::Debug;
37
38/// Trait for domain entities
39///
40/// All generated entities implement this trait, providing
41/// a common interface for working with domain objects.
42pub trait Entity: Debug + Clone {
43    /// The type of the entity's unique identifier
44    type Id: Debug + Clone + PartialEq;
45
46    /// Get the entity's unique identifier
47    fn entity_id(&self) -> &Self::Id;
48
49    /// Get the entity type name
50    fn entity_type() -> &'static str;
51}
52
53// ==========================================================================
54// Audit Metadata
55// ==========================================================================
56
57use chrono::{DateTime, Utc};
58use serde::{Deserialize, Serialize};
59use uuid::Uuid;
60#[cfg(feature = "openapi")]
61use utoipa::ToSchema;
62
63/// Audit metadata stored as JSONB in the database
64///
65/// This struct consolidates audit fields (timestamps and actors) into a single
66/// JSONB column for efficient storage and flexible querying.
67#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
68#[cfg_attr(feature = "openapi", derive(ToSchema))]
69pub struct AuditMetadata {
70    /// Timestamp when the record was created
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub created_at: Option<DateTime<Utc>>,
73
74    /// Timestamp when the record was last updated
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub updated_at: Option<DateTime<Utc>>,
77
78    /// Timestamp when the record was soft-deleted
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub deleted_at: Option<DateTime<Utc>>,
81
82    /// User ID who created the record
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub created_by: Option<Uuid>,
85
86    /// User ID who last updated the record
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub updated_by: Option<Uuid>,
89
90    /// User ID who deleted the record
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub deleted_by: Option<Uuid>,
93}
94
95impl AuditMetadata {
96    /// Create new audit metadata with created_at set to now
97    pub fn new() -> Self {
98        Self {
99            created_at: Some(Utc::now()),
100            ..Default::default()
101        }
102    }
103
104    /// Create with creator ID
105    pub fn with_creator(creator_id: Uuid) -> Self {
106        Self {
107            created_at: Some(Utc::now()),
108            created_by: Some(creator_id),
109            ..Default::default()
110        }
111    }
112
113    /// Touch the record (update updated_at)
114    pub fn touch(&mut self) {
115        self.updated_at = Some(Utc::now());
116    }
117
118    /// Touch with updater ID
119    pub fn touch_by(&mut self, updater_id: Uuid) {
120        self.updated_at = Some(Utc::now());
121        self.updated_by = Some(updater_id);
122    }
123
124    /// Soft delete the record
125    pub fn soft_delete(&mut self) {
126        self.deleted_at = Some(Utc::now());
127    }
128
129    /// Soft delete with deleter ID
130    pub fn soft_delete_by(&mut self, deleter_id: Uuid) {
131        self.deleted_at = Some(Utc::now());
132        self.deleted_by = Some(deleter_id);
133    }
134
135    /// Restore a soft-deleted record
136    pub fn restore(&mut self) {
137        self.deleted_at = None;
138        self.deleted_by = None;
139    }
140
141    /// Check if record is deleted
142    pub fn is_deleted(&self) -> bool {
143        self.deleted_at.is_some()
144    }
145
146    /// Check if record is active (not deleted)
147    pub fn is_active(&self) -> bool {
148        self.deleted_at.is_none()
149    }
150}