stateset-db 1.22.0

Database implementations 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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! SQLite implementation of store credit repository

use super::{
    map_db_error, parse_datetime_row, parse_decimal_row, parse_enum_row, parse_uuid_row,
    with_immediate_transaction,
};
use chrono::Utc;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rust_decimal::Decimal;
use stateset_core::{
    AdjustStoreCredit, CommerceError, CreateStoreCredit, Result, StoreCredit, StoreCreditFilter,
    StoreCreditId, StoreCreditRepository, StoreCreditStatus, StoreCreditTransaction,
    StoreCreditTransactionId,
};

#[derive(Debug)]
pub struct SqliteStoreCreditRepository {
    pool: Pool<SqliteConnectionManager>,
}

impl SqliteStoreCreditRepository {
    #[must_use]
    pub const fn new(pool: Pool<SqliteConnectionManager>) -> Self {
        Self { pool }
    }

    fn conn(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
        self.pool.get().map_err(|e| CommerceError::DatabaseError(e.to_string()))
    }

    fn row_to_store_credit(row: &rusqlite::Row<'_>) -> rusqlite::Result<StoreCredit> {
        Ok(StoreCredit {
            id: parse_uuid_row(&row.get::<_, String>("id")?, "store_credit", "id")?.into(),
            customer_id: parse_uuid_row(
                &row.get::<_, String>("customer_id")?,
                "store_credit",
                "customer_id",
            )?
            .into(),
            original_balance: parse_decimal_row(
                &row.get::<_, String>("original_balance")?,
                "store_credit",
                "original_balance",
            )?,
            current_balance: parse_decimal_row(
                &row.get::<_, String>("current_balance")?,
                "store_credit",
                "current_balance",
            )?,
            currency: parse_enum_row(
                &row.get::<_, String>("currency")?,
                "store_credit",
                "currency",
            )?,
            status: parse_enum_row(&row.get::<_, String>("status")?, "store_credit", "status")?,
            reason: parse_enum_row(&row.get::<_, String>("reason")?, "store_credit", "reason")?,
            reference_id: row.get("reference_id")?,
            note: row.get("note")?,
            expires_at: {
                let raw: Option<String> = row.get("expires_at")?;
                match raw {
                    Some(ref s) if !s.is_empty() => {
                        Some(parse_datetime_row(s, "store_credit", "expires_at")?)
                    }
                    _ => None,
                }
            },
            created_at: parse_datetime_row(
                &row.get::<_, String>("created_at")?,
                "store_credit",
                "created_at",
            )?,
            updated_at: parse_datetime_row(
                &row.get::<_, String>("updated_at")?,
                "store_credit",
                "updated_at",
            )?,
        })
    }

    fn row_to_transaction(row: &rusqlite::Row<'_>) -> rusqlite::Result<StoreCreditTransaction> {
        Ok(StoreCreditTransaction {
            id: parse_uuid_row(&row.get::<_, String>("id")?, "store_credit_txn", "id")?.into(),
            store_credit_id: parse_uuid_row(
                &row.get::<_, String>("store_credit_id")?,
                "store_credit_txn",
                "store_credit_id",
            )?
            .into(),
            amount: parse_decimal_row(
                &row.get::<_, String>("amount")?,
                "store_credit_txn",
                "amount",
            )?,
            balance_after: parse_decimal_row(
                &row.get::<_, String>("balance_after")?,
                "store_credit_txn",
                "balance_after",
            )?,
            transaction_type: parse_enum_row(
                &row.get::<_, String>("transaction_type")?,
                "store_credit_txn",
                "transaction_type",
            )?,
            reference_id: row.get("reference_id")?,
            created_at: parse_datetime_row(
                &row.get::<_, String>("created_at")?,
                "store_credit_txn",
                "created_at",
            )?,
        })
    }
}

