use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateStockMoveDto, UpdateStockMoveDto};
use crate::domain::entity::StockMove;
use crate::infrastructure::persistence::StockMoveRepository;
pub type StockMoveService = GenericCrudService<
StockMove,
CreateStockMoveDto,
UpdateStockMoveDto,
StockMoveRepository,
>;
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();
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"));
}
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(400.0_f64))) {
refuse.push(Violation::new("name", "max_length", "name must be at most 400 characters").with_param("max", 400_i64));
}
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"));
}
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"));
}
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"));
}
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));
}
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"));
}
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"));
}
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 }
}
}