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
//! Gift card operations for issuing, charging, and refunding gift cards
//!
//! # Example
//!
//! ```rust,ignore
//! use stateset_embedded::{Commerce, CreateGiftCard, CustomerId};
//! use rust_decimal_macros::dec;
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! let gift_card = commerce.gift_cards().create(CreateGiftCard {
//! initial_balance: dec!(50.00),
//! customer_id: Some(CustomerId::new()),
//! ..Default::default()
//! })?;
//!
//! println!("Gift card code: {}", gift_card.code);
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use rust_decimal::Decimal;
use stateset_core::{
CreateGiftCard, GiftCard, GiftCardFilter, GiftCardId, GiftCardTransaction, Result,
UpdateGiftCard,
};
use stateset_db::{Database, DatabaseCapability};
use std::sync::Arc;
/// Gift card operations for issuing, charging, and refunding.
pub struct GiftCards {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for GiftCards {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GiftCards").finish_non_exhaustive()
}
}
impl GiftCards {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
/// Whether gift cards are supported by the active backend.
#[must_use]
pub fn is_supported(&self) -> bool {
self.db.supports_capability(DatabaseCapability::GiftCards)
}
fn ensure_supported(&self) -> Result<()> {
self.db.ensure_capability(DatabaseCapability::GiftCards)
}
/// Create a new gift card.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, CreateGiftCard, CustomerId};
/// use rust_decimal_macros::dec;
///
/// let commerce = Commerce::new("./store.db")?;
///
/// let gift_card = commerce.gift_cards().create(CreateGiftCard {
/// initial_balance: dec!(100.00),
/// customer_id: Some(CustomerId::new()),
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create(&self, input: CreateGiftCard) -> Result<GiftCard> {
self.ensure_supported()?;
self.db.gift_cards().create(input)
}
/// Get a gift card by ID.
pub fn get(&self, id: GiftCardId) -> Result<Option<GiftCard>> {
self.ensure_supported()?;
self.db.gift_cards().get(id)
}
/// Get a gift card by its unique code.
pub fn get_by_code(&self, code: &str) -> Result<Option<GiftCard>> {
self.ensure_supported()?;
self.db.gift_cards().get_by_code(code)
}
/// Update a gift card.
pub fn update(&self, id: GiftCardId, input: UpdateGiftCard) -> Result<GiftCard> {
self.ensure_supported()?;
self.db.gift_cards().update(id, input)
}
/// List gift cards with optional filtering.
pub fn list(&self, filter: GiftCardFilter) -> Result<Vec<GiftCard>> {
self.ensure_supported()?;
self.db.gift_cards().list(filter)
}
/// Charge (debit) a gift card.
///
/// Reduces the gift card balance by the specified amount.
pub fn charge(
&self,
id: GiftCardId,
amount: Decimal,
reference_id: Option<String>,
) -> Result<GiftCardTransaction> {
self.ensure_supported()?;
self.db.gift_cards().charge(id, amount, reference_id)
}
/// Refund (credit) to a gift card.
///
/// Increases the gift card balance by the specified amount.
pub fn refund(
&self,
id: GiftCardId,
amount: Decimal,
reference_id: Option<String>,
) -> Result<GiftCardTransaction> {
self.ensure_supported()?;
self.db.gift_cards().refund(id, amount, reference_id)
}
/// Disable a gift card so it can no longer be used.
pub fn disable(&self, id: GiftCardId) -> Result<GiftCard> {
self.ensure_supported()?;
self.db.gift_cards().disable(id)
}
/// Get transaction history for a gift card.
pub fn get_transactions(&self, gift_card_id: GiftCardId) -> Result<Vec<GiftCardTransaction>> {
self.ensure_supported()?;
self.db.gift_cards().get_transactions(gift_card_id)
}
}