use backbone_core::GenericCrudService;
use crate::presentation::dto::{CreateUserQuotaDto, UpdateUserQuotaDto};
use crate::domain::entity::UserQuota;
use crate::infrastructure::persistence::UserQuotaRepository;
pub type UserQuotaService = GenericCrudService<
UserQuota,
CreateUserQuotaDto,
UpdateUserQuotaDto,
UserQuotaRepository,
>;
pub struct UserQuotaRowRules;
#[async_trait::async_trait]
impl backbone_core::write_guard::WriteGuard<UserQuota> for UserQuotaRowRules {
async fn check(
&self,
ctx: &backbone_core::write_guard::WriteCtx<'_, UserQuota>,
) -> 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, &["quota_status"]) {
shadow.push(Violation::new("quota_status", "state_field_verbs_only", "the state field moves only through its verbs"));
}
if (create || (update && r::changed(old.as_ref(), row, &["user_id"]))) && !r::holds(&r::and(r::is_not_null(r::field(row, "user_id")), r::not(r::is_empty(r::trim(r::field(row, "user_id")))))) {
refuse.push(Violation::new("user_id", "required", "user_id is required"));
}
if (create || (update && r::changed(old.as_ref(), row, &["limit_bytes"]))) && !r::holds(&r::ge(r::field(row, "limit_bytes"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("limit_bytes", "non_negative", "limit_bytes must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["used_bytes"]))) && !r::holds(&r::ge(r::field(row, "used_bytes"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("used_bytes", "non_negative", "used_bytes must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["file_count"]))) && !r::holds(&r::ge(r::field(row, "file_count"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("file_count", "non_negative", "file_count must not be negative"));
}
if (create || (update && r::changed(old.as_ref(), row, &["tier"]))) && !r::holds(&r::le(r::length(r::field(row, "tier")), r::Val::Num(50.0_f64))) {
refuse.push(Violation::new("tier", "max_length", "tier must be at most 50 characters").with_param("max", 50_i64));
}
if (create || (update && r::changed(old.as_ref(), row, &["peak_usage_bytes"]))) && !r::holds(&r::ge(r::field(row, "peak_usage_bytes"), r::Val::Num(0.0_f64))) {
refuse.push(Violation::new("peak_usage_bytes", "non_negative", "peak_usage_bytes must not be negative"));
}
if (create || update) && !r::holds(&r::in_list(r::field(row, "tier"), r::Val::List(vec![r::text("free"), r::text("pro"), r::text("enterprise")]))) {
refuse.push(Violation::new("tier", "invalid_tier", "Invalid tier. Must be free, pro, or enterprise"));
}
if (create || update) && !r::holds(&r::and(r::ge(r::field(row, "warning_threshold_percent"), r::Val::Num(50.0_f64)), r::le(r::field(row, "warning_threshold_percent"), r::Val::Num(99.0_f64)))) {
refuse.push(Violation::new("warning_threshold_percent", "invalid_warning_threshold", "Warning threshold must be between 50 and 99 percent"));
}
GuardOutcome { refuse, shadow }
}
}