use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
use super::QuotaStatus;
use super::AuditMetadata;
use super::*;
use crate::domain::state_machine::{UserQuotaStateMachine, UserQuotaState, StateMachineError};
use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum QuotaExceeded {
#[error("{0}")]
Message(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Validation failed: {0}")]
ValidationFailed(String),
#[error("Conflict: {0}")]
Conflict(String),
}
impl From<String> for QuotaExceeded {
fn from(msg: String) -> Self { Self::Message(msg) }
}
impl From<&str> for QuotaExceeded {
fn from(msg: &str) -> Self { Self::Message(msg.to_string()) }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct UserQuotaId(pub Uuid);
impl UserQuotaId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for UserQuotaId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for UserQuotaId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for UserQuotaId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<UserQuotaId> for Uuid {
fn from(id: UserQuotaId) -> Self { id.0 }
}
impl AsRef<Uuid> for UserQuotaId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for UserQuotaId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct UserQuota {
pub id: Uuid,
pub user_id: Uuid,
pub limit_bytes: i64,
pub used_bytes: i64,
pub file_count: i32,
pub max_file_size: Option<i64>,
pub max_file_count: Option<i32>,
pub tier: String,
pub(crate) quota_status: QuotaStatus,
pub warning_threshold_percent: i32,
pub last_warning_sent_at: Option<DateTime<Utc>>,
pub peak_usage_bytes: i64,
pub peak_usage_at: Option<DateTime<Utc>>,
#[serde(default)]
#[sqlx(json)]
pub metadata: AuditMetadata,
}
impl UserQuota {
pub fn builder() -> UserQuotaBuilder {
<UserQuotaBuilder as Default>::default()
}
pub fn new(user_id: Uuid, limit_bytes: i64, used_bytes: i64, file_count: i32, tier: String, quota_status: QuotaStatus, warning_threshold_percent: i32, peak_usage_bytes: i64) -> Self {
Self {
id: Uuid::new_v4(),
user_id,
limit_bytes,
used_bytes,
file_count,
max_file_size: None,
max_file_count: None,
tier,
quota_status,
warning_threshold_percent,
last_warning_sent_at: None,
peak_usage_bytes,
peak_usage_at: None,
metadata: AuditMetadata::default(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> UserQuotaId {
UserQuotaId(self.id)
}
pub fn created_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.created_at.as_ref()
}
pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.updated_at.as_ref()
}
pub fn is_deleted(&self) -> bool {
self.metadata.deleted_at.is_some()
}
pub fn is_active(&self) -> bool {
self.metadata.deleted_at.is_none()
}
pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
self.metadata.deleted_at.as_ref()
}
pub fn created_by(&self) -> Option<&Uuid> {
self.metadata.created_by.as_ref()
}
pub fn updated_by(&self) -> Option<&Uuid> {
self.metadata.updated_by.as_ref()
}
pub fn deleted_by(&self) -> Option<&Uuid> {
self.metadata.deleted_by.as_ref()
}
pub fn with_max_file_size(mut self, value: i64) -> Self {
self.max_file_size = Some(value);
self
}
pub fn with_max_file_count(mut self, value: i32) -> Self {
self.max_file_count = Some(value);
self
}
pub fn with_last_warning_sent_at(mut self, value: DateTime<Utc>) -> Self {
self.last_warning_sent_at = Some(value);
self
}
pub fn with_peak_usage_at(mut self, value: DateTime<Utc>) -> Self {
self.peak_usage_at = Some(value);
self
}
pub fn transition_to(&mut self, new_state: UserQuotaState) -> Result<(), StateMachineError> {
let current = self.quota_status.to_string().parse::<UserQuotaState>()?;
let mut sm = UserQuotaStateMachine::from_state(current);
sm.transition_to_state(new_state)?;
self.quota_status = new_state.to_string().parse::<QuotaStatus>()
.map_err(|e| StateMachineError::InvalidState(e.to_string()))?;
Ok(())
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"user_id" => {
if let Ok(v) = serde_json::from_value(value) { self.user_id = v; }
}
"limit_bytes" => {
if let Ok(v) = serde_json::from_value(value) { self.limit_bytes = v; }
}
"used_bytes" => {
if let Ok(v) = serde_json::from_value(value) { self.used_bytes = v; }
}
"file_count" => {
if let Ok(v) = serde_json::from_value(value) { self.file_count = v; }
}
"max_file_size" => {
if let Ok(v) = serde_json::from_value(value) { self.max_file_size = v; }
}
"max_file_count" => {
if let Ok(v) = serde_json::from_value(value) { self.max_file_count = v; }
}
"tier" => {
if let Ok(v) = serde_json::from_value(value) { self.tier = v; }
}
"warning_threshold_percent" => {
if let Ok(v) = serde_json::from_value(value) { self.warning_threshold_percent = v; }
}
"last_warning_sent_at" => {
if let Ok(v) = serde_json::from_value(value) { self.last_warning_sent_at = v; }
}
"peak_usage_bytes" => {
if let Ok(v) = serde_json::from_value(value) { self.peak_usage_bytes = v; }
}
"peak_usage_at" => {
if let Ok(v) = serde_json::from_value(value) { self.peak_usage_at = v; }
}
_ => {} }
}
}
}
impl super::Entity for UserQuota {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"UserQuota"
}
}
impl backbone_core::PersistentEntity for UserQuota {
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.created_at
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.created_at = Some(ts);
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.updated_at
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.metadata.updated_at = Some(ts);
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
self.metadata.deleted_at
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
self.metadata.deleted_at = ts;
}
}
impl backbone_orm::EntityRepoMeta for UserQuota {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("user_id".to_string(), "uuid".to_string());
m.insert("quota_status".to_string(), "quota_status".to_string());
m.insert("last_warning_sent_at".to_string(), "timestamptz".to_string());
m.insert("peak_usage_at".to_string(), "timestamptz".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&["tier"]
}
}
#[derive(Debug, Clone, Default)]
pub struct UserQuotaBuilder {
user_id: Option<Uuid>,
limit_bytes: Option<i64>,
used_bytes: Option<i64>,
file_count: Option<i32>,
max_file_size: Option<i64>,
max_file_count: Option<i32>,
tier: Option<String>,
quota_status: Option<QuotaStatus>,
warning_threshold_percent: Option<i32>,
last_warning_sent_at: Option<DateTime<Utc>>,
peak_usage_bytes: Option<i64>,
peak_usage_at: Option<DateTime<Utc>>,
}
impl UserQuotaBuilder {
pub fn user_id(mut self, value: Uuid) -> Self {
self.user_id = Some(value);
self
}
pub fn limit_bytes(mut self, value: i64) -> Self {
self.limit_bytes = Some(value);
self
}
pub fn used_bytes(mut self, value: i64) -> Self {
self.used_bytes = Some(value);
self
}
pub fn file_count(mut self, value: i32) -> Self {
self.file_count = Some(value);
self
}
pub fn max_file_size(mut self, value: i64) -> Self {
self.max_file_size = Some(value);
self
}
pub fn max_file_count(mut self, value: i32) -> Self {
self.max_file_count = Some(value);
self
}
pub fn tier(mut self, value: String) -> Self {
self.tier = Some(value);
self
}
pub fn quota_status(mut self, value: QuotaStatus) -> Self {
self.quota_status = Some(value);
self
}
pub fn warning_threshold_percent(mut self, value: i32) -> Self {
self.warning_threshold_percent = Some(value);
self
}
pub fn last_warning_sent_at(mut self, value: DateTime<Utc>) -> Self {
self.last_warning_sent_at = Some(value);
self
}
pub fn peak_usage_bytes(mut self, value: i64) -> Self {
self.peak_usage_bytes = Some(value);
self
}
pub fn peak_usage_at(mut self, value: DateTime<Utc>) -> Self {
self.peak_usage_at = Some(value);
self
}
pub fn build(self) -> Result<UserQuota, String> {
let user_id = self.user_id.ok_or_else(|| "user_id is required".to_string())?;
let limit_bytes = self.limit_bytes.ok_or_else(|| "limit_bytes is required".to_string())?;
Ok(UserQuota {
id: Uuid::new_v4(),
user_id,
limit_bytes,
used_bytes: self.used_bytes.unwrap_or(0),
file_count: self.file_count.unwrap_or(0),
max_file_size: self.max_file_size,
max_file_count: self.max_file_count,
tier: self.tier.unwrap_or_default(),
quota_status: self.quota_status.unwrap_or_default(),
warning_threshold_percent: self.warning_threshold_percent.unwrap_or(80),
last_warning_sent_at: self.last_warning_sent_at,
peak_usage_bytes: self.peak_usage_bytes.unwrap_or(0),
peak_usage_at: self.peak_usage_at,
metadata: AuditMetadata::default(),
})
}
}
#[path = "user_quota.ext.rs"]
mod user_quota_ext;
pub use user_quota_ext::*;