Skip to main content

backbone_inventory/application/service/
delivery_note_service.rs

1//! Application service for DeliveryNote entities
2//!
3//! Generated by metaphor-schema. Do not edit manually.
4//!
5//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
6//! For custom behaviour, create `delivery_note_service_custom.rs` (preserved on regen).
7
8use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateDeliveryNoteDto, UpdateDeliveryNoteDto};
11use crate::domain::entity::DeliveryNote;
12use crate::infrastructure::persistence::DeliveryNoteRepository;
13
14/// Application service for DeliveryNote entities.
15///
16/// All 12 standard CRUD operations (list, create, get, update, patch,
17/// soft_delete, restore, empty_trash, bulk_create, upsert, find_by_id,
18/// list_deleted) are provided by `GenericCrudService`.
19///
20/// Custom extensions go in `delivery_note_service_custom.rs`:
21///
22/// ```rust,ignore
23/// pub struct DeliveryNoteServiceCustom {
24///     inner: Arc<DeliveryNoteService>,
25///     // add domain-specific dependencies here
26/// }
27/// ```
28pub type DeliveryNoteService = GenericCrudService<
29    DeliveryNote,
30    CreateDeliveryNoteDto,
31    UpdateDeliveryNoteDto,
32    DeliveryNoteRepository,
33>;
34
35/// The rules the schema declares for DeliveryNote, checked against the row a create or update
36/// would store. The generated build attaches it to [`DeliveryNoteService`]. A refusing rule's
37/// break answers 422; a rule in shadow is logged under `backbone::write_guard` and the
38/// write goes ahead.
39pub struct DeliveryNoteRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<DeliveryNote> for DeliveryNoteRowRules {
43    async fn check(
44        &self,
45        ctx: &backbone_core::write_guard::WriteCtx<'_, DeliveryNote>,
46    ) -> backbone_core::write_guard::GuardOutcome {
47        use backbone_core::row_rule as r;
48        use backbone_core::violation::Violation;
49        use backbone_core::write_guard::{GuardOutcome, WriteKind};
50        let (create, update) = (ctx.kind == WriteKind::Create, ctx.kind == WriteKind::Update);
51        let Some(after) = ctx.after.filter(|_| create || update) else {
52            return GuardOutcome::allow();
53        };
54        let Ok(row) = serde_json::to_value(after) else {
55            return GuardOutcome::allow();
56        };
57        let row = &row;
58        let old = ctx.before.and_then(|b| serde_json::to_value(b).ok());
59        let shadow = Vec::new();
60        let mut refuse = Vec::new();
61        // `status` belongs to a state machine: only its verbs move it
62        if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["status"]) {
63            refuse.push(Violation::new("status", "state_field_verbs_only", "the state field moves only through its verbs"));
64        }
65        // `delivery_number_required` (@required on DeliveryNote.delivery_number)
66        if (create || (update && r::changed(old.as_ref(), row, &["delivery_number"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "delivery_number")), r::not(r::is_empty(r::trim(r::field(row, "delivery_number")))))) {
67            refuse.push(Violation::new("delivery_number", "required", "delivery_number is required"));
68        }
69        // `delivery_number_max_length` (@max(40) on DeliveryNote.delivery_number)
70        if (create || (update && r::changed(old.as_ref(), row, &["delivery_number"]))) && !r::holds(&r::le(r::length(r::field(row, "delivery_number")), r::Val::Num(40.0_f64))) {
71            refuse.push(Violation::new("delivery_number", "max_length", "delivery_number must be at most 40 characters").with_param("max", 40_i64));
72        }
73        // `customer_id_required` (@required on DeliveryNote.customer_id)
74        if (create || (update && r::changed(old.as_ref(), row, &["customer_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "customer_id")), r::not(r::is_empty(r::trim(r::field(row, "customer_id")))))) {
75            refuse.push(Violation::new("customer_id", "required", "customer_id is required"));
76        }
77        // `warehouse_id_required` (@required on DeliveryNote.warehouse_id)
78        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")))))) {
79            refuse.push(Violation::new("warehouse_id", "required", "warehouse_id is required"));
80        }
81        // `total_cogs_non_negative` (@non_negative on DeliveryNote.total_cogs)
82        if (create || (update && r::changed(old.as_ref(), row, &["total_cogs"]))) && !r::holds(&r::ge(r::field(row, "total_cogs"), r::Val::Num(0.0_f64))) {
83            refuse.push(Violation::new("total_cogs", "non_negative", "total_cogs must not be negative"));
84        }
85        // `cogs_account_id_required` (@required on DeliveryNote.cogs_account_id)
86        if (create || (update && r::changed(old.as_ref(), row, &["cogs_account_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "cogs_account_id")), r::not(r::is_empty(r::trim(r::field(row, "cogs_account_id")))))) {
87            refuse.push(Violation::new("cogs_account_id", "required", "cogs_account_id is required"));
88        }
89        // `inventory_account_id_required` (@required on DeliveryNote.inventory_account_id)
90        if (create || (update && r::changed(old.as_ref(), row, &["inventory_account_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "inventory_account_id")), r::not(r::is_empty(r::trim(r::field(row, "inventory_account_id")))))) {
91            refuse.push(Violation::new("inventory_account_id", "required", "inventory_account_id is required"));
92        }
93        // `notes_max_length` (@max(1000) on DeliveryNote.notes)
94        if (create || (update && r::changed(old.as_ref(), row, &["notes"]))) && !r::holds(&r::le(r::length(r::field(row, "notes")), r::Val::Num(1000.0_f64))) {
95            refuse.push(Violation::new("notes", "max_length", "notes must be at most 1000 characters").with_param("max", 1000_i64));
96        }
97        GuardOutcome { refuse, shadow }
98    }
99}
100