Skip to main content

backbone_support/
lib.rs

1//! Support Module
2//!
3//! Generated by metaphor-schema. Enhanced with runtime implementations.
4//!
5//! This module provides:
6//! - Domain entities and repositories
7//! - Application services
8//! - HTTP and gRPC handlers
9//! - Route configuration
10//! - State machine enforcement
11//! - Validation rules runtime
12//! - RBAC middleware
13//! - Trigger execution system
14//! - Computed fields
15//! - Workflow orchestrator
16
17#![recursion_limit = "1024"]
18#![allow(unused_imports)]
19
20// Generated modules
21pub mod domain;
22pub mod infrastructure;
23pub mod application;
24pub mod presentation;
25pub mod seeders;
26pub mod exports;
27
28// Re-exports for convenience - Domain entities
29pub use domain::entity::*;
30
31// Re-exports - Infrastructure
32pub use infrastructure::persistence::*;
33
34// Re-exports - Application services
35pub use application::service::IssueService;
36pub use application::service::ServiceLevelAgreementService;
37pub use application::service::ServiceLevelPriorityService;
38pub use application::service::WarrantyClaimService;
39
40use std::sync::Arc;
41use axum::Router;
42use sqlx::PgPool;
43
44/// Support module configuration
45///
46/// Use the builder pattern to configure and register this module:
47///
48/// ```text
49/// let support = SupportModule::builder()
50///     .with_database(pool.clone())
51///     .build()?;
52///
53/// // Unguarded full CRUD (trusted/admin); compose a guarded router for production.
54/// let router = support.all_crud_routes();
55/// ```
56pub struct SupportModule {
57    pub(crate) issue_service: Arc<IssueService>,
58    pub(crate) service_level_agreement_service: Arc<ServiceLevelAgreementService>,
59    pub(crate) service_level_priority_service: Arc<ServiceLevelPriorityService>,
60    pub(crate) warranty_claim_service: Arc<WarrantyClaimService>,
61    // <<< CUSTOM FIELDS
62    // END CUSTOM
63}
64
65impl SupportModule {
66    /// Create a new module builder
67    pub fn builder() -> SupportModuleBuilder {
68        SupportModuleBuilder::new()
69    }
70
71    /// Mount ALL generated CRUD endpoints (12 per entity) with NO domain
72    /// validation — the fully **unguarded** surface. A well-formed request can
73    /// create invalid rows or soft-delete a referenced master out from under its
74    /// dependents. Prefer a guarded composition (read + validated writes) for any
75    /// real deployment; use this only in trusted/admin/seeding contexts.
76    pub fn all_crud_routes(&self) -> Router {
77        use presentation::http::{
78            create_issue_routes,
79            create_service_level_agreement_routes,
80            create_service_level_priority_routes,
81            create_warranty_claim_routes,
82        };
83
84        Router::new()
85            .merge(create_issue_routes(self.issue_service.clone()))
86            .merge(create_service_level_agreement_routes(self.service_level_agreement_service.clone()))
87            .merge(create_service_level_priority_routes(self.service_level_priority_service.clone()))
88            .merge(create_warranty_claim_routes(self.warranty_claim_service.clone()))
89    }
90
91    /// Deprecated alias for [`Self::all_crud_routes`]. `routes()` reads like
92    /// "the routes" but mounts UNVALIDATED generic CRUD on every entity — a naive
93    /// mount exposes unguarded writes. Compose a guarded router (read + validated
94    /// writes) for production, or call `all_crud_routes()` to opt into the full
95    /// unguarded surface explicitly.
96    #[deprecated(note = "mounts unvalidated generic CRUD; prefer readonly_routes() + validated writes, or all_crud_routes() for the full/unguarded surface")]
97    pub fn routes(&self) -> Router {
98        self.all_crud_routes()
99    }
100
101    /// Read-only routes for every entity (GET endpoints only) — the safe base.
102    ///
103    /// Generic mutation can't reach here, so this surface cannot bypass a
104    /// validated write service's invariants. Use this as the production base and
105    /// merge validated write routes (or a write service's HTTP layer) onto it.
106    pub fn readonly_routes(&self) -> Router {
107        use presentation::http::{
108            create_issue_read_routes,
109            create_service_level_agreement_read_routes,
110            create_service_level_priority_read_routes,
111            create_warranty_claim_read_routes,
112        };
113
114        Router::new()
115            .merge(create_issue_read_routes(self.issue_service.clone()))
116            .merge(create_service_level_agreement_read_routes(self.service_level_agreement_service.clone()))
117            .merge(create_service_level_priority_read_routes(self.service_level_priority_service.clone()))
118            .merge(create_warranty_claim_read_routes(self.warranty_claim_service.clone()))
119    }
120
121    // <<< CUSTOM METHODS
122    // END CUSTOM
123}
124
125/// Builder for SupportModule
126pub struct SupportModuleBuilder {
127    db_pool: Option<PgPool>,
128}
129
130impl SupportModuleBuilder {
131    /// Create a new builder
132    pub fn new() -> Self {
133        Self {
134            db_pool: None,
135        }
136    }
137
138    /// Set the database connection pool
139    pub fn with_database(mut self, pool: PgPool) -> Self {
140        self.db_pool = Some(pool);
141        self
142    }
143
144    // <<< CUSTOM - custom builder methods
145    // END CUSTOM
146
147    /// Build the module with configured dependencies
148    pub fn build(self) -> anyhow::Result<SupportModule> {
149        let db_pool = self.db_pool
150            .ok_or_else(|| anyhow::anyhow!("Database pool not configured"))?;
151
152        // Issue service
153        let issue_repository = Arc::new(IssueRepository::new(db_pool.clone()));
154        let issue_service = Arc::new(IssueService::with_repository(issue_repository.clone()));
155
156        // ServiceLevelAgreement service
157        let service_level_agreement_repository = Arc::new(ServiceLevelAgreementRepository::new(db_pool.clone()));
158        let service_level_agreement_service = Arc::new(ServiceLevelAgreementService::with_repository(service_level_agreement_repository.clone()));
159
160        // ServiceLevelPriority service
161        let service_level_priority_repository = Arc::new(ServiceLevelPriorityRepository::new(db_pool.clone()));
162        let service_level_priority_service = Arc::new(ServiceLevelPriorityService::with_repository(service_level_priority_repository.clone()));
163
164        // WarrantyClaim service
165        let warranty_claim_repository = Arc::new(WarrantyClaimRepository::new(db_pool.clone()));
166        let warranty_claim_service = Arc::new(WarrantyClaimService::with_repository(warranty_claim_repository.clone()));
167
168        // <<< CUSTOM
169        // END CUSTOM
170
171        Ok(SupportModule {
172            issue_service,
173            service_level_agreement_service,
174            service_level_priority_service,
175            warranty_claim_service,
176            // <<< CUSTOM
177            // END CUSTOM
178        })
179    }
180}
181
182impl Default for SupportModuleBuilder {
183    fn default() -> Self {
184        Self::new()
185    }
186}