stateset-http 1.22.0

HTTP service layer (REST + SSE) for the StateSet commerce engine
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
//! Gift card endpoints.

use axum::{
    Json, Router,
    extract::{Path, Query, State},
    http::{HeaderMap, StatusCode},
    routing::{get, post},
};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use utoipa::{IntoParams, ToSchema};

use crate::error::{ErrorBody, HttpError};
use crate::state::{AppState, tenant_id_from_headers};

// ============================================================================
// DTOs
// ============================================================================

/// Request body for `POST /api/v1/gift-cards`.
#[derive(Debug, Clone, Deserialize, Serialize, ToSchema)]
pub(crate) struct CreateGiftCardRequest {
    /// Initial balance.
    pub initial_balance: f64,
    /// ISO currency code (default: USD).
    pub currency: Option<String>,
    /// Optional recipient email.
    pub recipient_email: Option<String>,
    /// Optional gift message.
    pub message: Option<String>,
    /// Optional expiration date (RFC 3339).
    pub expires_at: Option<String>,
}

/// Query parameters for `GET /api/v1/gift-cards` with filtering.
#[derive(Debug, Clone, Deserialize, Default, IntoParams)]
#[into_params(parameter_in = Query)]
pub(crate) struct GiftCardFilterParams {
    /// Maximum number of results to return (default: 50).
    pub limit: Option<u32>,
    /// Number of results to skip (default: 0).
    pub offset: Option<u32>,
    /// Filter by gift card status (active, disabled, expired, depleted).
    pub status: Option<String>,
}

impl GiftCardFilterParams {
    /// Default page size.
    const DEFAULT_LIMIT: u32 = 50;
    /// Maximum allowed page size.
    const MAX_LIMIT: u32 = 200;

    /// Resolved limit with bounds checking.
    #[must_use]
    pub(crate) fn resolved_limit(&self) -> u32 {
        self.limit.unwrap_or(Self::DEFAULT_LIMIT).clamp(1, Self::MAX_LIMIT)
    }

    /// Resolved offset.
    #[must_use]
    pub(crate) fn resolved_offset(&self) -> u32 {
        self.offset.unwrap_or(0)
    }
}

/// Response body for a single gift card.
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub(crate) struct GiftCardResponse {
    #[schema(value_type = String, format = "uuid")]
    pub id: String,
    pub code: String,
    #[schema(value_type = String)]
    pub initial_balance: Decimal,
    #[schema(value_type = String)]
    pub current_balance: Decimal,
    pub currency: String,
    pub status: String,
    pub recipient_email: Option<String>,
    pub message: Option<String>,
    pub expires_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Request body for `POST /api/v1/gift-cards/{id}/charge` and `/refund`.
#[derive(Debug, Clone, Deserialize, Serialize, ToSchema)]
pub(crate) struct GiftCardAmountRequest {
    /// Amount to charge (debit) or refund (credit). Must be positive.
    #[schema(value_type = String)]
    pub amount: Decimal,
    /// Optional reference (e.g. order ID) recorded on the transaction.
    pub reference_id: Option<String>,
}

/// A gift card ledger transaction.
#[derive(Debug, Clone, Serialize, ToSchema)]
pub(crate) struct GiftCardTransactionResponse {
    pub id: String,
    pub gift_card_id: String,
    #[schema(value_type = String)]
    pub amount: Decimal,
    #[schema(value_type = String)]
    pub balance_after: Decimal,
    pub transaction_type: String,
    pub reference_id: Option<String>,
    pub created_at: DateTime<Utc>,
}

/// Response body for `GET /api/v1/gift-cards` (list).
#[derive(Debug, Clone, Serialize, ToSchema)]
pub(crate) struct GiftCardListResponse {
    pub gift_cards: Vec<GiftCardResponse>,
    pub total: usize,
    pub limit: u32,
    pub offset: u32,
    pub has_more: bool,
}

// ============================================================================
// Router
// ============================================================================

/// Build the gift-cards sub-router.
pub fn router() -> Router<AppState> {
    Router::new()
        .route("/gift-cards", post(create_gift_card).get(list_gift_cards))
        .route("/gift-cards/{id}", get(get_gift_card))
        .route("/gift-cards/{id}/charge", post(charge_gift_card))
        .route("/gift-cards/{id}/refund", post(refund_gift_card))
        .route("/gift-cards/{id}/disable", post(disable_gift_card))
}

// ============================================================================
// Handlers
// ============================================================================

/// `POST /api/v1/gift-cards`
#[utoipa::path(
    post,
    path = "/api/v1/gift-cards",
    tag = "gift_cards",
    request_body = CreateGiftCardRequest,
    responses(
        (status = 201, description = "Gift card created", body = GiftCardResponse),
        (status = 400, description = "Invalid request", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers, req))]