impl StoreCreditRepository for SqliteStoreCreditRepository {
    fn create(&self, input: CreateStoreCredit) -> Result<StoreCredit> {
        // Reject non-positive issuance up front (the Postgres schema enforces this
        // with a CHECK; SQLite has no such constraint and would otherwise mint a
        // zero/negative-balance credit plus a bogus negative issue transaction).
        if input.amount <= Decimal::ZERO {
            return Err(CommerceError::ValidationError(
                "Store credit amount must be positive".to_string(),
            ));
        }

        let id = StoreCreditId::new();
        let now = Utc::now();
        let id_str = id.to_string();
        let now_str = now.to_rfc3339();

        with_immediate_transaction(&self.pool, |tx| {
            tx.execute(
                "INSERT INTO store_credits (id, customer_id, original_balance, current_balance, currency, status, reason, reference_id, note, expires_at, created_at, updated_at)
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                rusqlite::params![
                    &id_str,
                    input.customer_id.to_string(),
                    input.amount.to_string(),
                    input.amount.to_string(),
                    input.currency.to_string(),
                    "active",
                    input.reason.to_string(),
                    &input.reference_id,
                    &input.note,
                    input.expires_at.map(|dt| dt.to_rfc3339()),
                    &now_str,
                    &now_str,
                ],
            )?;

            // Record the initial issue transaction
            let txn_id = StoreCreditTransactionId::new();
            tx.execute(
                "INSERT INTO store_credit_transactions (id, store_credit_id, amount, balance_after, transaction_type, reference_id, created_at)
                 VALUES (?, ?, ?, ?, ?, ?, ?)",
                rusqlite::params![
                    txn_id.to_string(),
                    &id_str,
                    input.amount.to_string(),
                    input.amount.to_string(),
                    "issue",
                    &input.reference_id,
                    &now_str,
                ],
            )?;

            tx.query_row(
                "SELECT * FROM store_credits WHERE id = ?",
                [&id_str],
                Self::row_to_store_credit,
            )
        })
    }

