backbone-bucket 0.3.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
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;

/// Domain error for quotaexceeded operations
#[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()) }
}


/// Strongly-typed ID for UserQuota
#[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 {
    /// Create a builder for UserQuota
    pub fn builder() -> UserQuotaBuilder {
        <UserQuotaBuilder as Default>::default()
    }

    /// Create a new UserQuota with required fields
    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(),
        }
    }

    /// Get the entity's unique identifier
    pub fn id(&self) -> &Uuid {
        &self.id
    }

    /// Get a strongly-typed ID for this entity
    pub fn typed_id(&self) -> UserQuotaId {
        UserQuotaId(self.id)
    }

    /// Get when this entity was created
    pub fn created_at(&self) -> Option<&DateTime<Utc>> {
        self.metadata.created_at.as_ref()
    }

    /// Get when this entity was last updated
    pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
        self.metadata.updated_at.as_ref()
    }

    /// Check if this entity is soft deleted
    pub fn is_deleted(&self) -> bool {
        self.metadata.deleted_at.is_some()
    }

    /// Check if this entity is active (not deleted)
    pub fn is_active(&self) -> bool {
        self.metadata.deleted_at.is_none()
    }

    /// Get when this entity was deleted
    pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
        self.metadata.deleted_at.as_ref()
    }

    /// Get who created this entity
    pub fn created_by(&self) -> Option<&Uuid> {
        self.metadata.created_by.as_ref()
    }

    /// Get who last updated this entity
    pub fn updated_by(&self) -> Option<&Uuid> {
        self.metadata.updated_by.as_ref()
    }

    /// Get who deleted this entity
    pub fn deleted_by(&self) -> Option<&Uuid> {
        self.metadata.deleted_by.as_ref()
    }


    // ==========================================================
    // Fluent Setters (with_* for optional fields)
    // ==========================================================

    /// Set the max_file_size field (chainable)
    pub fn with_max_file_size(mut self, value: i64) -> Self {
        self.max_file_size = Some(value);
        self
    }

    /// Set the max_file_count field (chainable)
    pub fn with_max_file_count(mut self, value: i32) -> Self {
        self.max_file_count = Some(value);
        self
    }

    /// Set the last_warning_sent_at field (chainable)
    pub fn with_last_warning_sent_at(mut self, value: DateTime<Utc>) -> Self {
        self.last_warning_sent_at = Some(value);
        self
    }

    /// Set the peak_usage_at field (chainable)
    pub fn with_peak_usage_at(mut self, value: DateTime<Utc>) -> Self {
        self.peak_usage_at = Some(value);
        self
    }

    // ==========================================================
    // State Machine
    // ==========================================================

    /// Transition to a new state via the quota_status state machine.
    ///
    /// Returns `Err` if the transition is not permitted from the current state.
    /// Use this method instead of assigning `self.quota_status` directly.
    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(())
    }

    // ==========================================================
    // Partial Update
    // ==========================================================

    /// Apply partial updates from a map of field name to JSON value
    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; }
                }
                _ => {} // ignore unknown fields
            }
        }
    }

    // <<< CUSTOM METHODS START >>>

    // ==========================================================
    // DDD Entity Methods
    // ==========================================================

    /// Check if user has space for additional bytes
    pub fn has_space_for(&self, bytes: i64) -> bool {
        self.used_bytes + bytes <= self.limit_bytes
    }

    /// Get current usage percentage
    pub fn usage_percent(&self) -> f64 {
        if self.limit_bytes <= 0 {
            return 0.0;
        }
        (self.used_bytes as f64 / self.limit_bytes as f64) * 100.0
    }

    /// Check if usage exceeds warning threshold
    pub fn is_over_warning_threshold(&self) -> bool {
        self.usage_percent() >= self.warning_threshold_percent as f64
    }

    /// Add bytes to usage, returns error if exceeded
    pub fn add_usage(&mut self, bytes: i64) -> Result<(), QuotaExceeded> {
        if self.used_bytes + bytes > self.limit_bytes {
            return Err(QuotaExceeded::ValidationFailed(
                format!(
                    "Adding {} bytes would exceed quota limit ({}/{} bytes)",
                    bytes, self.used_bytes + bytes, self.limit_bytes
                ),
            ));
        }
        self.used_bytes += bytes;
        self.metadata.updated_at = Some(Utc::now());
        Ok(())
    }

    /// Subtract bytes from usage
    pub fn subtract_usage(&mut self, bytes: i64) {
        self.used_bytes = (self.used_bytes - bytes).max(0);
        self.metadata.updated_at = Some(Utc::now());
    }

    /// Update peak usage if current exceeds
    pub fn update_peak(&mut self) {
        if self.used_bytes > self.peak_usage_bytes {
            self.peak_usage_bytes = self.used_bytes;
            self.peak_usage_at = Some(Utc::now());
        }
    }

    /// Get remaining available bytes
    pub fn remaining_bytes(&self) -> i64 {
        (self.limit_bytes - self.used_bytes).max(0)
    }

    /// Check all business invariants
    pub fn check_invariants(&self) -> Result<(), Vec<&'static str>> {
        let mut errors = Vec::new();
        // Invariant 1: used_bytes <= limit_bytes
        if self.used_bytes > self.limit_bytes {
            errors.push("used_bytes must not exceed limit_bytes");
        }
        // Invariant 2: used_bytes >= 0
        if self.used_bytes < 0 {
            errors.push("used_bytes must be non-negative");
        }
        // Invariant 3: file_count >= 0
        if self.file_count < 0 {
            errors.push("file_count must be non-negative");
        }
        // Invariant 4: warning_threshold_percent in 0..=100
        if self.warning_threshold_percent < 0 || self.warning_threshold_percent > 100 {
            errors.push("warning_threshold_percent must be between 0 and 100");
        }
        // Invariant 5: limit_bytes must be positive
        if self.limit_bytes <= 0 {
            errors.push("limit_bytes must be positive");
        }
        if errors.is_empty() { Ok(()) } else { Err(errors) }
    }
    // <<< CUSTOM METHODS END >>>
}

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"]
    }
}

