use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateItemDto, UpdateItemDto};
use crate::domain::entity::Item;
use crate::infrastructure::persistence::ItemRepository;
pub type ItemService = GenericCrudService<
Item,
CreateItemDto,
UpdateItemDto,
ItemRepository,
>;
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();
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"));
}
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"));
}
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));
}
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"));
}
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));
}
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));
}
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));
}
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"));
}
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"));
}
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));
}
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));
}
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"));
}
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 }
}
}