Skip to main content

backbone_accounting/application/service/
journal_line_service.rs

1//! Application service for JournalLine 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_line_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateJournalLineDto, UpdateJournalLineDto};
11use crate::domain::entity::JournalLine;
12use crate::infrastructure::persistence::JournalLineRepository;
13
14/// Application service for JournalLine 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_line_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct JournalLineServiceCustom {
24///     inner: Arc<JournalLineService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type JournalLineService = GenericCrudService<
29    JournalLine,
30    CreateJournalLineDto,
31    UpdateJournalLineDto,
32    JournalLineRepository,
33>;
34
35/// The rules the schema declares for JournalLine, checked against the row a create or update
36/// would store. The generated build attaches it to [`JournalLineService`]. 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 JournalLineRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<JournalLine> for JournalLineRowRules {
43    async fn check(
44        &self,
45        ctx: &backbone_core::write_guard::WriteCtx<'_, JournalLine>,
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        // `journal_id_required` (@required on JournalLine.journal_id)
62        if (create || (update && r::changed(old.as_ref(), row, &["journal_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "journal_id")), r::not(r::is_empty(r::trim(r::field(row, "journal_id")))))) {
63            refuse.push(Violation::new("journal_id", "required", "journal_id is required"));
64        }
65        // `line_number_positive` (@positive on JournalLine.line_number)
66        if (create || (update && r::changed(old.as_ref(), row, &["line_number"]))) && !r::holds(&r::gt(r::field(row, "line_number"), r::Val::Num(0.0_f64))) {
67            refuse.push(Violation::new("line_number", "positive", "line_number must be positive"));
68        }
69        // `account_id_required` (@required on JournalLine.account_id)
70        if (create || (update && r::changed(old.as_ref(), row, &["account_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "account_id")), r::not(r::is_empty(r::trim(r::field(row, "account_id")))))) {
71            refuse.push(Violation::new("account_id", "required", "account_id is required"));
72        }
73        // `account_number_required` (@required on JournalLine.account_number)
74        if (create || (update && r::changed(old.as_ref(), row, &["account_number"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "account_number")), r::not(r::is_empty(r::trim(r::field(row, "account_number")))))) {
75            refuse.push(Violation::new("account_number", "required", "account_number is required"));
76        }
77        // `account_number_max_length` (@max(20) on JournalLine.account_number)
78        if (create || (update && r::changed(old.as_ref(), row, &["account_number"]))) && !r::holds(&r::le(r::length(r::field(row, "account_number")), r::Val::Num(20.0_f64))) {
79            refuse.push(Violation::new("account_number", "max_length", "account_number must be at most 20 characters").with_param("max", 20_i64));
80        }
81        // `account_name_required` (@required on JournalLine.account_name)
82        if (create || (update && r::changed(old.as_ref(), row, &["account_name"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "account_name")), r::not(r::is_empty(r::trim(r::field(row, "account_name")))))) {
83            refuse.push(Violation::new("account_name", "required", "account_name is required"));
84        }
85        // `account_name_max_length` (@max(100) on JournalLine.account_name)
86        if (create || (update && r::changed(old.as_ref(), row, &["account_name"]))) && !r::holds(&r::le(r::length(r::field(row, "account_name")), r::Val::Num(100.0_f64))) {
87            refuse.push(Violation::new("account_name", "max_length", "account_name must be at most 100 characters").with_param("max", 100_i64));
88        }
89        // `debit_amount_non_negative` (@non_negative on JournalLine.debit_amount)
90        if (create || (update && r::changed(old.as_ref(), row, &["debit_amount"]))) && !r::holds(&r::ge(r::field(row, "debit_amount"), r::Val::Num(0.0_f64))) {
91            refuse.push(Violation::new("debit_amount", "non_negative", "debit_amount must not be negative"));
92        }
93        // `credit_amount_non_negative` (@non_negative on JournalLine.credit_amount)
94        if (create || (update && r::changed(old.as_ref(), row, &["credit_amount"]))) && !r::holds(&r::ge(r::field(row, "credit_amount"), r::Val::Num(0.0_f64))) {
95            refuse.push(Violation::new("credit_amount", "non_negative", "credit_amount must not be negative"));
96        }
97        // `currency_length` (@length(3) on JournalLine.currency)
98        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))) {
99            refuse.push(Violation::new("currency", "length", "currency must be exactly 3 characters").with_param("length", 3_i64));
100        }
101        // `exchange_rate_positive` (@positive on JournalLine.exchange_rate)
102        if (create || (update && r::changed(old.as_ref(), row, &["exchange_rate"]))) && !r::holds(&r::gt(r::field(row, "exchange_rate"), r::Val::Num(0.0_f64))) {
103            refuse.push(Violation::new("exchange_rate", "positive", "exchange_rate must be positive"));
104        }
105        // `base_debit_amount_non_negative` (@non_negative on JournalLine.base_debit_amount)
106        if (create || (update && r::changed(old.as_ref(), row, &["base_debit_amount"]))) && !r::holds(&r::ge(r::field(row, "base_debit_amount"), r::Val::Num(0.0_f64))) {
107            refuse.push(Violation::new("base_debit_amount", "non_negative", "base_debit_amount must not be negative"));
108        }
109        // `base_credit_amount_non_negative` (@non_negative on JournalLine.base_credit_amount)
110        if (create || (update && r::changed(old.as_ref(), row, &["base_credit_amount"]))) && !r::holds(&r::ge(r::field(row, "base_credit_amount"), r::Val::Num(0.0_f64))) {
111            refuse.push(Violation::new("base_credit_amount", "non_negative", "base_credit_amount must not be negative"));
112        }
113        // `description_max_length` (@max(255) on JournalLine.description)
114        if (create || (update && r::changed(old.as_ref(), row, &["description"]))) && !r::holds(&r::le(r::length(r::field(row, "description")), r::Val::Num(255.0_f64))) {
115            refuse.push(Violation::new("description", "max_length", "description must be at most 255 characters").with_param("max", 255_i64));
116        }
117        // `source_type_max_length` (@max(50) on JournalLine.source_type)
118        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))) {
119            refuse.push(Violation::new("source_type", "max_length", "source_type must be at most 50 characters").with_param("max", 50_i64));
120        }
121        // `source_reference_max_length` (@max(100) on JournalLine.source_reference)
122        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(100.0_f64))) {
123            refuse.push(Violation::new("source_reference", "max_length", "source_reference must be at most 100 characters").with_param("max", 100_i64));
124        }
125        // `tax_rate_range` (@range(0, 100) on JournalLine.tax_rate)
126        if (create || (update && r::changed(old.as_ref(), row, &["tax_rate"]))) && !r::holds(&r::and(r::ge(r::field(row, "tax_rate"), r::Val::Num(0.0_f64)), r::le(r::field(row, "tax_rate"), r::Val::Num(100.0_f64)))) {
127            refuse.push(Violation::new("tax_rate", "range", "tax_rate must be between 0 and 100").with_param("min", 0_i64).with_param("max", 100_i64));
128        }
129        // `tax_base_amount_non_negative` (@non_negative on JournalLine.tax_base_amount)
130        if (create || (update && r::changed(old.as_ref(), row, &["tax_base_amount"]))) && !r::holds(&r::ge(r::field(row, "tax_base_amount"), r::Val::Num(0.0_f64))) {
131            refuse.push(Violation::new("tax_base_amount", "non_negative", "tax_base_amount must not be negative"));
132        }
133        // `unit_max_length` (@max(20) on JournalLine.unit)
134        if (create || (update && r::changed(old.as_ref(), row, &["unit"]))) && !r::holds(&r::le(r::length(r::field(row, "unit")), r::Val::Num(20.0_f64))) {
135            refuse.push(Violation::new("unit", "max_length", "unit must be at most 20 characters").with_param("max", 20_i64));
136        }
137        GuardOutcome { refuse, shadow }
138    }
139}
140