pub(crate) async fn create_gift_card(
    State(state): State<AppState>,
    headers: HeaderMap,
    Json(req): Json<CreateGiftCardRequest>,
) -> Result<(StatusCode, Json<GiftCardResponse>), HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let commerce = state.commerce_for_tenant(tenant_id.as_deref())?;

    if req.initial_balance <= 0.0 {
        return Err(HttpError::BadRequest("initial_balance must be positive".to_string()));
    }

    let currency = req
        .currency
        .map(|s| s.parse::<stateset_primitives::CurrencyCode>())
        .transpose()
        .map_err(|e| HttpError::BadRequest(format!("Invalid currency: {e}")))?
        .unwrap_or(stateset_primitives::CurrencyCode::USD);

    let expires_at = req
        .expires_at
        .map(|s| {
            s.parse::<chrono::DateTime<Utc>>()
                .map_err(|e| HttpError::BadRequest(format!("Invalid expires_at: {e}")))
        })
        .transpose()?;

    let gift_card = commerce.gift_cards().create(stateset_core::CreateGiftCard {
        code: None,
        initial_balance: Decimal::try_from(req.initial_balance)
            .map_err(|e| HttpError::BadRequest(format!("Invalid initial_balance: {e}")))?,
        currency,
        recipient_email: req.recipient_email,
        sender_name: None,
        message: req.message,
        expires_at,
    })?;

    Ok((StatusCode::CREATED, Json(gift_card_to_response(gift_card))))
}

/// `GET /api/v1/gift-cards/{id}`
#[utoipa::path(
    get,
    path = "/api/v1/gift-cards/{id}",
    tag = "gift_cards",
    params(("id" = String, Path, description = "Gift card ID (UUID)")),
    responses(
        (status = 200, description = "Gift card details", body = GiftCardResponse),
        (status = 404, description = "Gift card not found", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers))]
pub(crate) async fn get_gift_card(
    State(state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<stateset_primitives::GiftCardId>,
) -> Result<Json<GiftCardResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let commerce = state.commerce_for_tenant(tenant_id.as_deref())?;
    let gift_card = commerce
        .gift_cards()
        .get(id)?
        .ok_or_else(|| HttpError::NotFound(format!("Gift card {id} not found")))?;
    Ok(Json(gift_card_to_response(gift_card)))
}

/// `GET /api/v1/gift-cards`
#[utoipa::path(
    get,
    path = "/api/v1/gift-cards",
    tag = "gift_cards",
    params(GiftCardFilterParams),
    responses(
        (status = 200, description = "List of gift cards", body = GiftCardListResponse),
        (status = 400, description = "Invalid filter parameter", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers, params))]
pub(crate) async fn list_gift_cards(
    State(state): State<AppState>,
    headers: HeaderMap,
    Query(params): Query<GiftCardFilterParams>,
) -> Result<Json<GiftCardListResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let commerce = state.commerce_for_tenant(tenant_id.as_deref())?;

    let limit = params.resolved_limit();
    let offset = params.resolved_offset();

    let status = params
        .status
        .map(|s| s.parse::<stateset_core::GiftCardStatus>())
        .transpose()
        .map_err(|e| HttpError::BadRequest(format!("Invalid status: {e}")))?;

    // Count total matching records (without pagination)
    let count_filter =
        stateset_core::GiftCardFilter { status, code: None, limit: None, offset: None };
    let total = commerce.gift_cards().list(count_filter)?.len();

    // Fetch the requested page
    let filter = stateset_core::GiftCardFilter {
        status,
        code: None,
        limit: Some(limit.saturating_add(1)),
        offset: Some(offset),
    };
    let mut gift_cards = commerce.gift_cards().list(filter)?;
    let has_more = gift_cards.len() > limit as usize;
    if has_more {
        gift_cards.truncate(limit as usize);
    }

    Ok(Json(GiftCardListResponse {
        gift_cards: gift_cards.into_iter().map(gift_card_to_response).collect(),
        total,
        limit,
        offset,
        has_more,
    }))
}

/// `POST /api/v1/gift-cards/{id}/charge`
#[utoipa::path(
    post,
    path = "/api/v1/gift-cards/{id}/charge",
    tag = "gift_cards",
    params(("id" = String, Path, description = "Gift card ID (UUID)")),
    request_body = GiftCardAmountRequest,
    responses(
        (status = 200, description = "Gift card charged", body = GiftCardTransactionResponse),
        (status = 422, description = "Invalid amount, inactive or expired card, or insufficient balance", body = ErrorBody),
        (status = 404, description = "Gift card not found", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers, req))]
pub(crate) async fn charge_gift_card(
    State(state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<stateset_primitives::GiftCardId>,
    Json(req): Json<GiftCardAmountRequest>,
) -> Result<Json<GiftCardTransactionResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let commerce = state.commerce_for_tenant(tenant_id.as_deref())?;
    let txn = commerce.gift_cards().charge(id, req.amount, req.reference_id)?;
    Ok(Json(transaction_to_response(txn)))
}