    fn get(&self, id: StoreCreditId) -> Result<Option<StoreCredit>> {
        let conn = self.conn()?;
        match conn.query_row(
            "SELECT * FROM store_credits WHERE id = ?",
            [id.to_string()],
            Self::row_to_store_credit,
        ) {
            Ok(sc) => Ok(Some(sc)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(map_db_error(e)),
        }
    }

    fn list(&self, filter: StoreCreditFilter) -> Result<Vec<StoreCredit>> {
        let conn = self.conn()?;
        let mut sql = "SELECT * FROM store_credits WHERE 1=1".to_string();
        let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = vec![];

        if let Some(customer_id) = filter.customer_id {
            sql.push_str(" AND customer_id = ?");
            params.push(Box::new(customer_id.to_string()));
        }
        if let Some(status) = filter.status {
            sql.push_str(" AND status = ?");
            params.push(Box::new(status.to_string()));
        }
        if let Some(reason) = filter.reason {
            sql.push_str(" AND reason = ?");
            params.push(Box::new(reason.to_string()));
        }

        sql.push_str(" ORDER BY created_at DESC");

        // SQLite rejects OFFSET without LIMIT, so use `LIMIT -1` (unbounded) when
        // only an offset is set.
        match (filter.limit, filter.offset) {
            (Some(limit), Some(offset)) => sql.push_str(&format!(" LIMIT {limit} OFFSET {offset}")),
            (Some(limit), None) => sql.push_str(&format!(" LIMIT {limit}")),
            (None, Some(offset)) => sql.push_str(&format!(" LIMIT -1 OFFSET {offset}")),
            (None, None) => {}
        }

        let param_refs: Vec<&dyn rusqlite::types::ToSql> =
            params.iter().map(|p| p.as_ref()).collect();
        let mut stmt = conn.prepare(&sql).map_err(map_db_error)?;
        let rows = stmt
            .query_map(param_refs.as_slice(), Self::row_to_store_credit)
            .map_err(map_db_error)?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(map_db_error)?;
        Ok(rows)
    }

    fn adjust(&self, id: StoreCreditId, input: AdjustStoreCredit) -> Result<StoreCredit> {
        let id_str = id.to_string();
        let now_str = Utc::now().to_rfc3339();

        with_immediate_transaction(&self.pool, |tx| {
            let (current_balance_str, status_str): (String, String) = tx.query_row(
                "SELECT current_balance, status FROM store_credits WHERE id = ?",
                [&id_str],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )?;

            let status: StoreCreditStatus = parse_enum_row(&status_str, "store_credit", "status")?;
            if matches!(status, StoreCreditStatus::Voided | StoreCreditStatus::Expired) {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::ValidationError(
                        "Cannot adjust a voided or expired store credit".to_string(),
                    ),
                )));
            }

            let current_balance =
                parse_decimal_row(&current_balance_str, "store_credit", "current_balance")?;
            let new_balance = current_balance + input.amount;

            if new_balance < Decimal::ZERO {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::ValidationError(
                        "Adjustment would result in negative balance".to_string(),
                    ),
                )));
            }

            let status = if new_balance == Decimal::ZERO { "depleted" } else { "active" };

            tx.execute(
                "UPDATE store_credits SET current_balance = ?, status = ?, updated_at = ? WHERE id = ?",
                rusqlite::params![new_balance.to_string(), status, &now_str, &id_str],
            )?;

            let txn_id = StoreCreditTransactionId::new();
            tx.execute(
                "INSERT INTO store_credit_transactions (id, store_credit_id, amount, balance_after, transaction_type, reference_id, created_at)
                 VALUES (?, ?, ?, ?, ?, ?, ?)",
                rusqlite::params![
                    txn_id.to_string(),
                    &id_str,
                    input.amount.to_string(),
                    new_balance.to_string(),
                    "adjust",
                    &input.reference_id,
                    &now_str,
                ],
            )?;

            tx.query_row(
                "SELECT * FROM store_credits WHERE id = ?",
                [&id_str],
                Self::row_to_store_credit,
            )
        })
    }

    fn apply(
        &self,
        id: StoreCreditId,
        amount: Decimal,
        reference_id: Option<String>,
    ) -> Result<StoreCreditTransaction> {
        if amount <= Decimal::ZERO {
            return Err(CommerceError::ValidationError(
                "Apply amount must be positive".to_string(),
            ));
        }

        let id_str = id.to_string();
        let now = Utc::now();
        let now_str = now.to_rfc3339();

        with_immediate_transaction(&self.pool, |tx| {
            let (current_balance_str, status_str, expires_at_raw): (
                String,
                String,
                Option<String>,
            ) = tx.query_row(
                "SELECT current_balance, status, expires_at FROM store_credits WHERE id = ?",
                [&id_str],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
            )?;

            let status: StoreCreditStatus = parse_enum_row(&status_str, "store_credit", "status")?;
            if status != StoreCreditStatus::Active {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::ValidationError("Store credit is not active".to_string()),
                )));
            }

            let expires_at = match expires_at_raw {
                Some(s) if !s.is_empty() => {
                    Some(parse_datetime_row(&s, "store_credit", "expires_at")?)
                }
                _ => None,
            };
            if expires_at.is_some_and(|exp| exp < now) {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::ValidationError("Store credit has expired".to_string()),
                )));
            }

            let current_balance =
                parse_decimal_row(&current_balance_str, "store_credit", "current_balance")?;

            if current_balance < amount {
                return Err(rusqlite::Error::ToSqlConversionFailure(Box::new(
                    CommerceError::ValidationError("Insufficient store credit balance".to_string()),
                )));
            }

            let new_balance = current_balance - amount;
            let status = if new_balance == Decimal::ZERO { "depleted" } else { "active" };

            tx.execute(
                "UPDATE store_credits SET current_balance = ?, status = ?, updated_at = ? WHERE id = ?",
                rusqlite::params![new_balance.to_string(), status, &now_str, &id_str],
            )?;

            let txn_id = StoreCreditTransactionId::new();
            let txn_id_str = txn_id.to_string();
            let debit_amount = -amount;

            tx.execute(
                "INSERT INTO store_credit_transactions (id, store_credit_id, amount, balance_after, transaction_type, reference_id, created_at)
                 VALUES (?, ?, ?, ?, ?, ?, ?)",
                rusqlite::params![
                    &txn_id_str,
                    &id_str,
                    debit_amount.to_string(),
                    new_balance.to_string(),
                    "apply",
                    &reference_id,
                    &now_str,
                ],
            )?;

            tx.query_row(
                "SELECT * FROM store_credit_transactions WHERE id = ?",
                [&txn_id_str],
                Self::row_to_transaction,
            )
        })
    }

    fn get_transactions(
        &self,
        store_credit_id: StoreCreditId,
    ) -> Result<Vec<StoreCreditTransaction>> {
        let conn = self.conn()?;
        let mut stmt = conn
            .prepare(
                "SELECT * FROM store_credit_transactions WHERE store_credit_id = ? ORDER BY created_at DESC",
            )
            .map_err(map_db_error)?;
        let rows = stmt
            .query_map([store_credit_id.to_string()], Self::row_to_transaction)
            .map_err(map_db_error)?
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(map_db_error)?;
        Ok(rows)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DatabaseConfig;
    use crate::sqlite::SqliteDatabase;
    use rust_decimal_macros::dec;
    use stateset_core::{CurrencyCode, CustomerId, StoreCreditReason, StoreCreditTransactionType};

    fn test_db() -> SqliteDatabase {
        let db = SqliteDatabase::new(&DatabaseConfig::in_memory()).expect("in-memory db");
        let conn = db.conn().expect("conn");
        conn.execute_batch(
            "CREATE TABLE IF NOT EXISTS store_credits (
                id TEXT PRIMARY KEY,
                customer_id TEXT NOT NULL,
                original_balance TEXT NOT NULL,
                current_balance TEXT NOT NULL,
                currency TEXT NOT NULL DEFAULT 'USD',
                status TEXT NOT NULL DEFAULT 'active',
                reason TEXT NOT NULL DEFAULT 'return',
                reference_id TEXT,
                note TEXT,
                expires_at TEXT,
                created_at TEXT NOT NULL DEFAULT (datetime('now')),
                updated_at TEXT NOT NULL DEFAULT (datetime('now'))
            );
            CREATE TABLE IF NOT EXISTS store_credit_transactions (
                id TEXT PRIMARY KEY,
                store_credit_id TEXT NOT NULL,
                amount TEXT NOT NULL,
                balance_after TEXT NOT NULL,
                transaction_type TEXT NOT NULL,
                reference_id TEXT,
                created_at TEXT NOT NULL DEFAULT (datetime('now')),
                FOREIGN KEY (store_credit_id) REFERENCES store_credits(id)
            );",
        )
        .expect("create tables");
        db
    }

    fn test_repo() -> SqliteStoreCreditRepository {
        SqliteStoreCreditRepository::new(test_db().pool().clone())
    }

    fn create_credit(repo: &SqliteStoreCreditRepository, amount: Decimal) -> StoreCredit {
        repo.create(CreateStoreCredit {
            customer_id: CustomerId::new(),
            amount,
            currency: CurrencyCode::USD,
            reason: StoreCreditReason::Return,
            reference_id: None,
            note: None,
            expires_at: None,
        })
        .expect("create")
    }

    #[test]
    fn create_rejects_non_positive_amount() {
        let repo = test_repo();
        for bad in [dec!(-50.00), dec!(0)] {
            let err = repo
                .create(CreateStoreCredit {
                    customer_id: CustomerId::new(),
                    amount: bad,
                    currency: CurrencyCode::USD,
                    reason: StoreCreditReason::Return,
                    reference_id: None,
                    note: None,
                    expires_at: None,
                })
                .expect_err("non-positive store credit amount must be rejected");
            assert!(matches!(err, CommerceError::ValidationError(_)), "amount {bad}: got {err:?}");
        }
    }

    #[test]
    fn create_and_get_store_credit() {
        let repo = test_repo();
        let customer_id = CustomerId::new();
        let sc = repo
            .create(CreateStoreCredit {
                customer_id,
                amount: dec!(50.00),
                currency: CurrencyCode::USD,
                reason: StoreCreditReason::Return,
                reference_id: Some("RET-001".into()),
                note: None,
                expires_at: None,
            })
            .expect("create");

        assert_eq!(sc.original_balance, dec!(50.00));
        assert_eq!(sc.current_balance, dec!(50.00));
        assert_eq!(sc.customer_id, customer_id);

        let fetched = repo.get(sc.id).expect("get").expect("found");
        assert_eq!(fetched.id, sc.id);
        assert_eq!(fetched.original_balance, dec!(50.00));
    }

    #[test]
    fn apply_credit_and_get_transactions() {
        let repo = test_repo();
        let sc = repo
            .create(CreateStoreCredit {
                customer_id: CustomerId::new(),
                amount: dec!(100.00),
                currency: CurrencyCode::USD,
                reason: StoreCreditReason::Compensation,
                reference_id: None,
                note: Some("Compensation credit".into()),
                expires_at: None,
            })
            .expect("create");

        let txn = repo.apply(sc.id, dec!(30.00), Some("ORD-123".into())).expect("apply");
        assert_eq!(txn.amount, dec!(-30.00));
        assert_eq!(txn.balance_after, dec!(70.00));
        assert_eq!(txn.transaction_type, StoreCreditTransactionType::Apply);

        let updated = repo.get(sc.id).expect("get").expect("found");
        assert_eq!(updated.current_balance, dec!(70.00));

        let txns = repo.get_transactions(sc.id).expect("transactions");
        // Should have initial issue + apply = 2 transactions
        assert_eq!(txns.len(), 2);
    }

    #[test]
    fn concurrent_applies_cannot_overspend() {
        use std::sync::{Arc, Barrier};
        use std::thread;

        let db = Arc::new(test_db());
        let repo = SqliteStoreCreditRepository::new(db.pool().clone());
        let sc = create_credit(&repo, dec!(50.00));

        let thread_count = 10;
        let barrier = Arc::new(Barrier::new(thread_count));
        let mut handles = Vec::new();
        for _ in 0..thread_count {
            let db = Arc::clone(&db);
            let barrier = Arc::clone(&barrier);
            let credit_id = sc.id;
            handles.push(thread::spawn(move || {
                let repo = SqliteStoreCreditRepository::new(db.pool().clone());
                barrier.wait();
                repo.apply(credit_id, dec!(30.00), None)
            }));
        }

        let results: Vec<_> = handles.into_iter().map(|h| h.join().expect("thread")).collect();
        let successes = results.iter().filter(|r| r.is_ok()).count();
        // The $50 credit can fund at most one $30 apply — the safety invariant is
        // that no more than one succeeds (never overspent). Under extreme lock
        // contention the sole winner can fail with a retryable "table is locked"
        // error, so zero successes is acceptable; two or more would be a bug.
        assert!(successes <= 1, "store credit overspent under concurrency: {results:?}");

        let fetched = repo.get(sc.id).expect("get").expect("found");
        assert_eq!(
            fetched.current_balance,
            dec!(50.00) - dec!(30.00) * Decimal::from(successes as u64),
            "balance must reflect exactly the successful apply: {results:?}"
        );
    }

    #[test]
    fn apply_rejects_nonpositive_amount() {
        let repo = test_repo();
        let sc = create_credit(&repo, dec!(50.00));

        // A negative apply must not mint balance.
        assert!(repo.apply(sc.id, Decimal::ZERO, None).is_err());
        assert!(repo.apply(sc.id, dec!(-10.00), None).is_err());

        let fetched = repo.get(sc.id).expect("get").expect("found");
        assert_eq!(fetched.current_balance, dec!(50.00));
    }

    #[test]
    fn apply_rejects_date_expired_credit() {
        let repo = test_repo();
        let sc = repo
            .create(CreateStoreCredit {
                customer_id: CustomerId::new(),
                amount: dec!(50.00),
                currency: CurrencyCode::USD,
                reason: StoreCreditReason::Return,
                reference_id: None,
                note: None,
                expires_at: Some(Utc::now() - chrono::Duration::days(1)),
            })
            .expect("create");

        // Status is still 'active' — only the expiry date has passed.
        assert!(repo.apply(sc.id, dec!(10.00), None).is_err());

        let fetched = repo.get(sc.id).expect("get").expect("found");
        assert_eq!(fetched.current_balance, dec!(50.00));
    }

    #[test]
    fn apply_rejects_voided_credit() {
        let db = test_db();
        let repo = SqliteStoreCreditRepository::new(db.pool().clone());
        let sc = create_credit(&repo, dec!(50.00));

        db.conn()
            .expect("conn")
            .execute("UPDATE store_credits SET status = 'voided' WHERE id = ?", [sc.id.to_string()])
            .expect("void");

        assert!(repo.apply(sc.id, dec!(10.00), None).is_err());

        let fetched = repo.get(sc.id).expect("get").expect("found");
        assert_eq!(fetched.current_balance, dec!(50.00));
    }

    #[test]
    fn adjust_rejects_voided_credit() {
        let db = test_db();
        let repo = SqliteStoreCreditRepository::new(db.pool().clone());
        let sc = create_credit(&repo, dec!(50.00));

        db.conn()
            .expect("conn")
            .execute("UPDATE store_credits SET status = 'voided' WHERE id = ?", [sc.id.to_string()])
            .expect("void");

        // Adjusting a voided credit must not silently resurrect it.
        assert!(
            repo.adjust(
                sc.id,
                AdjustStoreCredit { amount: dec!(10.00), note: None, reference_id: None }
            )
            .is_err()
        );

        let fetched = repo.get(sc.id).expect("get").expect("found");
        assert_eq!(fetched.status.to_string(), "voided");
        assert_eq!(fetched.current_balance, dec!(50.00));
    }
}