stateset-core 1.22.0

Core domain models and business logic for StateSet iCommerce
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
//! Warranty domain models
//!
//! Handles warranty registration, coverage tracking, and claims processing.

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use stateset_primitives::{CustomerId, OrderId, OrderItemId, ProductId, WarrantyId};
use strum::{Display, EnumString};
use uuid::Uuid;

/// Warranty status
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, Display, EnumString,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
#[non_exhaustive]
pub enum WarrantyStatus {
    /// Warranty is active and valid
    #[default]
    Active,
    /// Warranty has expired
    Expired,
    /// Warranty was voided
    Voided,
    /// Warranty was transferred to another owner
    Transferred,
}

/// Warranty type/tier
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, Display, EnumString,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
#[non_exhaustive]
pub enum WarrantyType {
    /// Standard manufacturer warranty
    #[default]
    Standard,
    /// Extended warranty
    Extended,
    /// Limited warranty
    Limited,
    /// Lifetime warranty
    Lifetime,
    /// Accidental damage protection
    AccidentalDamage,
    /// Comprehensive coverage
    Comprehensive,
}

/// Warranty claim status
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, Display, EnumString,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
#[non_exhaustive]
pub enum ClaimStatus {
    /// Claim submitted, awaiting review
    #[default]
    Submitted,
    /// Claim is under review
    UnderReview,
    /// Additional information requested
    InfoRequested,
    /// Claim approved
    Approved,
    /// Claim denied
    Denied,
    /// Repair/replacement in progress
    InProgress,
    /// Claim completed/resolved
    Completed,
    /// Claim was cancelled
    #[strum(serialize = "cancelled", serialize = "canceled")]
    Cancelled,
}

/// Claim resolution type
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, Display, EnumString,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
#[non_exhaustive]
pub enum ClaimResolution {
    /// No resolution yet
    #[default]
    None,
    /// Item was repaired
    Repair,
    /// Item was replaced
    Replacement,
    /// Customer received refund
    Refund,
    /// Store credit issued
    StoreCredit,
    /// Claim was denied
    Denied,
}

/// A warranty registration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Warranty {
    /// Unique warranty ID
    pub id: WarrantyId,
    /// Human-readable warranty number
    pub warranty_number: String,
    /// Customer who owns the warranty
    pub customer_id: CustomerId,
    /// Associated order ID
    pub order_id: Option<OrderId>,
    /// Associated order item ID
    pub order_item_id: Option<OrderItemId>,
    /// Product ID
    pub product_id: Option<ProductId>,
    /// Product SKU
    pub sku: Option<String>,
    /// Product serial number
    pub serial_number: Option<String>,
    /// Warranty status
    pub status: WarrantyStatus,
    /// Warranty type
    pub warranty_type: WarrantyType,
    /// Warranty provider/issuer
    pub provider: Option<String>,
    /// Coverage description
    pub coverage_description: Option<String>,
    /// Purchase date
    pub purchase_date: DateTime<Utc>,
    /// Warranty start date
    pub start_date: DateTime<Utc>,
    /// Warranty end date
    pub end_date: Option<DateTime<Utc>>,
    /// Duration in months (for non-lifetime)
    pub duration_months: Option<i32>,
    /// Maximum coverage amount
    pub max_coverage_amount: Option<Decimal>,
    /// Deductible amount
    pub deductible: Option<Decimal>,
    /// Number of claims allowed
    pub max_claims: Option<i32>,
    /// Number of claims used
    pub claims_used: i32,
    /// Additional terms and conditions
    pub terms: Option<String>,
    /// Notes
    pub notes: Option<String>,
    /// When warranty was created
    pub created_at: DateTime<Utc>,
    /// When warranty was last updated
    pub updated_at: DateTime<Utc>,
}

impl Warranty {
    /// Check if the warranty is currently valid
    #[must_use]
    pub fn is_valid(&self) -> bool {
        if self.status != WarrantyStatus::Active {
            return false;
        }

        let now = Utc::now();
        if now < self.start_date {
            return false;
        }

        if let Some(end_date) = self.end_date {
            if now > end_date {
                return false;
            }
        }

        // Check if max claims exceeded
        if let Some(max) = self.max_claims {
            if self.claims_used >= max {
                return false;
            }
        }

        true
    }

    /// Get remaining days of coverage
    #[must_use]
    pub fn days_remaining(&self) -> Option<i64> {
        self.end_date.map(|end| {
            let now = Utc::now();
            (end - now).num_days().max(0)
        })
    }
}

/// Input for creating a warranty
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CreateWarranty {
    /// Customer ID
    pub customer_id: CustomerId,
    /// Order ID
    pub order_id: Option<OrderId>,
    /// Order item ID
    pub order_item_id: Option<OrderItemId>,
    /// Product ID
    pub product_id: Option<ProductId>,
    /// Product SKU
    pub sku: Option<String>,
    /// Serial number
    pub serial_number: Option<String>,
    /// Warranty type
    pub warranty_type: Option<WarrantyType>,
    /// Provider
    pub provider: Option<String>,
    /// Coverage description
    pub coverage_description: Option<String>,
    /// Purchase date (defaults to now)
    pub purchase_date: Option<DateTime<Utc>>,
    /// Start date (defaults to purchase date)
    pub start_date: Option<DateTime<Utc>>,
    /// End date (calculated from duration if not provided)
    pub end_date: Option<DateTime<Utc>>,
    /// Duration in months
    pub duration_months: Option<i32>,
    /// Max coverage amount
    pub max_coverage_amount: Option<Decimal>,
    /// Deductible
    pub deductible: Option<Decimal>,
    /// Max claims allowed
    pub max_claims: Option<i32>,
    /// Terms and conditions
    pub terms: Option<String>,
    /// Notes
    pub notes: Option<String>,
}

