backbone_inventory/application/service/
delivery_note_item_service.rs1use backbone_core::GenericCrudService;
9
10use crate::presentation::dto::{CreateDeliveryNoteItemDto, UpdateDeliveryNoteItemDto};
11use crate::domain::entity::DeliveryNoteItem;
12use crate::infrastructure::persistence::DeliveryNoteItemRepository;
13
14pub type DeliveryNoteItemService = GenericCrudService<
29 DeliveryNoteItem,
30 CreateDeliveryNoteItemDto,
31 UpdateDeliveryNoteItemDto,
32 DeliveryNoteItemRepository,
33>;
34
35pub struct DeliveryNoteItemRowRules;
40
41#[async_trait::async_trait]
42impl backbone_core::write_guard::WriteGuard<DeliveryNoteItem> for DeliveryNoteItemRowRules {
43 async fn check(
44 &self,
45 ctx: &backbone_core::write_guard::WriteCtx<'_, DeliveryNoteItem>,
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 if (create || (update && r::changed(old.as_ref(), row, &["delivery_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "delivery_id")), r::not(r::is_empty(r::trim(r::field(row, "delivery_id")))))) {
63 refuse.push(Violation::new("delivery_id", "required", "delivery_id is required"));
64 }
65 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")))))) {
67 refuse.push(Violation::new("item_id", "required", "item_id is required"));
68 }
69 if (create || (update && r::changed(old.as_ref(), row, &["quantity"]))) && !r::holds(&r::ge(r::field(row, "quantity"), r::Val::Num(0.0_f64))) {
71 refuse.push(Violation::new("quantity", "non_negative", "quantity must not be negative"));
72 }
73 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))) {
75 refuse.push(Violation::new("valuation_rate", "non_negative", "valuation_rate must not be negative"));
76 }
77 if (create || (update && r::changed(old.as_ref(), row, &["cogs_amount"]))) && !r::holds(&r::ge(r::field(row, "cogs_amount"), r::Val::Num(0.0_f64))) {
79 refuse.push(Violation::new("cogs_amount", "non_negative", "cogs_amount must not be negative"));
80 }
81 GuardOutcome { refuse, shadow }
82 }
83}
84