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
//! Warranty operations for product warranty registration and claims
//!
//! # Example
//!
//! ```rust,no_run
//! use stateset_embedded::{Commerce, CreateWarranty, ProductId, WarrantyType};
//! use uuid::Uuid;
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! // Register a warranty for a product
//! let warranty = commerce.warranties().create(CreateWarranty {
//! customer_id: Uuid::new_v4().into(),
//! product_id: Some(ProductId::new()),
//! order_id: Some(Uuid::new_v4().into()),
//! warranty_type: Some(WarrantyType::Standard),
//! duration_months: Some(12),
//! ..Default::default()
//! })?;
//!
//! // File a warranty claim
//! let claim = commerce.warranties().create_claim(stateset_embedded::CreateWarrantyClaim {
//! warranty_id: warranty.id,
//! issue_description: "Product stopped working after 3 months".into(),
//! ..Default::default()
//! })?;
//!
//! // Approve and complete the claim
//! let claim = commerce.warranties().approve_claim(claim.id)?;
//! let claim = commerce.warranties().complete_claim(
//! claim.id,
//! stateset_embedded::ClaimResolution::Replacement
//! )?;
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use crate::Database;
use stateset_core::{
ClaimResolution, CreateWarranty, CreateWarrantyClaim, Result, UpdateWarrantyClaim, Warranty,
WarrantyClaim, WarrantyClaimFilter, WarrantyFilter,
};
use std::sync::Arc;
use uuid::Uuid;
/// Warranty operations for product warranty management
pub struct Warranties {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for Warranties {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Warranties").finish_non_exhaustive()
}
}
impl Warranties {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
/// Register a new warranty
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, CreateWarranty, ProductId, WarrantyType};
/// use uuid::Uuid;
///
/// let commerce = Commerce::new("./store.db")?;
///
/// let warranty = commerce.warranties().create(CreateWarranty {
/// customer_id: Uuid::new_v4().into(),
/// product_id: Some(ProductId::new()),
/// warranty_type: Some(WarrantyType::Extended),
/// duration_months: Some(24),
/// coverage_description: Some("Full coverage including accidental damage".into()),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create(&self, input: CreateWarranty) -> Result<Warranty> {
self.db.warranties().create(input)
}
/// Get a warranty by ID
pub fn get(&self, id: Uuid) -> Result<Option<Warranty>> {
self.db.warranties().get(id.into())
}
/// Get a warranty by warranty number (e.g., "WTY-20231215123456")
pub fn get_by_number(&self, warranty_number: &str) -> Result<Option<Warranty>> {
self.db.warranties().get_by_number(warranty_number)
}
/// Get a warranty by serial number
pub fn get_by_serial(&self, serial_number: &str) -> Result<Option<Warranty>> {
self.db.warranties().get_by_serial(serial_number)
}
/// Update a warranty
pub fn update(&self, id: Uuid, input: stateset_core::UpdateWarranty) -> Result<Warranty> {
self.db.warranties().update(id.into(), input)
}
/// List warranties with optional filtering
pub fn list(&self, filter: WarrantyFilter) -> Result<Vec<Warranty>> {
self.db.warranties().list(filter)
}
/// Get all warranties for a customer
pub fn for_customer(&self, customer_id: Uuid) -> Result<Vec<Warranty>> {
self.db.warranties().for_customer(customer_id.into())
}
/// Get all warranties for an order
pub fn for_order(&self, order_id: Uuid) -> Result<Vec<Warranty>> {
self.db.warranties().for_order(order_id.into())
}
/// Expire a warranty
pub fn expire(&self, id: Uuid) -> Result<Warranty> {
self.db.warranties().expire(id.into())
}
/// Void a warranty (e.g., due to terms violation)
pub fn void(&self, id: Uuid) -> Result<Warranty> {
self.db.warranties().void(id.into())
}
/// Transfer warranty to a new customer
pub fn transfer(&self, id: Uuid, new_customer_id: Uuid) -> Result<Warranty> {
self.db.warranties().transfer(id.into(), new_customer_id.into())
}
/// Check if a warranty is valid (active and not expired)
pub fn is_valid(&self, id: Uuid) -> Result<bool> {
if let Some(warranty) = self.get(id)? { Ok(warranty.is_valid()) } else { Ok(false) }
}
/// File a warranty claim
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, CreateWarrantyClaim};
/// use uuid::Uuid;
///
/// let commerce = Commerce::new("./store.db")?;
///
/// let claim = commerce.warranties().create_claim(CreateWarrantyClaim {
/// warranty_id: Uuid::new_v4().into(),
/// issue_description: "Screen cracked".into(),
/// contact_email: Some("customer@example.com".into()),
/// contact_phone: Some("555-1234".into()),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_claim(&self, input: CreateWarrantyClaim) -> Result<WarrantyClaim> {
self.db.warranties().create_claim(input)
}
/// Get a warranty claim by ID
pub fn get_claim(&self, id: Uuid) -> Result<Option<WarrantyClaim>> {
self.db.warranties().get_claim(id)
}
/// Get a claim by claim number
pub fn get_claim_by_number(&self, claim_number: &str) -> Result<Option<WarrantyClaim>> {
self.db.warranties().get_claim_by_number(claim_number)
}
/// Update a warranty claim
pub fn update_claim(&self, id: Uuid, input: UpdateWarrantyClaim) -> Result<WarrantyClaim> {
self.db.warranties().update_claim(id, input)
}
/// Get all claims for a warranty
pub fn get_claims(&self, warranty_id: Uuid) -> Result<Vec<WarrantyClaim>> {
self.db.warranties().get_claims(warranty_id.into())
}
/// List claims with optional filtering
pub fn list_claims(&self, filter: WarrantyClaimFilter) -> Result<Vec<WarrantyClaim>> {
self.db.warranties().list_claims(filter)
}
/// Approve a warranty claim
pub fn approve_claim(&self, id: Uuid) -> Result<WarrantyClaim> {
self.db.warranties().approve_claim(id)
}
/// Deny a warranty claim
pub fn deny_claim(&self, id: Uuid, reason: &str) -> Result<WarrantyClaim> {
self.db.warranties().deny_claim(id, reason)
}
/// Complete a warranty claim with resolution
///
/// # Example
///
/// ```rust,no_run
/// use stateset_embedded::{Commerce, ClaimResolution};
/// use uuid::Uuid;
///
/// let commerce = Commerce::new("./store.db")?;
///
/// let claim = commerce.warranties().complete_claim(
/// Uuid::new_v4(),
/// ClaimResolution::Replacement
/// )?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn complete_claim(&self, id: Uuid, resolution: ClaimResolution) -> Result<WarrantyClaim> {
self.db.warranties().complete_claim(id, resolution)
}
/// Cancel a warranty claim
pub fn cancel_claim(&self, id: Uuid) -> Result<WarrantyClaim> {
self.db.warranties().cancel_claim(id)
}
/// Count warranties matching a filter
pub fn count(&self, filter: WarrantyFilter) -> Result<u64> {
self.db.warranties().count(filter)
}
/// Count claims matching a filter
pub fn count_claims(&self, filter: WarrantyClaimFilter) -> Result<u64> {
self.db.warranties().count_claims(filter)
}
}