backbone-activity 0.8.0

Sales activity / touchpoints + rep tasks — the substrate lead/deal sit on
Documentation
//! Activity 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;

// 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::ActivityService;

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

/// Activity module configuration
///
/// Use the builder pattern to configure and register this module:
///
/// ```text
/// let activity = ActivityModule::builder()
///     .with_database(pool.clone())
///     .build()?;
///
/// // Unguarded full CRUD (trusted/admin); compose a guarded router for production.
/// let router = activity.all_crud_routes();
/// ```
pub struct ActivityModule {
    pub(crate) activity_service: Arc<ActivityService>,
}

impl ActivityModule {
    /// Create a new module builder
    pub fn builder() -> ActivityModuleBuilder {
        ActivityModuleBuilder::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_activity_routes,
        };

        Router::new()
            .merge(create_activity_routes(self.activity_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_activity_read_routes,
        };

        Router::new()
            .merge(create_activity_read_routes(self.activity_service.clone()))
    }

}

/// Builder for ActivityModule
pub struct ActivityModuleBuilder {
    db_pool: Option<PgPool>,
}

impl ActivityModuleBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self {
            db_pool: None,
        }
    }

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


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

        // Activity service
        let activity_repository = Arc::new(ActivityRepository::new(db_pool.clone()));
        let activity_service = Arc::new(crate::application::service::build_activity_service(activity_repository.clone()));


        Ok(ActivityModule {
            activity_service,
        })
    }
}

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