use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateMailNotificationDto, UpdateMailNotificationDto};
use crate::domain::entity::MailNotification;
use crate::infrastructure::persistence::MailNotificationRepository;
pub type MailNotificationService = GenericCrudService<
MailNotification,
CreateMailNotificationDto,
UpdateMailNotificationDto,
MailNotificationRepository,
>;
pub struct MailNotificationRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<MailNotification> for MailNotificationRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, MailNotification>,
) -> 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, &["notification_status"]) {
shadow.push(Violation::new("notification_status", "state_field_verbs_only", "the state field moves only through its verbs"));
}
if (create || (update && r::changed(old.as_ref(), row, &["mail_message_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "mail_message_id")), r::not(r::is_empty(r::trim(r::field(row, "mail_message_id")))))) {
refuse.push(Violation::new("mail_message_id", "required", "mail_message_id is required"));
}
GuardOutcome { refuse, shadow }
}
}