use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreatePosClosingEntryDto, UpdatePosClosingEntryDto};
use crate::domain::entity::PosClosingEntry;
use crate::infrastructure::persistence::PosClosingEntryRepository;
pub type PosClosingEntryService = GenericCrudService<
PosClosingEntry,
CreatePosClosingEntryDto,
UpdatePosClosingEntryDto,
PosClosingEntryRepository,
>;
pub struct PosClosingEntryRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<PosClosingEntry> for PosClosingEntryRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, PosClosingEntry>,
) -> 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, &["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, &["cashier_party_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "cashier_party_id")), r::not(r::is_empty(r::trim(r::field(row, "cashier_party_id")))))) {
refuse.push(Violation::new("cashier_party_id", "required", "cashier_party_id is required"));
}
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))) {
refuse.push(Violation::new("grand_total", "non_negative", "grand_total must not be negative"));
}
GuardOutcome { refuse, shadow }
}
}