Skip to main content

backbone_accounting/
lib.rs

1//! Accounting 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 = AccountingModule;
35#[allow(dead_code)]
36pub(crate) type ThisModuleBuilder = AccountingModuleBuilder;
37
38// Re-exports for convenience - Domain entities
39pub use domain::entity::*;
40
41// Re-exports - Infrastructure
42pub use infrastructure::persistence::*;
43
44// Re-exports - Application services
45pub use application::service::AccountService;
46pub use application::service::AccountingPostService;
47pub use application::service::CostCenterService;
48pub use application::service::EmvQrConfigService;
49pub use application::service::BankCheckSequenceService;
50pub use application::service::PrintedCheckService;
51pub use application::service::TaxTagRepairRunService;
52pub use application::service::FinancialStatementService;
53pub use application::service::FiscalPeriodService;
54pub use application::service::JournalService;
55pub use application::service::JournalLineService;
56pub use application::service::LedgerService;
57pub use application::service::ReconciliationService;
58pub use application::service::ReconciliationItemService;
59pub use application::service::FullReconcileService;
60pub use application::service::PartialReconcileService;
61
62// Re-exports - Validation
63pub use application::validator::{ValidationError, ValidationResult};
64
65// Re-exports - Workflows
66pub use application::workflows::*;
67
68use std::sync::Arc;
69use axum::Router;
70use sqlx::PgPool;
71
72/// Accounting module configuration
73///
74/// Use the builder pattern to configure and register this module:
75///
76/// ```text
77/// let accounting = AccountingModule::builder()
78///     .with_database(pool.clone())
79///     .build()?;
80///
81/// // Unguarded full CRUD (trusted/admin); compose a guarded router for production.
82/// let router = accounting.all_crud_routes();
83/// ```
84pub struct AccountingModule {
85    pub(crate) account_service: Arc<AccountService>,
86    pub(crate) accounting_post_service: Arc<AccountingPostService>,
87    pub(crate) cost_center_service: Arc<CostCenterService>,
88    pub(crate) emv_qr_config_service: Arc<EmvQrConfigService>,
89    pub(crate) bank_check_sequence_service: Arc<BankCheckSequenceService>,
90    pub(crate) printed_check_service: Arc<PrintedCheckService>,
91    pub(crate) tax_tag_repair_run_service: Arc<TaxTagRepairRunService>,
92    pub(crate) financial_statement_service: Arc<FinancialStatementService>,
93    pub(crate) fiscal_period_service: Arc<FiscalPeriodService>,
94    pub(crate) journal_service: Arc<JournalService>,
95    pub(crate) journal_line_service: Arc<JournalLineService>,
96    pub(crate) ledger_service: Arc<LedgerService>,
97    pub(crate) reconciliation_service: Arc<ReconciliationService>,
98    pub(crate) reconciliation_item_service: Arc<ReconciliationItemService>,
99    pub(crate) full_reconcile_service: Arc<FullReconcileService>,
100    pub(crate) partial_reconcile_service: Arc<PartialReconcileService>,
101    /// The module's extension state (`lib.ext.rs`); its fields read through `Deref`.
102    pub(crate) ext: ModuleExt,
103}
104
105impl std::ops::Deref for AccountingModule {
106    type Target = ModuleExt;
107    fn deref(&self) -> &Self::Target {
108        &self.ext
109    }
110}
111
112impl std::ops::DerefMut for AccountingModule {
113    fn deref_mut(&mut self) -> &mut Self::Target {
114        &mut self.ext
115    }
116}
117
118/// What the extension's `build` hook receives from the generated build (ADR-0031).
119#[allow(dead_code)]
120pub(crate) struct ModuleParts<'a> {
121    pub(crate) db_pool: &'a PgPool,
122    pub(crate) account_service: &'a Arc<AccountService>,
123    pub(crate) account_repository: &'a Arc<AccountRepository>,
124    pub(crate) accounting_post_service: &'a Arc<AccountingPostService>,
125    pub(crate) accounting_post_repository: &'a Arc<AccountingPostRepository>,
126    pub(crate) cost_center_service: &'a Arc<CostCenterService>,
127    pub(crate) cost_center_repository: &'a Arc<CostCenterRepository>,
128    pub(crate) emv_qr_config_service: &'a Arc<EmvQrConfigService>,
129    pub(crate) emv_qr_config_repository: &'a Arc<EmvQrConfigRepository>,
130    pub(crate) bank_check_sequence_service: &'a Arc<BankCheckSequenceService>,
131    pub(crate) bank_check_sequence_repository: &'a Arc<BankCheckSequenceRepository>,
132    pub(crate) printed_check_service: &'a Arc<PrintedCheckService>,
133    pub(crate) printed_check_repository: &'a Arc<PrintedCheckRepository>,
134    pub(crate) tax_tag_repair_run_service: &'a Arc<TaxTagRepairRunService>,
135    pub(crate) tax_tag_repair_run_repository: &'a Arc<TaxTagRepairRunRepository>,
136    pub(crate) financial_statement_service: &'a Arc<FinancialStatementService>,
137    pub(crate) financial_statement_repository: &'a Arc<FinancialStatementRepository>,
138    pub(crate) fiscal_period_service: &'a Arc<FiscalPeriodService>,
139    pub(crate) fiscal_period_repository: &'a Arc<FiscalPeriodRepository>,
140    pub(crate) journal_service: &'a Arc<JournalService>,
141    pub(crate) journal_repository: &'a Arc<JournalRepository>,
142    pub(crate) journal_line_service: &'a Arc<JournalLineService>,
143    pub(crate) journal_line_repository: &'a Arc<JournalLineRepository>,
144    pub(crate) ledger_service: &'a Arc<LedgerService>,
145    pub(crate) ledger_repository: &'a Arc<LedgerRepository>,
146    pub(crate) reconciliation_service: &'a Arc<ReconciliationService>,
147    pub(crate) reconciliation_repository: &'a Arc<ReconciliationRepository>,
148    pub(crate) reconciliation_item_service: &'a Arc<ReconciliationItemService>,
149    pub(crate) reconciliation_item_repository: &'a Arc<ReconciliationItemRepository>,
150    pub(crate) full_reconcile_service: &'a Arc<FullReconcileService>,
151    pub(crate) full_reconcile_repository: &'a Arc<FullReconcileRepository>,
152    pub(crate) partial_reconcile_service: &'a Arc<PartialReconcileService>,
153    pub(crate) partial_reconcile_repository: &'a Arc<PartialReconcileRepository>,
154}
155
156impl AccountingModule {
157    /// Create a new module builder
158    pub fn builder() -> AccountingModuleBuilder {
159        AccountingModuleBuilder::new()
160    }
161
162    /// Mount ALL generated CRUD endpoints (12 per entity) with NO domain
163    /// validation — the fully **unguarded** surface. A well-formed request can
164    /// create invalid rows or soft-delete a referenced master out from under its
165    /// dependents. Prefer a guarded composition (read + validated writes) for any
166    /// real deployment; use this only in trusted/admin/seeding contexts.
167    pub fn all_crud_routes(&self) -> Router {
168        use presentation::http::{
169            create_account_routes,
170            create_accounting_post_routes,
171            create_cost_center_routes,
172            create_emv_qr_config_routes,
173            create_bank_check_sequence_routes,
174            create_printed_check_routes,
175            create_tax_tag_repair_run_routes,
176            create_financial_statement_routes,
177            create_fiscal_period_read_routes,
178            create_journal_routes,
179            create_journal_line_routes,
180            create_ledger_routes,
181            create_reconciliation_routes,
182            create_reconciliation_item_routes,
183            create_full_reconcile_routes,
184            create_partial_reconcile_routes,
185        };
186
187        Router::new()
188            .merge(create_account_routes(self.account_service.clone()))
189            .merge(create_accounting_post_routes(self.accounting_post_service.clone()))
190            .merge(create_cost_center_routes(self.cost_center_service.clone()))
191            .merge(create_emv_qr_config_routes(self.emv_qr_config_service.clone()))
192            .merge(create_bank_check_sequence_routes(self.bank_check_sequence_service.clone()))
193            .merge(create_printed_check_routes(self.printed_check_service.clone()))
194            .merge(create_tax_tag_repair_run_routes(self.tax_tag_repair_run_service.clone()))
195            .merge(create_financial_statement_routes(self.financial_statement_service.clone()))
196            // FiscalPeriod: hand_set lifecycle — the state field moves only through the
197            // module's validated verbs; generic writes cannot reach it, so only the
198            // read surface mounts here.
199            .merge(create_fiscal_period_read_routes(self.fiscal_period_service.clone()))
200            .merge(create_journal_routes(self.journal_service.clone()))
201            .merge(create_journal_line_routes(self.journal_line_service.clone()))
202            .merge(create_ledger_routes(self.ledger_service.clone()))
203            .merge(create_reconciliation_routes(self.reconciliation_service.clone()))
204            .merge(create_reconciliation_item_routes(self.reconciliation_item_service.clone()))
205            .merge(create_full_reconcile_routes(self.full_reconcile_service.clone()))
206            .merge(create_partial_reconcile_routes(self.partial_reconcile_service.clone()))
207    }
208
209    /// Deprecated alias for [`Self::all_crud_routes`]. `routes()` reads like
210    /// "the routes" but mounts UNVALIDATED generic CRUD on every entity — a naive
211    /// mount exposes unguarded writes. Compose a guarded router (read + validated
212    /// writes) for production, or call `all_crud_routes()` to opt into the full
213    /// unguarded surface explicitly.
214    #[deprecated(note = "mounts unvalidated generic CRUD; prefer readonly_routes() + validated writes, or all_crud_routes() for the full/unguarded surface")]
215    pub fn routes(&self) -> Router {
216        self.all_crud_routes()
217    }
218
219    /// Read-only routes for every entity (GET endpoints only) — the safe base.
220    ///
221    /// Generic mutation can't reach here, so this surface cannot bypass a
222    /// validated write service's invariants. Use this as the production base and
223    /// merge validated write routes (or a write service's HTTP layer) onto it.
224    pub fn readonly_routes(&self) -> Router {
225        use presentation::http::{
226            create_account_read_routes,
227            create_accounting_post_read_routes,
228            create_cost_center_read_routes,
229            create_emv_qr_config_read_routes,
230            create_bank_check_sequence_read_routes,
231            create_printed_check_read_routes,
232            create_tax_tag_repair_run_read_routes,
233            create_financial_statement_read_routes,
234            create_fiscal_period_read_routes,
235            create_journal_read_routes,
236            create_journal_line_read_routes,
237            create_ledger_read_routes,
238            create_reconciliation_read_routes,
239            create_reconciliation_item_read_routes,
240            create_full_reconcile_read_routes,
241            create_partial_reconcile_read_routes,
242        };
243
244        Router::new()
245            .merge(create_account_read_routes(self.account_service.clone()))
246            .merge(create_accounting_post_read_routes(self.accounting_post_service.clone()))
247            .merge(create_cost_center_read_routes(self.cost_center_service.clone()))
248            .merge(create_emv_qr_config_read_routes(self.emv_qr_config_service.clone()))
249            .merge(create_bank_check_sequence_read_routes(self.bank_check_sequence_service.clone()))
250            .merge(create_printed_check_read_routes(self.printed_check_service.clone()))
251            .merge(create_tax_tag_repair_run_read_routes(self.tax_tag_repair_run_service.clone()))
252            .merge(create_financial_statement_read_routes(self.financial_statement_service.clone()))
253            .merge(create_fiscal_period_read_routes(self.fiscal_period_service.clone()))
254            .merge(create_journal_read_routes(self.journal_service.clone()))
255            .merge(create_journal_line_read_routes(self.journal_line_service.clone()))
256            .merge(create_ledger_read_routes(self.ledger_service.clone()))
257            .merge(create_reconciliation_read_routes(self.reconciliation_service.clone()))
258            .merge(create_reconciliation_item_read_routes(self.reconciliation_item_service.clone()))
259            .merge(create_full_reconcile_read_routes(self.full_reconcile_service.clone()))
260            .merge(create_partial_reconcile_read_routes(self.partial_reconcile_service.clone()))
261    }
262
263}
264
265/// Builder for AccountingModule
266pub struct AccountingModuleBuilder {
267    db_pool: Option<PgPool>,
268    /// The builder's extension state (`lib.ext.rs`); its fields read through `Deref`.
269    ext: ModuleBuilderExt,
270}
271
272impl std::ops::Deref for AccountingModuleBuilder {
273    type Target = ModuleBuilderExt;
274    fn deref(&self) -> &Self::Target {
275        &self.ext
276    }
277}
278
279impl std::ops::DerefMut for AccountingModuleBuilder {
280    fn deref_mut(&mut self) -> &mut Self::Target {
281        &mut self.ext
282    }
283}
284
285impl AccountingModuleBuilder {
286    /// Create a new builder
287    pub fn new() -> Self {
288        Self {
289            db_pool: None,
290            ext: Default::default(),
291        }
292    }
293
294    /// Set the database connection pool
295    pub fn with_database(mut self, pool: PgPool) -> Self {
296        self.db_pool = Some(pool);
297        self
298    }
299
300
301    /// Build the module with configured dependencies
302    pub fn build(self) -> anyhow::Result<AccountingModule> {
303        let db_pool = self.db_pool
304            .ok_or_else(|| anyhow::anyhow!("Database pool not configured"))?;
305
306        // Account service
307        let account_repository = Arc::new(AccountRepository::new(db_pool.clone()));
308        let account_service = Arc::new(AccountService::with_repository(account_repository.clone()));
309
310        // AccountingPost service
311        let accounting_post_repository = Arc::new(AccountingPostRepository::new(db_pool.clone()));
312        let accounting_post_service = Arc::new(AccountingPostService::with_repository(accounting_post_repository.clone()));
313
314        // CostCenter service
315        let cost_center_repository = Arc::new(CostCenterRepository::new(db_pool.clone()));
316        let cost_center_service = Arc::new(CostCenterService::with_repository(cost_center_repository.clone()));
317
318        // EmvQrConfig service
319        let emv_qr_config_repository = Arc::new(EmvQrConfigRepository::new(db_pool.clone()));
320        let emv_qr_config_service = Arc::new(EmvQrConfigService::with_repository(emv_qr_config_repository.clone()));
321
322        // BankCheckSequence service
323        let bank_check_sequence_repository = Arc::new(BankCheckSequenceRepository::new(db_pool.clone()));
324        let bank_check_sequence_service = Arc::new(BankCheckSequenceService::with_repository(bank_check_sequence_repository.clone()));
325
326        // PrintedCheck service
327        let printed_check_repository = Arc::new(PrintedCheckRepository::new(db_pool.clone()));
328        let printed_check_service = Arc::new(PrintedCheckService::with_repository(printed_check_repository.clone()));
329
330        // TaxTagRepairRun service
331        let tax_tag_repair_run_repository = Arc::new(TaxTagRepairRunRepository::new(db_pool.clone()));
332        let tax_tag_repair_run_service = Arc::new(TaxTagRepairRunService::with_repository(tax_tag_repair_run_repository.clone()));
333
334        // FinancialStatement service
335        let financial_statement_repository = Arc::new(FinancialStatementRepository::new(db_pool.clone()));
336        let financial_statement_service = Arc::new(FinancialStatementService::with_repository(financial_statement_repository.clone()));
337
338        // FiscalPeriod service
339        let fiscal_period_repository = Arc::new(FiscalPeriodRepository::new(db_pool.clone()));
340        let fiscal_period_service = Arc::new(FiscalPeriodService::with_repository(fiscal_period_repository.clone()));
341
342        // Journal service
343        let journal_repository = Arc::new(JournalRepository::new(db_pool.clone()));
344        let journal_service = Arc::new(JournalService::with_repository(journal_repository.clone()).with_guard(Arc::new(crate::application::service::JournalRowRules)));
345
346        // JournalLine service
347        let journal_line_repository = Arc::new(JournalLineRepository::new(db_pool.clone()));
348        let journal_line_service = Arc::new(JournalLineService::with_repository(journal_line_repository.clone()));
349
350        // Ledger service
351        let ledger_repository = Arc::new(LedgerRepository::new(db_pool.clone()));
352        let ledger_service = Arc::new(LedgerService::with_repository(ledger_repository.clone()));
353
354        // Reconciliation service
355        let reconciliation_repository = Arc::new(ReconciliationRepository::new(db_pool.clone()));
356        let reconciliation_service = Arc::new(ReconciliationService::with_repository(reconciliation_repository.clone()));
357
358        // ReconciliationItem service
359        let reconciliation_item_repository = Arc::new(ReconciliationItemRepository::new(db_pool.clone()));
360        let reconciliation_item_service = Arc::new(ReconciliationItemService::with_repository(reconciliation_item_repository.clone()));
361
362        // FullReconcile service
363        let full_reconcile_repository = Arc::new(FullReconcileRepository::new(db_pool.clone()));
364        let full_reconcile_service = Arc::new(FullReconcileService::with_repository(full_reconcile_repository.clone()));
365
366        // PartialReconcile service
367        let partial_reconcile_repository = Arc::new(PartialReconcileRepository::new(db_pool.clone()));
368        let partial_reconcile_service = Arc::new(PartialReconcileService::with_repository(partial_reconcile_repository.clone()));
369
370
371        // The extension builds its own state from what the generated build made.
372        let ext = self.ext.build(&ModuleParts {
373            db_pool: &db_pool,
374            account_service: &account_service,
375            account_repository: &account_repository,
376            accounting_post_service: &accounting_post_service,
377            accounting_post_repository: &accounting_post_repository,
378            cost_center_service: &cost_center_service,
379            cost_center_repository: &cost_center_repository,
380            emv_qr_config_service: &emv_qr_config_service,
381            emv_qr_config_repository: &emv_qr_config_repository,
382            bank_check_sequence_service: &bank_check_sequence_service,
383            bank_check_sequence_repository: &bank_check_sequence_repository,
384            printed_check_service: &printed_check_service,
385            printed_check_repository: &printed_check_repository,
386            tax_tag_repair_run_service: &tax_tag_repair_run_service,
387            tax_tag_repair_run_repository: &tax_tag_repair_run_repository,
388            financial_statement_service: &financial_statement_service,
389            financial_statement_repository: &financial_statement_repository,
390            fiscal_period_service: &fiscal_period_service,
391            fiscal_period_repository: &fiscal_period_repository,
392            journal_service: &journal_service,
393            journal_repository: &journal_repository,
394            journal_line_service: &journal_line_service,
395            journal_line_repository: &journal_line_repository,
396            ledger_service: &ledger_service,
397            ledger_repository: &ledger_repository,
398            reconciliation_service: &reconciliation_service,
399            reconciliation_repository: &reconciliation_repository,
400            reconciliation_item_service: &reconciliation_item_service,
401            reconciliation_item_repository: &reconciliation_item_repository,
402            full_reconcile_service: &full_reconcile_service,
403            full_reconcile_repository: &full_reconcile_repository,
404            partial_reconcile_service: &partial_reconcile_service,
405            partial_reconcile_repository: &partial_reconcile_repository,
406        })?;
407
408        Ok(AccountingModule {
409            account_service,
410            accounting_post_service,
411            cost_center_service,
412            emv_qr_config_service,
413            bank_check_sequence_service,
414            printed_check_service,
415            tax_tag_repair_run_service,
416            financial_statement_service,
417            fiscal_period_service,
418            journal_service,
419            journal_line_service,
420            ledger_service,
421            reconciliation_service,
422            reconciliation_item_service,
423            full_reconcile_service,
424            partial_reconcile_service,
425            ext,
426        })
427    }
428}
429
430impl Default for AccountingModuleBuilder {
431    fn default() -> Self {
432        Self::new()
433    }
434}