/// Input for updating a warranty
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UpdateWarranty {
    /// Update status
    pub status: Option<WarrantyStatus>,
    /// Update serial number
    pub serial_number: Option<String>,
    /// Update end date
    pub end_date: Option<DateTime<Utc>>,
    /// Update coverage description
    pub coverage_description: Option<String>,
    /// Update terms
    pub terms: Option<String>,
    /// Update notes
    pub notes: Option<String>,
}

/// Filter for listing warranties
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WarrantyFilter {
    /// Filter by customer ID
    pub customer_id: Option<CustomerId>,
    /// Filter by order ID
    pub order_id: Option<OrderId>,
    /// Filter by product ID
    pub product_id: Option<ProductId>,
    /// Filter by SKU
    pub sku: Option<String>,
    /// Filter by serial number
    pub serial_number: Option<String>,
    /// Filter by status
    pub status: Option<WarrantyStatus>,
    /// Filter by warranty type
    pub warranty_type: Option<WarrantyType>,
    /// Filter by active only (not expired)
    pub active_only: Option<bool>,
    /// Filter by expiring within days
    pub expiring_within_days: Option<i32>,
    /// Limit results
    pub limit: Option<u32>,
    /// Offset for pagination
    pub offset: Option<u32>,
}

/// A warranty claim
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarrantyClaim {
    /// Unique claim ID
    pub id: Uuid,
    /// Human-readable claim number
    pub claim_number: String,
    /// Associated warranty ID
    pub warranty_id: WarrantyId,
    /// Customer ID
    pub customer_id: CustomerId,
    /// Claim status
    pub status: ClaimStatus,
    /// Resolution type
    pub resolution: ClaimResolution,
    /// Issue description
    pub issue_description: String,
    /// Issue category
    pub issue_category: Option<String>,
    /// Date issue occurred
    pub issue_date: Option<DateTime<Utc>>,
    /// Contact phone
    pub contact_phone: Option<String>,
    /// Contact email
    pub contact_email: Option<String>,
    /// Shipping address for returns/replacements
    pub shipping_address: Option<String>,
    /// Repair cost (if repair)
    pub repair_cost: Option<Decimal>,
    /// Replacement product ID (if replacement)
    pub replacement_product_id: Option<ProductId>,
    /// Refund amount (if refund)
    pub refund_amount: Option<Decimal>,
    /// Denial reason (if denied)
    pub denial_reason: Option<String>,
    /// Internal notes
    pub internal_notes: Option<String>,
    /// Customer-facing notes
    pub customer_notes: Option<String>,
    /// When claim was submitted
    pub submitted_at: DateTime<Utc>,
    /// When claim was approved
    pub approved_at: Option<DateTime<Utc>>,
    /// When claim was resolved
    pub resolved_at: Option<DateTime<Utc>>,
    /// When claim was created
    pub created_at: DateTime<Utc>,
    /// When claim was last updated
    pub updated_at: DateTime<Utc>,
}

/// Input for creating a warranty claim
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CreateWarrantyClaim {
    /// Warranty ID
    pub warranty_id: WarrantyId,
    /// Issue description
    pub issue_description: String,
    /// Issue category
    pub issue_category: Option<String>,
    /// Date issue occurred
    pub issue_date: Option<DateTime<Utc>>,
    /// Contact phone
    pub contact_phone: Option<String>,
    /// Contact email
    pub contact_email: Option<String>,
    /// Shipping address
    pub shipping_address: Option<String>,
    /// Customer notes
    pub customer_notes: Option<String>,
}

/// Input for updating a warranty claim
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UpdateWarrantyClaim {
    /// Update status
    pub status: Option<ClaimStatus>,
    /// Update resolution
    pub resolution: Option<ClaimResolution>,
    /// Update repair cost
    pub repair_cost: Option<Decimal>,
    /// Update replacement product ID
    pub replacement_product_id: Option<ProductId>,
    /// Update refund amount
    pub refund_amount: Option<Decimal>,
    /// Update denial reason
    pub denial_reason: Option<String>,
    /// Update internal notes
    pub internal_notes: Option<String>,
    /// Update customer notes
    pub customer_notes: Option<String>,
}

/// Filter for listing warranty claims
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WarrantyClaimFilter {
    /// Filter by warranty ID
    pub warranty_id: Option<WarrantyId>,
    /// Filter by customer ID
    pub customer_id: Option<CustomerId>,
    /// Filter by status
    pub status: Option<ClaimStatus>,
    /// Filter by resolution
    pub resolution: Option<ClaimResolution>,
    /// Filter by date range start
    pub from_date: Option<DateTime<Utc>>,
    /// Filter by date range end
    pub to_date: Option<DateTime<Utc>>,
    /// Limit results
    pub limit: Option<u32>,
    /// Offset for pagination
    pub offset: Option<u32>,
}

/// Generate a unique warranty number
#[must_use]
pub fn generate_warranty_number() -> String {
    // Millisecond timestamp + random UUID suffix so concurrent or rapid-fire
    // warranty creation cannot collide on the UNIQUE constraint.
    let now = chrono::Utc::now();
    let suffix = &uuid::Uuid::new_v4().simple().to_string()[..8];
    format!("WRN-{}-{suffix}", now.format("%Y%m%d%H%M%S%3f"))
}

/// Generate a unique claim number
#[must_use]
pub fn generate_claim_number() -> String {
    // Millisecond timestamp + random UUID suffix so concurrent or rapid-fire
    // claim creation cannot collide on the UNIQUE constraint.
    let now = chrono::Utc::now();
    let suffix = &uuid::Uuid::new_v4().simple().to_string()[..8];
    format!("CLM-{}-{suffix}", now.format("%Y%m%d%H%M%S%3f"))
}