backbone-accounting 0.18.2

GL, journals, ledger, reconciliation (extracted from bersihir)
Documentation
//! Application service for JournalLine entities
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
//! For custom behaviour, create `journal_line_service_custom.rs` (preserved on regen).

use backbone_core::GenericCrudService;

use crate::presentation::dto::{CreateJournalLineDto, UpdateJournalLineDto};
use crate::domain::entity::JournalLine;
use crate::infrastructure::persistence::JournalLineRepository;

/// Application service for JournalLine entities.
///
/// All 12 standard CRUD operations (list, create, get, update, patch,
/// soft_delete, restore, empty_trash, bulk_create, upsert, find_by_id,
/// list_deleted) are provided by `GenericCrudService`.
///
/// Custom extensions go in `journal_line_service_custom.rs`:
///
/// ```rust,ignore
/// pub struct JournalLineServiceCustom {
///     inner: Arc<JournalLineService>,
///     // add domain-specific dependencies here
/// }
/// ```
pub type JournalLineService = GenericCrudService<
    JournalLine,
    CreateJournalLineDto,
    UpdateJournalLineDto,
    JournalLineRepository,
>;

/// The rules the schema declares for JournalLine, checked against the row a create or update
/// would store. The generated build attaches it to [`JournalLineService`]. A refusing rule's
/// break answers 422; a rule in shadow is logged under `backbone::write_guard` and the
/// write goes ahead.
pub struct JournalLineRowRules;

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<JournalLine> for JournalLineRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, JournalLine>,
    ) -> backbone_core::write_guard::GuardOutcome {
        use backbone_core::row_rule as r;
        use backbone_core::violation::Violation;
        use backbone_core::write_guard::{GuardOutcome, WriteKind};
        let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
        let Some(after) = ctx.after.filter(|_| create || update) else {
            return GuardOutcome::allow();
        };
        let Ok(row) = serde_json::to_value(after) else {
            return GuardOutcome::allow();
        };
        let row = &row;
        let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
        let shadow = Vec::new();
        let mut refuse = Vec::new();
        // `journal_id_required` (@required on JournalLine.journal_id)
        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")))))) {
            refuse.push(Violation::new("journal_id", "required", "journal_id is required"));
        }
        // `line_number_positive` (@positive on JournalLine.line_number)
        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))) {
            refuse.push(Violation::new("line_number", "positive", "line_number must be positive"));
        }
        // `account_id_required` (@required on JournalLine.account_id)
        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")))))) {
            refuse.push(Violation::new("account_id", "required", "account_id is required"));
        }
        // `account_number_required` (@required on JournalLine.account_number)
        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")))))) {
            refuse.push(Violation::new("account_number", "required", "account_number is required"));
        }
        // `account_number_max_length` (@max(20) on JournalLine.account_number)
        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))) {
            refuse.push(Violation::new("account_number", "max_length", "account_number must be at most 20 characters").with_param("max", 20_i64));
        }
        // `account_name_required` (@required on JournalLine.account_name)
        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")))))) {
            refuse.push(Violation::new("account_name", "required", "account_name is required"));
        }
        // `account_name_max_length` (@max(100) on JournalLine.account_name)
        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))) {
            refuse.push(Violation::new("account_name", "max_length", "account_name must be at most 100 characters").with_param("max", 100_i64));
        }
        // `debit_amount_non_negative` (@non_negative on JournalLine.debit_amount)
        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))) {
            refuse.push(Violation::new("debit_amount", "non_negative", "debit_amount must not be negative"));
        }
        // `credit_amount_non_negative` (@non_negative on JournalLine.credit_amount)
        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))) {
            refuse.push(Violation::new("credit_amount", "non_negative", "credit_amount must not be negative"));
        }
        // `currency_length` (@length(3) on JournalLine.currency)
        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))) {
            refuse.push(Violation::new("currency", "length", "currency must be exactly 3 characters").with_param("length", 3_i64));
        }
        // `exchange_rate_positive` (@positive on JournalLine.exchange_rate)
        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))) {
            refuse.push(Violation::new("exchange_rate", "positive", "exchange_rate must be positive"));
        }
        // `base_debit_amount_non_negative` (@non_negative on JournalLine.base_debit_amount)
        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))) {
            refuse.push(Violation::new("base_debit_amount", "non_negative", "base_debit_amount must not be negative"));
        }
        // `base_credit_amount_non_negative` (@non_negative on JournalLine.base_credit_amount)
        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))) {
            refuse.push(Violation::new("base_credit_amount", "non_negative", "base_credit_amount must not be negative"));
        }
        // `description_max_length` (@max(255) on JournalLine.description)
        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))) {
            refuse.push(Violation::new("description", "max_length", "description must be at most 255 characters").with_param("max", 255_i64));
        }
        // `source_type_max_length` (@max(50) on JournalLine.source_type)
        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))) {
            refuse.push(Violation::new("source_type", "max_length", "source_type must be at most 50 characters").with_param("max", 50_i64));
        }
        // `source_reference_max_length` (@max(100) on JournalLine.source_reference)
        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))) {
            refuse.push(Violation::new("source_reference", "max_length", "source_reference must be at most 100 characters").with_param("max", 100_i64));
        }
        // `tax_rate_range` (@range(0, 100) on JournalLine.tax_rate)
        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)))) {
            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));
        }
        // `tax_base_amount_non_negative` (@non_negative on JournalLine.tax_base_amount)
        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))) {
            refuse.push(Violation::new("tax_base_amount", "non_negative", "tax_base_amount must not be negative"));
        }
        // `unit_max_length` (@max(20) on JournalLine.unit)
        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))) {
            refuse.push(Violation::new("unit", "max_length", "unit must be at most 20 characters").with_param("max", 20_i64));
        }
        GuardOutcome { refuse, shadow }
    }
}