backbone-mailing 0.6.3

Email marketing — mass-mail engine: audiences, subscriptions, hand-set mailing lifecycle, seeded A/B testing, per-recipient traces, cycle-44 bridge targets (Odoo mass_mailing port)
//! Application service for Mailing entities
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! Type alias over `GenericCrudService` — all CRUD logic lives in backbone-core.
//! For custom behaviour, create `mailing_service_custom.rs` (preserved on regen).

use backbone_core::GenericCrudService;

use crate::presentation::dto::{CreateMailingDto, UpdateMailingDto};
use crate::domain::entity::Mailing;
use crate::infrastructure::persistence::MailingRepository;

/// Application service for Mailing entities.
///
/// All 12 standard CRUD operations (list, create, get, update, patch,
/// soft_delete, restore, empty_trash, bulk_create, upsert, find_by_id,
/// list_deleted) are provided by `GenericCrudService`.
///
/// Custom extensions go in `mailing_service_custom.rs`:
///
/// ```rust,ignore
/// pub struct MailingServiceCustom {
///     inner: Arc<MailingService>,
///     // add domain-specific dependencies here
/// }
/// ```
pub type MailingService = GenericCrudService<
    Mailing,
    CreateMailingDto,
    UpdateMailingDto,
    MailingRepository,
>;

/// The row rules the schema declares for Mailing, checked against the row a create or
/// update would store. The generated build attaches it to [`MailingService`]. Every rule
/// runs in shadow: a break is logged under `backbone::write_guard` and the write goes ahead.
pub struct MailingRowRules;

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<Mailing> for MailingRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, Mailing>,
    ) -> 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();
        // `state` belongs to a state machine: only its verbs move it
        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 }
    }
}