/// Builder for UserQuota entity
///
/// Provides a fluent API for constructing UserQuota instances.
/// System fields (id, metadata, timestamps) are auto-initialized.
#[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 {
    /// Set the user_id field (required)
    pub fn user_id(mut self, value: Uuid) -> Self {
        self.user_id = Some(value);
        self
    }

    /// Set the limit_bytes field (required)
    pub fn limit_bytes(mut self, value: i64) -> Self {
        self.limit_bytes = Some(value);
        self
    }

    /// Set the used_bytes field (default: `0`)
    pub fn used_bytes(mut self, value: i64) -> Self {
        self.used_bytes = Some(value);
        self
    }

    /// Set the file_count field (default: `0`)
    pub fn file_count(mut self, value: i32) -> Self {
        self.file_count = Some(value);
        self
    }

    /// Set the max_file_size field (optional)
    pub fn max_file_size(mut self, value: i64) -> Self {
        self.max_file_size = Some(value);
        self
    }

    /// Set the max_file_count field (optional)
    pub fn max_file_count(mut self, value: i32) -> Self {
        self.max_file_count = Some(value);
        self
    }

    /// Set the tier field (default: `Default::default()`)
    pub fn tier(mut self, value: String) -> Self {
        self.tier = Some(value);
        self
    }

    /// Set the quota_status field (default: `QuotaStatus::default()`)
    pub fn quota_status(mut self, value: QuotaStatus) -> Self {
        self.quota_status = Some(value);
        self
    }

    /// Set the warning_threshold_percent field (default: `80`)
    pub fn warning_threshold_percent(mut self, value: i32) -> Self {
        self.warning_threshold_percent = Some(value);
        self
    }

    /// Set the last_warning_sent_at field (optional)
    pub fn last_warning_sent_at(mut self, value: DateTime<Utc>) -> Self {
        self.last_warning_sent_at = Some(value);
        self
    }

    /// Set the peak_usage_bytes field (default: `0`)
    pub fn peak_usage_bytes(mut self, value: i64) -> Self {
        self.peak_usage_bytes = Some(value);
        self
    }

    /// Set the peak_usage_at field (optional)
    pub fn peak_usage_at(mut self, value: DateTime<Utc>) -> Self {
        self.peak_usage_at = Some(value);
        self
    }

    /// Build the UserQuota entity
    ///
    /// Returns Err if any required field without a default is missing.
    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(),
        })
    }
}