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
//! Loyalty program operations for points, accounts, and rewards
//!
//! Manages loyalty programs, customer enrollment, point adjustments,
//! and a reward catalog for point redemption.
//!
//! # Example
//!
//! ```rust,ignore
//! use stateset_embedded::{Commerce, CreateLoyaltyProgram};
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! let program = commerce.loyalty().create_program(CreateLoyaltyProgram {
//! name: "Gold Rewards".into(),
//! points_per_dollar: Some(10),
//! ..Default::default()
//! })?;
//!
//! println!("Program created: {}", program.name);
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use stateset_core::{
AdjustPoints, CreateLoyaltyProgram, CreateReward, CustomerId, EnrollCustomer, LoyaltyAccount,
LoyaltyAccountFilter, LoyaltyAccountId, LoyaltyProgram, LoyaltyProgramId, LoyaltyTransaction,
Result, Reward, RewardFilter, RewardId,
};
use stateset_db::Database;
use std::sync::Arc;
/// Loyalty program operations for points, accounts, and rewards.
pub struct Loyalty {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for Loyalty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Loyalty").finish_non_exhaustive()
}
}
impl Loyalty {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
// ========================================================================
// Program Operations
// ========================================================================
/// Create a new loyalty program.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, CreateLoyaltyProgram};
///
/// let commerce = Commerce::new("./store.db")?;
///
/// let program = commerce.loyalty().create_program(CreateLoyaltyProgram {
/// name: "Platinum Plus".into(),
/// points_per_dollar: Some(15),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create_program(&self, input: CreateLoyaltyProgram) -> Result<LoyaltyProgram> {
self.db.loyalty_programs().create(input)
}
/// Get a loyalty program by ID.
pub fn get_program(&self, id: LoyaltyProgramId) -> Result<Option<LoyaltyProgram>> {
self.db.loyalty_programs().get(id)
}
/// List all loyalty programs.
pub fn list_programs(&self) -> Result<Vec<LoyaltyProgram>> {
self.db.loyalty_programs().list()
}
// ========================================================================
// Account Operations
// ========================================================================
/// Enroll a customer in a loyalty program.
pub fn enroll(&self, input: EnrollCustomer) -> Result<LoyaltyAccount> {
self.db.loyalty_programs().enroll(input)
}
/// Get a loyalty account by ID.
pub fn get_account(&self, id: LoyaltyAccountId) -> Result<Option<LoyaltyAccount>> {
self.db.loyalty_programs().get_account(id)
}
/// Get a loyalty account by customer and program.
pub fn get_account_by_customer(
&self,
customer_id: CustomerId,
program_id: LoyaltyProgramId,
) -> Result<Option<LoyaltyAccount>> {
self.db.loyalty_programs().get_account_by_customer(customer_id, program_id)
}
/// List loyalty accounts with optional filtering.
pub fn list_accounts(&self, filter: LoyaltyAccountFilter) -> Result<Vec<LoyaltyAccount>> {
self.db.loyalty_programs().list_accounts(filter)
}
// ========================================================================
// Points Operations
// ========================================================================
/// Adjust points on an account (earn, redeem, expire, etc.).
pub fn adjust_points(&self, input: AdjustPoints) -> Result<LoyaltyTransaction> {
self.db.loyalty_programs().adjust_points(input)
}
/// Get transaction history for an account.
pub fn get_transactions(
&self,
account_id: LoyaltyAccountId,
limit: Option<u32>,
) -> Result<Vec<LoyaltyTransaction>> {
self.db.loyalty_programs().get_transactions(account_id, limit)
}
// ========================================================================
// Reward Catalog Operations
// ========================================================================
/// Create a new reward in the catalog.
pub fn create_reward(&self, input: CreateReward) -> Result<Reward> {
self.db.rewards().create(input)
}
/// Get a reward by ID.
pub fn get_reward(&self, id: RewardId) -> Result<Option<Reward>> {
self.db.rewards().get(id)
}
/// List rewards with optional filtering.
pub fn list_rewards(&self, filter: RewardFilter) -> Result<Vec<Reward>> {
self.db.rewards().list(filter)
}
/// Delete a reward from the catalog.
pub fn delete_reward(&self, id: RewardId) -> Result<()> {
self.db.rewards().delete(id)
}
}