use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreatePosInvoiceDto, UpdatePosInvoiceDto};
use crate::domain::entity::PosInvoice;
use crate::infrastructure::persistence::PosInvoiceRepository;
pub type PosInvoiceService = GenericCrudService<
PosInvoice,
CreatePosInvoiceDto,
UpdatePosInvoiceDto,
PosInvoiceRepository,
>;
pub struct PosInvoiceRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<PosInvoice> for PosInvoiceRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, PosInvoice>,
) -> 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, &["pos_profile_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "pos_profile_id")), r::not(r::is_empty(r::trim(r::field(row, "pos_profile_id")))))) {
refuse.push(Violation::new("pos_profile_id", "required", "pos_profile_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["opening_entry_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "opening_entry_id")), r::not(r::is_empty(r::trim(r::field(row, "opening_entry_id")))))) {
refuse.push(Violation::new("opening_entry_id", "required", "opening_entry_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["receipt_number"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "receipt_number")), r::not(r::is_empty(r::trim(r::field(row, "receipt_number")))))) {
refuse.push(Violation::new("receipt_number", "required", "receipt_number is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["receipt_number"]))) && !r::holds(&r::le(r::length(r::field(row, "receipt_number")), r::Val::Num(40.0_f64))) {
shadow.push(Violation::new("receipt_number", "max_length", "receipt_number must be at most 40 characters").with_param("max", 40_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["net_total"]))) && !r::holds(&r::ge(r::field(row, "net_total"), r::Val::Num(0.0_f64))) {
shadow.push(Violation::new("net_total", "non_negative", "net_total must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["tax_total"]))) && !r::holds(&r::ge(r::field(row, "tax_total"), r::Val::Num(0.0_f64))) {
shadow.push(Violation::new("tax_total", "non_negative", "tax_total must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["grand_total"]))) && !r::holds(&r::ge(r::field(row, "grand_total"), r::Val::Num(0.0_f64))) {
shadow.push(Violation::new("grand_total", "non_negative", "grand_total must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["rounded_total"]))) && !r::holds(&r::ge(r::field(row, "rounded_total"), r::Val::Num(0.0_f64))) {
shadow.push(Violation::new("rounded_total", "non_negative", "rounded_total must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["paid_total"]))) && !r::holds(&r::ge(r::field(row, "paid_total"), r::Val::Num(0.0_f64))) {
shadow.push(Violation::new("paid_total", "non_negative", "paid_total must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["change_due"]))) && !r::holds(&r::ge(r::field(row, "change_due"), r::Val::Num(0.0_f64))) {
shadow.push(Violation::new("change_due", "non_negative", "change_due must not be negative"));
}
GuardOutcome { refuse, shadow }
}
}