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