#[allow(unused_imports)]
use super::*;
use crate::domain::entity::Registration;
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();
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"));
}
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"));
}
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));
}
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));
}
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"));
}
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));
}
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));
}
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"));
}
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 }
}
}