backbone-events 0.10.0

Events core + the sale/booth/crm/sms/desk overlay — registrations under one authoritative seat counter, the sale seam (order-minted registrations, forward-only paid heal, SO-cancel mirror cascade), booth bookings with the DB exclusivity wall, closed-vocabulary lead rules through a host sink port, the dual-channel (mail/sms) self-arming scheduler, and the exact-match desk verb (Odoo event port, core + overlay)
//! The write guard of Registration (generated by metaphor-schema; do not edit). It lives in a
//! file of its own because `registration_service.rs` is hand-owned.

#[allow(unused_imports)]
use super::*;
use crate::domain::entity::Registration;

/// The rules the schema declares for Registration, checked against the row a create or update
/// would store. The generated build attaches it to [`RegistrationService`]. A refusing rule's
/// break answers 422; a rule in shadow is logged under `backbone::write_guard` and the
/// write goes ahead.
pub struct RegistrationRowRules;

#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<Registration> for RegistrationRowRules {
    async fn check(
        &self,
        ctx: &backbone_core::write_guard::WriteCtx<'_, Registration>,
    ) -> 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();
        // `state` belongs to a state machine: only its verbs move it
        if update && ctx.transition.is_none() && r::changed(old.as_ref(), row, &["state"]) {
            refuse.push(Violation::new("state", "state_field_verbs_only", "the state field moves only through its verbs"));
        }
        // `event_id_required` (@required on Registration.event_id)
        if (create || (update && r::changed(old.as_ref(), row, &["event_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "event_id")), r::not(r::is_empty(r::trim(r::field(row, "event_id")))))) {
            refuse.push(Violation::new("event_id", "required", "event_id is required"));
        }
        // `name_max_length` (@max(200) on Registration.name)
        if (create || (update && r::changed(old.as_ref(), row, &["name"]))) && !r::holds(&r::le(r::length(r::field(row, "name")), r::Val::Num(200.0_f64))) {
            refuse.push(Violation::new("name", "max_length", "name must be at most 200 characters").with_param("max", 200_i64));
        }
        // `email_max_length` (@max(254) on Registration.email)
        if (create || (update && r::changed(old.as_ref(), row, &["email"]))) && !r::holds(&r::le(r::length(r::field(row, "email")), r::Val::Num(254.0_f64))) {
            refuse.push(Violation::new("email", "max_length", "email must be at most 254 characters").with_param("max", 254_i64));
        }
        // `email_email` (type email on Registration.email)
        if (create || (update && r::changed(old.as_ref(), row, &["email"]))) && !r::holds(&r::matches(r::field(row, "email"), "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$")) {
            refuse.push(Violation::new("email", "email", "email must be an email address"));
        }
        // `phone_max_length` (@max(40) on Registration.phone)
        if (create || (update && r::changed(old.as_ref(), row, &["phone"]))) && !r::holds(&r::le(r::length(r::field(row, "phone")), r::Val::Num(40.0_f64))) {
            refuse.push(Violation::new("phone", "max_length", "phone must be at most 40 characters").with_param("max", 40_i64));
        }
        // `company_name_max_length` (@max(200) on Registration.company_name)
        if (create || (update && r::changed(old.as_ref(), row, &["company_name"]))) && !r::holds(&r::le(r::length(r::field(row, "company_name")), r::Val::Num(200.0_f64))) {
            refuse.push(Violation::new("company_name", "max_length", "company_name must be at most 200 characters").with_param("max", 200_i64));
        }
        // `barcode_required` (@required on Registration.barcode)
        if (create || (update && r::changed(old.as_ref(), row, &["barcode"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "barcode")), r::not(r::is_empty(r::trim(r::field(row, "barcode")))))) {
            refuse.push(Violation::new("barcode", "required", "barcode is required"));
        }
        // `barcode_max_length` (@max(32) on Registration.barcode)
        if (create || (update && r::changed(old.as_ref(), row, &["barcode"]))) && !r::holds(&r::le(r::length(r::field(row, "barcode")), r::Val::Num(32.0_f64))) {
            refuse.push(Violation::new("barcode", "max_length", "barcode must be at most 32 characters").with_param("max", 32_i64));
        }
        GuardOutcome { refuse, shadow }
    }
}