/// `POST /api/v1/gift-cards/{id}/refund`
#[utoipa::path(
    post,
    path = "/api/v1/gift-cards/{id}/refund",
    tag = "gift_cards",
    params(("id" = String, Path, description = "Gift card ID (UUID)")),
    request_body = GiftCardAmountRequest,
    responses(
        (status = 200, description = "Gift card refunded", body = GiftCardTransactionResponse),
        (status = 422, description = "Invalid amount or disabled card", body = ErrorBody),
        (status = 404, description = "Gift card not found", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers, req))]
pub(crate) async fn refund_gift_card(
    State(state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<stateset_primitives::GiftCardId>,
    Json(req): Json<GiftCardAmountRequest>,
) -> Result<Json<GiftCardTransactionResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let commerce = state.commerce_for_tenant(tenant_id.as_deref())?;
    let txn = commerce.gift_cards().refund(id, req.amount, req.reference_id)?;
    Ok(Json(transaction_to_response(txn)))
}

/// `POST /api/v1/gift-cards/{id}/disable`
#[utoipa::path(
    post,
    path = "/api/v1/gift-cards/{id}/disable",
    tag = "gift_cards",
    params(("id" = String, Path, description = "Gift card ID (UUID)")),
    responses(
        (status = 200, description = "Gift card disabled", body = GiftCardResponse),
        (status = 404, description = "Gift card not found", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers))]
pub(crate) async fn disable_gift_card(
    State(state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<stateset_primitives::GiftCardId>,
) -> Result<Json<GiftCardResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let commerce = state.commerce_for_tenant(tenant_id.as_deref())?;
    let gift_card = commerce.gift_cards().disable(id)?;
    Ok(Json(gift_card_to_response(gift_card)))
}

// ============================================================================
// Helpers
// ============================================================================

fn transaction_to_response(txn: stateset_core::GiftCardTransaction) -> GiftCardTransactionResponse {
    GiftCardTransactionResponse {
        id: txn.id.to_string(),
        gift_card_id: txn.gift_card_id.to_string(),
        amount: txn.amount,
        balance_after: txn.balance_after,
        transaction_type: txn.transaction_type.to_string(),
        reference_id: txn.reference_id,
        created_at: txn.created_at,
    }
}

fn gift_card_to_response(gc: stateset_core::GiftCard) -> GiftCardResponse {
    GiftCardResponse {
        id: gc.id.to_string(),
        code: gc.code,
        initial_balance: gc.initial_balance,
        current_balance: gc.current_balance,
        currency: gc.currency.to_string(),
        status: gc.status.to_string(),
        recipient_email: gc.recipient_email,
        message: gc.message,
        expires_at: gc.expires_at,
        created_at: gc.created_at,
        updated_at: gc.updated_at,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::Request;
    use stateset_embedded::Commerce;
    use tower::ServiceExt;

    fn app() -> Router {
        let state = AppState::new(Commerce::new(":memory:").expect("in-memory Commerce"));
        router().with_state(state)
    }

    async fn post_json(
        app: &Router,
        uri: &str,
        body: serde_json::Value,
    ) -> (StatusCode, serde_json::Value) {
        let resp = app
            .clone()
            .oneshot(
                Request::post(uri)
                    .header("content-type", "application/json")
                    .body(Body::from(body.to_string()))
                    .unwrap(),
            )
            .await
            .unwrap();
        let status = resp.status();
        let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        (status, serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null))
    }

    #[tokio::test]
    async fn charge_and_refund_roundtrip() {
        let app = app();
        let (status, card) =
            post_json(&app, "/gift-cards", serde_json::json!({"initial_balance": 50.0})).await;
        assert_eq!(status, StatusCode::CREATED);
        let id = card["id"].as_str().unwrap().to_string();

        let (status, txn) = post_json(
            &app,
            &format!("/gift-cards/{id}/charge"),
            serde_json::json!({"amount": "30.00", "reference_id": "ORD-1"}),
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(txn["balance_after"], "20.00");
        assert_eq!(txn["transaction_type"], "charge");

        let (status, txn) = post_json(
            &app,
            &format!("/gift-cards/{id}/refund"),
            serde_json::json!({"amount": "5.00"}),
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(txn["balance_after"], "25.00");
    }

    #[tokio::test]
    async fn charge_rejects_negative_and_overdraft() {
        let app = app();
        let (_, card) =
            post_json(&app, "/gift-cards", serde_json::json!({"initial_balance": 10.0})).await;
        let id = card["id"].as_str().unwrap().to_string();

        let (status, _) = post_json(
            &app,
            &format!("/gift-cards/{id}/charge"),
            serde_json::json!({"amount": "-5.00"}),
        )
        .await;
        assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);

        let (status, _) = post_json(
            &app,
            &format!("/gift-cards/{id}/charge"),
            serde_json::json!({"amount": "99.00"}),
        )
        .await;
        assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
    }
}