backbone-catalog 0.11.0

Canonical product/service identity: Item, Item Group, UOM (Indonesia-first)
Documentation
//! Application service for Item entities
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
//! For custom behaviour, create `item_service_custom.rs` (preserved on regen).

use backbone_core::GenericCrudService;

use crate::presentation::dto::{CreateItemDto, UpdateItemDto};
use crate::domain::entity::Item;
use crate::infrastructure::persistence::ItemRepository;

/// Application service for Item 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 `item_service_custom.rs`:
///
/// ```rust,ignore
/// pub struct ItemServiceCustom {
///     inner: Arc<ItemService>,
///     // add domain-specific dependencies here
/// }
/// ```
pub type ItemService = GenericCrudService<
    Item,
    CreateItemDto,
    UpdateItemDto,
    ItemRepository,
>;

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

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<Item> for ItemRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, Item>,
    ) -> 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 mut shadow = Vec::new();
        let mut refuse = Vec::new();
        // `status` belongs to a state machine: only its verbs move it
        if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
            shadow.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
        }
        // `item_code_required` (@required on Item.item_code)
        if (create || (update && r::changed(old.as_ref(), row, &["item_code"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "item_code")), r::not(r::is_empty(r::trim(r::field(row, "item_code")))))) {
            refuse.push(Violation::new("item_code", "required", "item_code is required"));
        }
        // `item_code_max_length` (@max(60) on Item.item_code)
        if (create || (update && r::changed(old.as_ref(), row, &["item_code"]))) && !r::holds(&r::le(r::length(r::field(row, "item_code")), r::Val::Num(60.0_f64))) {
            refuse.push(Violation::new("item_code", "max_length", "item_code must be at most 60 characters").with_param("max", 60_i64));
        }
        // `name_required` (@required on Item.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(200) on Item.name)
        if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(200.0_f64))) {
            refuse.push(Violation::new("name", "max_length", "name must be at most 200 characters").with_param("max", 200_i64));
        }
        // `description_max_length` (@max(2000) on Item.description)
        if (create || (update && r::changed(old.as_ref(), row, &["description"]))) && !r::holds(&r::le(r::length(r::field(row, "description")), r::Val::Num(2000.0_f64))) {
            refuse.push(Violation::new("description", "max_length", "description must be at most 2000 characters").with_param("max", 2000_i64));
        }
        // `barcode_max_length` (@max(60) on Item.barcode)
        if (create || (update && r::changed(old.as_ref(), row, &["barcode"]))) && !r::holds(&r::le(r::length(r::field(row, "barcode")), r::Val::Num(60.0_f64))) {
            refuse.push(Violation::new("barcode", "max_length", "barcode must be at most 60 characters").with_param("max", 60_i64));
        }
        // `item_group_id_required` (@required on Item.item_group_id)
        if (create || (update && r::changed(old.as_ref(), row, &["item_group_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "item_group_id")), r::not(r::is_empty(r::trim(r::field(row, "item_group_id")))))) {
            refuse.push(Violation::new("item_group_id", "required", "item_group_id is required"));
        }
        // `default_uom_id_required` (@required on Item.default_uom_id)
        if (create || (update && r::changed(old.as_ref(), row, &["default_uom_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "default_uom_id")), r::not(r::is_empty(r::trim(r::field(row, "default_uom_id")))))) {
            refuse.push(Violation::new("default_uom_id", "required", "default_uom_id is required"));
        }
        // `hsn_code_max_length` (@max(20) on Item.hsn_code)
        if (create || (update && r::changed(old.as_ref(), row, &["hsn_code"]))) && !r::holds(&r::le(r::length(r::field(row, "hsn_code")), r::Val::Num(20.0_f64))) {
            refuse.push(Violation::new("hsn_code", "max_length", "hsn_code must be at most 20 characters").with_param("max", 20_i64));
        }
        // `sni_max_length` (@max(30) on Item.sni)
        if (create || (update && r::changed(old.as_ref(), row, &["sni"]))) && !r::holds(&r::le(r::length(r::field(row, "sni")), r::Val::Num(30.0_f64))) {
            refuse.push(Violation::new("sni", "max_length", "sni must be at most 30 characters").with_param("max", 30_i64));
        }
        // `shelf_life_days_non_negative` (@non_negative on Item.shelf_life_days)
        if (create || (update && r::changed(old.as_ref(), row, &["shelf_life_days"]))) && !r::holds(&r::ge(r::field(row, "shelf_life_days"), r::Val::Num(0.0_f64))) {
            refuse.push(Violation::new("shelf_life_days", "non_negative", "shelf_life_days must not be negative"));
        }
        // `standard_cost_non_negative` (@non_negative on Item.standard_cost)
        if (create || (update && r::changed(old.as_ref(), row, &["standard_cost"]))) && !r::holds(&r::ge(r::field(row, "standard_cost"), r::Val::Num(0.0_f64))) {
            refuse.push(Violation::new("standard_cost", "non_negative", "standard_cost must not be negative"));
        }
        GuardOutcome { refuse, shadow }
    }
}