use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct UserQuotaProjection {
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 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>>,
pub metadata: serde_json::Value,
pub projection_version: i64,
pub last_event_sequence: i64,
pub projection_updated_at: DateTime<Utc>,
}
impl UserQuotaProjection {
pub fn new(
id: Uuid,
user_id: Uuid,
limit_bytes: i64,
used_bytes: i64,
file_count: i32,
max_file_size: Option<i64>,
max_file_count: Option<i32>,
tier: String,
quota_status: QuotaStatus,
warning_threshold_percent: i32,
last_warning_sent_at: Option<DateTime<Utc>>,
peak_usage_bytes: i64,
peak_usage_at: Option<DateTime<Utc>>,
metadata: serde_json::Value
) -> Self {
Self {
id,
user_id,
limit_bytes,
used_bytes,
file_count,
max_file_size,
max_file_count,
tier,
quota_status,
warning_threshold_percent,
last_warning_sent_at,
peak_usage_bytes,
peak_usage_at,
metadata,
projection_version: 1,
last_event_sequence: 0,
projection_updated_at: Utc::now(),
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn apply_event(&mut self, event_sequence: i64) {
self.projection_version += 1;
self.last_event_sequence = event_sequence;
self.projection_updated_at = Utc::now();
}
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct UserQuotaSummary {
pub id: Uuid,
}