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 Bin entities
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
//! For custom behaviour, create `bin_service_custom.rs` (preserved on regen).

use backbone_core::GenericCrudService;

use crate::presentation::dto::{CreateBinDto, UpdateBinDto};
use crate::domain::entity::Bin;
use crate::infrastructure::persistence::BinRepository;

/// Application service for Bin 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 `bin_service_custom.rs`:
///
/// ```rust,ignore
/// pub struct BinServiceCustom {
///     inner: Arc<BinService>,
///     // add domain-specific dependencies here
/// }
/// ```
pub type BinService = GenericCrudService<
    Bin,
    CreateBinDto,
    UpdateBinDto,
    BinRepository,
>;

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

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<Bin> for BinRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, Bin>,
    ) -> 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();
        // `item_id_required` (@required on Bin.item_id)
        if (create || (update && r::changed(old.as_ref(), row, &["item_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "item_id")), r::not(r::is_empty(r::trim(r::field(row, "item_id")))))) {
            refuse.push(Violation::new("item_id", "required", "item_id is required"));
        }
        // `warehouse_id_required` (@required on Bin.warehouse_id)
        if (create || (update && r::changed(old.as_ref(), row, &["warehouse_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "warehouse_id")), r::not(r::is_empty(r::trim(r::field(row, "warehouse_id")))))) {
            refuse.push(Violation::new("warehouse_id", "required", "warehouse_id is required"));
        }
        // `reserved_qty_non_negative` (@non_negative on Bin.reserved_qty)
        if (create || (update && r::changed(old.as_ref(), row, &["reserved_qty"]))) && !r::holds(&r::ge(r::field(row, "reserved_qty"), r::Val::Num(0.0_f64))) {
            refuse.push(Violation::new("reserved_qty", "non_negative", "reserved_qty must not be negative"));
        }
        // `valuation_rate_non_negative` (@non_negative on Bin.valuation_rate)
        if (create || (update && r::changed(old.as_ref(), row, &["valuation_rate"]))) && !r::holds(&r::ge(r::field(row, "valuation_rate"), r::Val::Num(0.0_f64))) {
            refuse.push(Violation::new("valuation_rate", "non_negative", "valuation_rate must not be negative"));
        }
        GuardOutcome { refuse, shadow }
    }
}