backbone-inventory 0.17.2

Inventory: stock ledger of record (SLE) + moving-average valuation; emits COGS/asset AccountingPost (Delivery/Receipt -> GL)
Documentation
//! Application service for LandedCostLine entities
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
//! For custom behaviour, create `landed_cost_line_service_custom.rs` (preserved on regen).

use backbone_core::GenericCrudService;

use crate::presentation::dto::{CreateLandedCostLineDto, UpdateLandedCostLineDto};
use crate::domain::entity::LandedCostLine;
use crate::infrastructure::persistence::LandedCostLineRepository;

/// Application service for LandedCostLine 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 `landed_cost_line_service_custom.rs`:
///
/// ```rust,ignore
/// pub struct LandedCostLineServiceCustom {
///     inner: Arc<LandedCostLineService>,
///     // add domain-specific dependencies here
/// }
/// ```
pub type LandedCostLineService = GenericCrudService<
    LandedCostLine,
    CreateLandedCostLineDto,
    UpdateLandedCostLineDto,
    LandedCostLineRepository,
>;

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

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<LandedCostLine> for LandedCostLineRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, LandedCostLine>,
    ) -> 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();
        // `lc_id_required` (@required on LandedCostLine.lc_id)
        if (create || (update && r::changed(old.as_ref(), row, &["lc_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "lc_id")), r::not(r::is_empty(r::trim(r::field(row, "lc_id")))))) {
            refuse.push(Violation::new("lc_id", "required", "lc_id is required"));
        }
        // `name_required` (@required on LandedCostLine.name)
        if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "name")), r::not(r::is_empty(r::trim(r::field(row, "name")))))) {
            refuse.push(Violation::new("name", "required", "name is required"));
        }
        // `name_max_length` (@max(150) on LandedCostLine.name)
        if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(150.0_f64))) {
            refuse.push(Violation::new("name", "max_length", "name must be at most 150 characters").with_param("max", 150_i64));
        }
        // `account_id_required` (@required on LandedCostLine.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"));
        }
        GuardOutcome { refuse, shadow }
    }
}