Skip to main content

backbone_integrations/
lib.rs

1//! Integrations 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// The module's extension (hand-written; ADR-0031).
29#[path = "lib.ext.rs"]
30mod lib_ext;
31pub use lib_ext::*;
32/// This crate's module and builder under fixed names, for the extension's `impl` blocks.
33#[allow(dead_code)]
34pub(crate) type ThisModule = IntegrationsModule;
35#[allow(dead_code)]
36pub(crate) type ThisModuleBuilder = IntegrationsModuleBuilder;
37
38// Re-exports - Infrastructure
39pub use infrastructure::persistence::*;
40
41// Re-exports - Application services
42pub use application::service::IntegrationConnectorService;
43pub use application::service::IntegrationAccountService;
44pub use application::service::IntegrationEventService;
45
46use std::sync::Arc;
47use axum::Router;
48use sqlx::PgPool;
49
50/// Integrations module configuration
51///
52/// Use the builder pattern to configure and register this module:
53///
54/// ```text
55/// let integrations = IntegrationsModule::builder()
56///     .with_database(pool.clone())
57///     .build()?;
58///
59/// // Unguarded full CRUD (trusted/admin); compose a guarded router for production.
60/// let router = integrations.all_crud_routes();
61/// ```
62pub struct IntegrationsModule {
63    pub(crate) integration_connector_service: Arc<IntegrationConnectorService>,
64    #[allow(dead_code)]
65    pub(crate) integration_account_service: Arc<IntegrationAccountService>,
66    pub(crate) integration_event_service: Arc<IntegrationEventService>,
67    /// The module's extension state (`lib.ext.rs`); its fields read through `Deref`.
68    pub(crate) ext: ModuleExt,
69}
70
71impl std::ops::Deref for IntegrationsModule {
72    type Target = ModuleExt;
73    fn deref(&self) -> &Self::Target {
74        &self.ext
75    }
76}
77
78impl std::ops::DerefMut for IntegrationsModule {
79    fn deref_mut(&mut self) -> &mut Self::Target {
80        &mut self.ext
81    }
82}
83
84/// What the extension's `build` hook receives from the generated build (ADR-0031).
85#[allow(dead_code)]
86pub(crate) struct ModuleParts<'a> {
87    pub(crate) db_pool: &'a PgPool,
88    pub(crate) integration_connector_service: &'a Arc<IntegrationConnectorService>,
89    pub(crate) integration_connector_repository: &'a Arc<IntegrationConnectorRepository>,
90    pub(crate) integration_account_service: &'a Arc<IntegrationAccountService>,
91    pub(crate) integration_account_repository: &'a Arc<IntegrationAccountRepository>,
92    pub(crate) integration_event_service: &'a Arc<IntegrationEventService>,
93    pub(crate) integration_event_repository: &'a Arc<IntegrationEventRepository>,
94}
95
96impl IntegrationsModule {
97    /// Create a new module builder
98    pub fn builder() -> IntegrationsModuleBuilder {
99        IntegrationsModuleBuilder::new()
100    }
101
102    /// Mount ALL generated CRUD endpoints (12 per entity) with NO domain
103    /// validation — the fully **unguarded** surface. A well-formed request can
104    /// create invalid rows or soft-delete a referenced master out from under its
105    /// dependents. Prefer a guarded composition (read + validated writes) for any
106    /// real deployment; use this only in trusted/admin/seeding contexts.
107    pub fn all_crud_routes(&self) -> Router {
108        use presentation::http::{
109            create_integration_connector_routes,
110            create_integration_event_routes,
111        };
112
113        Router::new()
114            .merge(create_integration_connector_routes(self.integration_connector_service.clone()))
115            .merge(create_integration_event_routes(self.integration_event_service.clone()))
116    }
117
118    /// Deprecated alias for [`Self::all_crud_routes`]. `routes()` reads like
119    /// "the routes" but mounts UNVALIDATED generic CRUD on every entity — a naive
120    /// mount exposes unguarded writes. Compose a guarded router (read + validated
121    /// writes) for production, or call `all_crud_routes()` to opt into the full
122    /// unguarded surface explicitly.
123    #[deprecated(note = "mounts unvalidated generic CRUD; prefer readonly_routes() + validated writes, or all_crud_routes() for the full/unguarded surface")]
124    pub fn routes(&self) -> Router {
125        self.all_crud_routes()
126    }
127
128    /// Read-only routes for every entity (GET endpoints only) — the safe base.
129    ///
130    /// Generic mutation can't reach here, so this surface cannot bypass a
131    /// validated write service's invariants. Use this as the production base and
132    /// merge validated write routes (or a write service's HTTP layer) onto it.
133    pub fn readonly_routes(&self) -> Router {
134        use presentation::http::{
135            create_integration_connector_read_routes,
136            create_integration_event_read_routes,
137        };
138
139        Router::new()
140            .merge(create_integration_connector_read_routes(self.integration_connector_service.clone()))
141            .merge(create_integration_event_read_routes(self.integration_event_service.clone()))
142    }
143
144}
145
146/// Builder for IntegrationsModule
147pub struct IntegrationsModuleBuilder {
148    db_pool: Option<PgPool>,
149    /// The builder's extension state (`lib.ext.rs`); its fields read through `Deref`.
150    ext: ModuleBuilderExt,
151}
152
153impl std::ops::Deref for IntegrationsModuleBuilder {
154    type Target = ModuleBuilderExt;
155    fn deref(&self) -> &Self::Target {
156        &self.ext
157    }
158}
159
160impl std::ops::DerefMut for IntegrationsModuleBuilder {
161    fn deref_mut(&mut self) -> &mut Self::Target {
162        &mut self.ext
163    }
164}
165
166impl IntegrationsModuleBuilder {
167    /// Create a new builder
168    pub fn new() -> Self {
169        Self {
170            db_pool: None,
171            ext: Default::default(),
172        }
173    }
174
175    /// Set the database connection pool
176    pub fn with_database(mut self, pool: PgPool) -> Self {
177        self.db_pool = Some(pool);
178        self
179    }
180
181
182    /// Build the module with configured dependencies
183    pub fn build(self) -> anyhow::Result<IntegrationsModule> {
184        let db_pool = self.db_pool
185            .ok_or_else(|| anyhow::anyhow!("Database pool not configured"))?;
186
187        // IntegrationConnector service
188        let integration_connector_repository = Arc::new(IntegrationConnectorRepository::new(db_pool.clone()));
189        let integration_connector_service = Arc::new(IntegrationConnectorService::with_repository(integration_connector_repository.clone()).with_guard(Arc::new(crate::application::service::IntegrationConnectorRowRules)));
190
191        // IntegrationAccount service
192        let integration_account_repository = Arc::new(IntegrationAccountRepository::new(db_pool.clone()));
193        let integration_account_service = Arc::new(IntegrationAccountService::with_repository(integration_account_repository.clone()).with_guard(Arc::new(crate::application::service::IntegrationAccountRowRules)));
194
195        // IntegrationEvent service
196        let integration_event_repository = Arc::new(IntegrationEventRepository::new(db_pool.clone()));
197        let integration_event_service = Arc::new(IntegrationEventService::with_repository(integration_event_repository.clone()).with_guard(Arc::new(crate::application::service::IntegrationEventRowRules)));
198
199
200        // The extension builds its own state from what the generated build made.
201        let ext = self.ext.build(&ModuleParts {
202            db_pool: &db_pool,
203            integration_connector_service: &integration_connector_service,
204            integration_connector_repository: &integration_connector_repository,
205            integration_account_service: &integration_account_service,
206            integration_account_repository: &integration_account_repository,
207            integration_event_service: &integration_event_service,
208            integration_event_repository: &integration_event_repository,
209        })?;
210
211        Ok(IntegrationsModule {
212            integration_connector_service,
213            integration_account_service,
214            integration_event_service,
215            ext,
216        })
217    }
218}
219
220impl Default for IntegrationsModuleBuilder {
221    fn default() -> Self {
222        Self::new()
223    }
224}
225
226// This directory's extension (hand-written; ADR-0031).
227include!("mod.ext.rs");