Skip to main content

backbone_maintenance/domain/entity/
mod.rs

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