use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreatePartialReconcileDto, UpdatePartialReconcileDto};
use crate::domain::entity::PartialReconcile;
use crate::infrastructure::persistence::PartialReconcileRepository;
pub type PartialReconcileService = GenericCrudService<
PartialReconcile,
CreatePartialReconcileDto,
UpdatePartialReconcileDto,
PartialReconcileRepository,
>;
pub struct PartialReconcileRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<PartialReconcile> for PartialReconcileRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, PartialReconcile>,
) -> 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 shadow = Vec::new();
let mut refuse = Vec::new();
if (create || (update && r::changed(old.as_ref(), row, &["debit_move_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "debit_move_id")), r::not(r::is_empty(r::trim(r::field(row, "debit_move_id")))))) {
refuse.push(Violation::new("debit_move_id", "required", "debit_move_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["credit_move_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "credit_move_id")), r::not(r::is_empty(r::trim(r::field(row, "credit_move_id")))))) {
refuse.push(Violation::new("credit_move_id", "required", "credit_move_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["amount"]))) && !r::holds(&r::gt(r::field(row, "amount"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("amount", "positive", "amount must be positive"));
}
if (create || (update && r::changed(old.as_ref(), row, &["debit_amount_currency"]))) && !r::holds(&r::ge(r::field(row, "debit_amount_currency"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("debit_amount_currency", "non_negative", "debit_amount_currency must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["credit_amount_currency"]))) && !r::holds(&r::ge(r::field(row, "credit_amount_currency"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("credit_amount_currency", "non_negative", "credit_amount_currency must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["currency"]))) && !r::holds(&r::le(r::length(r::field(row, "currency")), r::Val::Num(3.0_f64))) {
refuse.push(Violation::new("currency", "max_length", "currency must be at most 3 characters").with_param("max", 3_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["source_type"]))) && !r::holds(&r::le(r::length(r::field(row, "source_type")), r::Val::Num(50.0_f64))) {
refuse.push(Violation::new("source_type", "max_length", "source_type must be at most 50 characters").with_param("max", 50_i64));
}
GuardOutcome { refuse, shadow }
}
}