Skip to main content

backbone_catalog/domain/entity/
mod.rs

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