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

use backbone_core::GenericCrudService;

use crate::presentation::dto::{CreateStockMoveDto, UpdateStockMoveDto};
use crate::domain::entity::StockMove;
use crate::infrastructure::persistence::StockMoveRepository;

/// Application service for StockMove 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 `stock_move_service_custom.rs`:
///
/// ```rust,ignore
/// pub struct StockMoveServiceCustom {
///     inner: Arc<StockMoveService>,
///     // add domain-specific dependencies here
/// }
/// ```
pub type StockMoveService = GenericCrudService<
    StockMove,
    CreateStockMoveDto,
    UpdateStockMoveDto,
    StockMoveRepository,
>;

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

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<StockMove> for StockMoveRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, StockMove>,
    ) -> 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 old_row = old.as_ref().unwrap_or(&serde_json::Value::Null);
        let shadow = Vec::new();
        let mut refuse = Vec::new();
        // `state` belongs to a state machine: only its verbs move it
        if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["state"]) {
            refuse.push(Violation::new("state", "state_field_verbs_only", "the state field moves only through its verbs"));
        }
        // `name_required` (@required on StockMove.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(400) on StockMove.name)
        if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(400.0_f64))) {
            refuse.push(Violation::new("name", "max_length", "name must be at most 400 characters").with_param("max", 400_i64));
        }
        // `item_id_required` (@required on StockMove.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"));
        }
        // `demand_qty_non_negative` (@non_negative on StockMove.demand_qty)
        if (create || (update && r::changed(old.as_ref(), row, &["demand_qty"]))) && !r::holds(&r::ge(r::field(row, "demand_qty"), r::Val::Num(0.0_f64))) {
            refuse.push(Violation::new("demand_qty", "non_negative", "demand_qty must not be negative"));
        }
        // `quantity_non_negative` (@non_negative on StockMove.quantity)
        if (create || (update && r::changed(old.as_ref(), row, &["quantity"]))) && !r::holds(&r::ge(r::field(row, "quantity"), r::Val::Num(0.0_f64))) {
            refuse.push(Violation::new("quantity", "non_negative", "quantity must not be negative"));
        }
        // `origin_max_length` (@max(200) on StockMove.origin)
        if (create || (update && r::changed(old.as_ref(), row, &["origin"]))) && !r::holds(&r::le(r::length(r::field(row, "origin")), r::Val::Num(200.0_f64))) {
            refuse.push(Violation::new("origin", "max_length", "origin must be at most 200 characters").with_param("max", 200_i64));
        }
        // `location_id_required` (@required on StockMove.location_id)
        if (create || (update && r::changed(old.as_ref(), row, &["location_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "location_id")), r::not(r::is_empty(r::trim(r::field(row, "location_id")))))) {
            refuse.push(Violation::new("location_id", "required", "location_id is required"));
        }
        // `location_dest_id_required` (@required on StockMove.location_dest_id)
        if (create || (update && r::changed(old.as_ref(), row, &["location_dest_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "location_dest_id")), r::not(r::is_empty(r::trim(r::field(row, "location_dest_id")))))) {
            refuse.push(Violation::new("location_dest_id", "required", "location_dest_id is required"));
        }
        // `done_moves_immutable` (stock.hook.yaml:119)
        if (update) && !r::holds(&r::ne(r::field(old_row, "state"), r::text("done"))) {
            refuse.push(Violation::new("state", "done_move_immutable", "breaks the rule `done_moves_immutable`: $old.state != 'done'"));
        }
        GuardOutcome { refuse, shadow }
    }
}