Skip to main content

backbone_accounting/application/service/
journal_service.rs

1//! Application service for Journal entities
2//!
3//! Generated by metaphor-schema. Do not edit manually.
4//!
5//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
6//! For custom behaviour, create `journal_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateJournalDto, UpdateJournalDto};
11use crate::domain::entity::Journal;
12use crate::infrastructure::persistence::JournalRepository;
13
14/// Application service for Journal entities.
15///
16/// All 12 standard CRUD operations (list, create, get, update, patch,
17/// soft_delete, restore, empty_trash, bulk_create, upsert, find_by_id,
18/// list_deleted) are provided by `GenericCrudService`.
19///
20/// Custom extensions go in `journal_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct JournalServiceCustom {
24///     inner: Arc<JournalService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type JournalService = GenericCrudService<
29    Journal,
30    CreateJournalDto,
31    UpdateJournalDto,
32    JournalRepository,
33>;
34
35/// The rules the schema declares for Journal, checked against the row a create or update
36/// would store. The generated build attaches it to [`JournalService`]. A refusing rule's
37/// break answers 422; a rule in shadow is logged under `backbone::write_guard` and the
38/// write goes ahead.
39pub struct JournalRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<Journal> for JournalRowRules {
43    async fn check(
44        &self,
45        ctx: &backbone_core::write_guard::WriteCtx<'_, Journal>,
46    ) -> backbone_core::write_guard::GuardOutcome {
47        use backbone_core::row_rule as r;
48        use backbone_core::violation::Violation;
49        use backbone_core::write_guard::{GuardOutcome, WriteKind};
50        let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
51        let Some(after) = ctx.after.filter(|_| create || update) else {
52            return GuardOutcome::allow();
53        };
54        let Ok(row) = serde_json::to_value(after) else {
55            return GuardOutcome::allow();
56        };
57        let row = &row;
58        let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
59        let shadow = Vec::new();
60        let mut refuse = Vec::new();
61        // `status` belongs to a state machine: only its verbs move it
62        if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
63            refuse.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
64        }
65        // `journal_number_required` (@required on Journal.journal_number)
66        if (create || (update && r::changed(old.as_ref(), row, &["journal_number"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "journal_number")), r::not(r::is_empty(r::trim(r::field(row, "journal_number")))))) {
67            refuse.push(Violation::new("journal_number", "required", "journal_number is required"));
68        }
69        // `journal_number_max_length` (@max(30) on Journal.journal_number)
70        if (create || (update && r::changed(old.as_ref(), row, &["journal_number"]))) && !r::holds(&r::le(r::length(r::field(row, "journal_number")), r::Val::Num(30.0_f64))) {
71            refuse.push(Violation::new("journal_number", "max_length", "journal_number must be at most 30 characters").with_param("max", 30_i64));
72        }
73        // `reference_number_max_length` (@max(50) on Journal.reference_number)
74        if (create || (update && r::changed(old.as_ref(), row, &["reference_number"]))) && !r::holds(&r::le(r::length(r::field(row, "reference_number")), r::Val::Num(50.0_f64))) {
75            refuse.push(Violation::new("reference_number", "max_length", "reference_number must be at most 50 characters").with_param("max", 50_i64));
76        }
77        // `fiscal_year_min` (@min(2020) on Journal.fiscal_year)
78        if (create || (update && r::changed(old.as_ref(), row, &["fiscal_year"]))) && !r::holds(&r::ge(r::field(row, "fiscal_year"), r::Val::Num(2020.0_f64))) {
79            refuse.push(Violation::new("fiscal_year", "min", "fiscal_year must be at least 2020").with_param("min", 2020_i64));
80        }
81        // `fiscal_year_max` (@max(2100) on Journal.fiscal_year)
82        if (create || (update && r::changed(old.as_ref(), row, &["fiscal_year"]))) && !r::holds(&r::le(r::field(row, "fiscal_year"), r::Val::Num(2100.0_f64))) {
83            refuse.push(Violation::new("fiscal_year", "max", "fiscal_year must be at most 2100").with_param("max", 2100_i64));
84        }
85        // `fiscal_month_min` (@min(1) on Journal.fiscal_month)
86        if (create || (update && r::changed(old.as_ref(), row, &["fiscal_month"]))) && !r::holds(&r::ge(r::field(row, "fiscal_month"), r::Val::Num(1.0_f64))) {
87            refuse.push(Violation::new("fiscal_month", "min", "fiscal_month must be at least 1").with_param("min", 1_i64));
88        }
89        // `fiscal_month_max` (@max(12) on Journal.fiscal_month)
90        if (create || (update && r::changed(old.as_ref(), row, &["fiscal_month"]))) && !r::holds(&r::le(r::field(row, "fiscal_month"), r::Val::Num(12.0_f64))) {
91            refuse.push(Violation::new("fiscal_month", "max", "fiscal_month must be at most 12").with_param("max", 12_i64));
92        }
93        // `description_required` (@required on Journal.description)
94        if (create || (update && r::changed(old.as_ref(), row, &["description"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "description")), r::not(r::is_empty(r::trim(r::field(row, "description")))))) {
95            refuse.push(Violation::new("description", "required", "description is required"));
96        }
97        // `description_max_length` (@max(500) on Journal.description)
98        if (create || (update && r::changed(old.as_ref(), row, &["description"]))) && !r::holds(&r::le(r::length(r::field(row, "description")), r::Val::Num(500.0_f64))) {
99            refuse.push(Violation::new("description", "max_length", "description must be at most 500 characters").with_param("max", 500_i64));
100        }
101        // `notes_max_length` (@max(1000) on Journal.notes)
102        if (create || (update && r::changed(old.as_ref(), row, &["notes"]))) && !r::holds(&r::le(r::length(r::field(row, "notes")), r::Val::Num(1000.0_f64))) {
103            refuse.push(Violation::new("notes", "max_length", "notes must be at most 1000 characters").with_param("max", 1000_i64));
104        }
105        // `currency_length` (@length(3) on Journal.currency)
106        if (create || (update && r::changed(old.as_ref(), row, &["currency"]))) && !r::holds(&r::eq(r::length(r::field(row, "currency")), r::Val::Num(3.0_f64))) {
107            refuse.push(Violation::new("currency", "length", "currency must be exactly 3 characters").with_param("length", 3_i64));
108        }
109        // `total_debit_non_negative` (@non_negative on Journal.total_debit)
110        if (create || (update && r::changed(old.as_ref(), row, &["total_debit"]))) && !r::holds(&r::ge(r::field(row, "total_debit"), r::Val::Num(0.0_f64))) {
111            refuse.push(Violation::new("total_debit", "non_negative", "total_debit must not be negative"));
112        }
113        // `total_credit_non_negative` (@non_negative on Journal.total_credit)
114        if (create || (update && r::changed(old.as_ref(), row, &["total_credit"]))) && !r::holds(&r::ge(r::field(row, "total_credit"), r::Val::Num(0.0_f64))) {
115            refuse.push(Violation::new("total_credit", "non_negative", "total_credit must not be negative"));
116        }
117        // `line_count_non_negative` (@non_negative on Journal.line_count)
118        if (create || (update && r::changed(old.as_ref(), row, &["line_count"]))) && !r::holds(&r::ge(r::field(row, "line_count"), r::Val::Num(0.0_f64))) {
119            refuse.push(Violation::new("line_count", "non_negative", "line_count must not be negative"));
120        }
121        // `source_type_max_length` (@max(50) on Journal.source_type)
122        if (create || (update && r::changed(old.as_ref(), row, &["source_type"]))) && !r::holds(&r::le(r::length(r::field(row, "source_type")), r::Val::Num(50.0_f64))) {
123            refuse.push(Violation::new("source_type", "max_length", "source_type must be at most 50 characters").with_param("max", 50_i64));
124        }
125        // `source_reference_max_length` (@max(255) on Journal.source_reference)
126        if (create || (update && r::changed(old.as_ref(), row, &["source_reference"]))) && !r::holds(&r::le(r::length(r::field(row, "source_reference")), r::Val::Num(255.0_f64))) {
127            refuse.push(Violation::new("source_reference", "max_length", "source_reference must be at most 255 characters").with_param("max", 255_i64));
128        }
129        // `reversal_reason_max_length` (@max(255) on Journal.reversal_reason)
130        if (create || (update && r::changed(old.as_ref(), row, &["reversal_reason"]))) && !r::holds(&r::le(r::length(r::field(row, "reversal_reason")), r::Val::Num(255.0_f64))) {
131            refuse.push(Violation::new("reversal_reason", "max_length", "reversal_reason must be at most 255 characters").with_param("max", 255_i64));
132        }
133        // `rejection_reason_max_length` (@max(500) on Journal.rejection_reason)
134        if (create || (update && r::changed(old.as_ref(), row, &["rejection_reason"]))) && !r::holds(&r::le(r::length(r::field(row, "rejection_reason")), r::Val::Num(500.0_f64))) {
135            refuse.push(Violation::new("rejection_reason", "max_length", "rejection_reason must be at most 500 characters").with_param("max", 500_i64));
136        }
137        // `void_reason_max_length` (@max(500) on Journal.void_reason)
138        if (create || (update && r::changed(old.as_ref(), row, &["void_reason"]))) && !r::holds(&r::le(r::length(r::field(row, "void_reason")), r::Val::Num(500.0_f64))) {
139            refuse.push(Violation::new("void_reason", "max_length", "void_reason must be at most 500 characters").with_param("max", 500_i64));
140        }
141        GuardOutcome { refuse, shadow }
142    }
143}
144