backbone-support 0.7.0

Customer support: issues tracked against service level agreements, and warranty claims
Documentation
//! Support Module
//!
//! Generated by metaphor-schema. Enhanced with runtime implementations.
//!
//! This module provides:
//! - Domain entities and repositories
//! - Application services
//! - HTTP and gRPC handlers
//! - Route configuration
//! - State machine enforcement
//! - Validation rules runtime
//! - RBAC middleware
//! - Trigger execution system
//! - Computed fields
//! - Workflow orchestrator

#![recursion_limit = "1024"]
#![allow(unused_imports)]

// Generated modules
pub mod domain;
pub mod infrastructure;
pub mod application;
pub mod presentation;
pub mod seeders;
pub mod exports;
// <<< CUSTOM MODULES
// END CUSTOM

// Re-exports for convenience - Domain entities
pub use domain::entity::*;

// Re-exports - Infrastructure
pub use infrastructure::persistence::*;

// Re-exports - Application services
pub use application::service::IssueService;
pub use application::service::ServiceLevelAgreementService;
pub use application::service::ServiceLevelPriorityService;
pub use application::service::WarrantyClaimService;

use std::sync::Arc;
use axum::Router;
use sqlx::PgPool;

/// Support module configuration
///
/// Use the builder pattern to configure and register this module:
///
/// ```text
/// let support = SupportModule::builder()
///     .with_database(pool.clone())
///     .build()?;
///
/// // Unguarded full CRUD (trusted/admin); compose a guarded router for production.
/// let router = support.all_crud_routes();
/// ```
pub struct SupportModule {
    pub(crate) issue_service: Arc<IssueService>,
    pub(crate) service_level_agreement_service: Arc<ServiceLevelAgreementService>,
    pub(crate) service_level_priority_service: Arc<ServiceLevelPriorityService>,
    pub(crate) warranty_claim_service: Arc<WarrantyClaimService>,
    // <<< CUSTOM FIELDS
    // END CUSTOM
}

impl SupportModule {
    /// Create a new module builder
    pub fn builder() -> SupportModuleBuilder {
        SupportModuleBuilder::new()
    }

    /// Mount ALL generated CRUD endpoints (12 per entity) with NO domain
    /// validation — the fully **unguarded** surface. A well-formed request can
    /// create invalid rows or soft-delete a referenced master out from under its
    /// dependents. Prefer a guarded composition (read + validated writes) for any
    /// real deployment; use this only in trusted/admin/seeding contexts.
    pub fn all_crud_routes(&self) -> Router {
        use presentation::http::{
            create_issue_routes,
            create_service_level_agreement_routes,
            create_service_level_priority_routes,
            create_warranty_claim_routes,
        };

        Router::new()
            .merge(create_issue_routes(self.issue_service.clone()))
            .merge(create_service_level_agreement_routes(self.service_level_agreement_service.clone()))
            .merge(create_service_level_priority_routes(self.service_level_priority_service.clone()))
            .merge(create_warranty_claim_routes(self.warranty_claim_service.clone()))
    }

    /// Deprecated alias for [`Self::all_crud_routes`]. `routes()` reads like
    /// "the routes" but mounts UNVALIDATED generic CRUD on every entity — a naive
    /// mount exposes unguarded writes. Compose a guarded router (read + validated
    /// writes) for production, or call `all_crud_routes()` to opt into the full
    /// unguarded surface explicitly.
    #[deprecated(note = "mounts unvalidated generic CRUD; prefer readonly_routes() + validated writes, or all_crud_routes() for the full/unguarded surface")]
    pub fn routes(&self) -> Router {
        self.all_crud_routes()
    }

    /// Read-only routes for every entity (GET endpoints only) — the safe base.
    ///
    /// Generic mutation can't reach here, so this surface cannot bypass a
    /// validated write service's invariants. Use this as the production base and
    /// merge validated write routes (or a write service's HTTP layer) onto it.
    pub fn readonly_routes(&self) -> Router {
        use presentation::http::{
            create_issue_read_routes,
            create_service_level_agreement_read_routes,
            create_service_level_priority_read_routes,
            create_warranty_claim_read_routes,
        };

        Router::new()
            .merge(create_issue_read_routes(self.issue_service.clone()))
            .merge(create_service_level_agreement_read_routes(self.service_level_agreement_service.clone()))
            .merge(create_service_level_priority_read_routes(self.service_level_priority_service.clone()))
            .merge(create_warranty_claim_read_routes(self.warranty_claim_service.clone()))
    }

    // <<< CUSTOM METHODS
    // END CUSTOM
}

/// Builder for SupportModule
pub struct SupportModuleBuilder {
    db_pool: Option<PgPool>,
    // <<< CUSTOM BUILDER FIELDS
    // END CUSTOM
}

impl SupportModuleBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self {
            db_pool: None,
            // <<< CUSTOM BUILDER DEFAULTS
            // END CUSTOM
        }
    }

    /// Set the database connection pool
    pub fn with_database(mut self, pool: PgPool) -> Self {
        self.db_pool = Some(pool);
        self
    }

    // <<< CUSTOM - custom builder methods
    // END CUSTOM

    /// Build the module with configured dependencies
    pub fn build(self) -> anyhow::Result<SupportModule> {
        let db_pool = self.db_pool
            .ok_or_else(|| anyhow::anyhow!("Database pool not configured"))?;

        // Issue service
        let issue_repository = Arc::new(IssueRepository::new(db_pool.clone()));
        let issue_service = Arc::new(IssueService::with_repository(issue_repository.clone()));

        // ServiceLevelAgreement service
        let service_level_agreement_repository = Arc::new(ServiceLevelAgreementRepository::new(db_pool.clone()));
        let service_level_agreement_service = Arc::new(ServiceLevelAgreementService::with_repository(service_level_agreement_repository.clone()));

        // ServiceLevelPriority service
        let service_level_priority_repository = Arc::new(ServiceLevelPriorityRepository::new(db_pool.clone()));
        let service_level_priority_service = Arc::new(ServiceLevelPriorityService::with_repository(service_level_priority_repository.clone()));

        // WarrantyClaim service
        let warranty_claim_repository = Arc::new(WarrantyClaimRepository::new(db_pool.clone()));
        let warranty_claim_service = Arc::new(WarrantyClaimService::with_repository(warranty_claim_repository.clone()));

        // <<< CUSTOM
        // END CUSTOM

        Ok(SupportModule {
            issue_service,
            service_level_agreement_service,
            service_level_priority_service,
            warranty_claim_service,
            // <<< CUSTOM
            // END CUSTOM
        })
    }
}

impl Default for SupportModuleBuilder {
    fn default() -> Self {
        Self::new()
    }
}