use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateDigestSubscriptionDto, UpdateDigestSubscriptionDto};
use crate::domain::entity::DigestSubscription;
use crate::infrastructure::persistence::DigestSubscriptionRepository;
pub type DigestSubscriptionService = GenericCrudService<
DigestSubscription,
CreateDigestSubscriptionDto,
UpdateDigestSubscriptionDto,
DigestSubscriptionRepository,
>;
pub struct DigestSubscriptionRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<DigestSubscription> for DigestSubscriptionRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, DigestSubscription>,
) -> 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();
if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["state"]) {
shadow.push(Violation::new("state", "state_field_verbs_only", "the state field moves only through its verbs"));
}
GuardOutcome { refuse: Vec::new(), shadow }
}
}