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
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
//! Customer endpoints.

use axum::{
    Json, Router,
    extract::{Path, Query, State},
    http::HeaderMap,
    routing::{get, post},
};

use crate::dto::{
    CreateCustomerRequest, CustomerFilterParams, CustomerListResponse, CustomerResponse,
    UpdateCustomerRequest, decode_cursor, encode_cursor, finalize_page, overfetch_limit,
};
use crate::error::{ErrorBody, HttpError};
use crate::state::{AppState, tenant_id_from_headers};
use stateset_core::{CreateCustomer, CustomerFilter, CustomerId, CustomerStatus, UpdateCustomer};
use std::str::FromStr;

/// Build the customers sub-router.
pub fn router() -> Router<AppState> {
    Router::new()
        .route("/customers", post(create_customer).get(list_customers))
        .route("/customers/{id}", get(get_customer).patch(update_customer).delete(delete_customer))
}

/// `POST /api/v1/customers`
#[utoipa::path(
    post,
    path = "/api/v1/customers",
    tag = "customers",
    request_body = CreateCustomerRequest,
    params(
        ("Idempotency-Key" = Option<String>, Header,
            description = "Optional client-generated key. Replaying the same key with an \
                identical body returns the original response without creating a duplicate; \
                reusing it with a different body returns 422. Scoped per tenant."),
    ),
    responses(
        (status = 201, description = "Customer created", body = CustomerResponse),
        (status = 400, description = "Invalid request", body = ErrorBody),
        (status = 422, description = "Idempotency-Key reused with a different body", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers, req))]
pub(crate) async fn create_customer(
    State(state): State<AppState>,
    headers: HeaderMap,
    Json(req): Json<CreateCustomerRequest>,
) -> Result<(axum::http::StatusCode, Json<CustomerResponse>), HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);

    let input = CreateCustomer {
        email: req.email,
        first_name: req.first_name,
        last_name: req.last_name,
        phone: req.phone,
        accepts_marketing: req.accepts_marketing,
        tags: req.tags,
        metadata: req.metadata,
    };
    let customer = state
        .run_blocking(tenant_id.as_deref(), move |commerce| {
            Ok(commerce.customers().create(input)?)
        })
        .await?;
    Ok((axum::http::StatusCode::CREATED, Json(CustomerResponse::from(customer))))
}

/// `GET /api/v1/customers/:id`
#[utoipa::path(
    get,
    path = "/api/v1/customers/{id}",
    tag = "customers",
    params(("id" = String, Path, description = "Customer ID (UUID)")),
    responses(
        (status = 200, description = "Customer details", body = CustomerResponse),
        (status = 404, description = "Customer not found", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers))]
pub(crate) async fn get_customer(
    State(state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<CustomerId>,
) -> Result<Json<CustomerResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    let customer = state
        .run_blocking(tenant_id.as_deref(), move |commerce| {
            commerce
                .customers()
                .get(id)?
                .ok_or_else(|| HttpError::NotFound(format!("Customer {id} not found")))
        })
        .await?;
    Ok(Json(CustomerResponse::from(customer)))
}

/// `GET /api/v1/customers`
#[utoipa::path(
    get,
    path = "/api/v1/customers",
    tag = "customers",
    params(CustomerFilterParams),
    responses(
        (status = 200, description = "List of customers", body = CustomerListResponse),
        (status = 400, description = "Invalid filter parameter", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers, params))]
pub(crate) async fn list_customers(
    State(state): State<AppState>,
    headers: HeaderMap,
    Query(params): Query<CustomerFilterParams>,
) -> Result<Json<CustomerListResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);

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

    // Parse filter parameters
    let status =
        params.status.as_deref().map(CustomerStatus::from_str).transpose().map_err(|e| {
            HttpError::BadRequest(format!(
                "Invalid status: {e}. Valid values: active, inactive, suspended"
            ))
        })?;

    // Decode cursor if provided
    let after_cursor = match &params.after {
        Some(cursor) => Some(
            decode_cursor(cursor).ok_or_else(|| HttpError::BadRequest("Invalid cursor".into()))?,
        ),
        None => None,
    };

    // Count total matching records (without pagination or cursor) using an
    // efficient `SELECT COUNT(*)` rather than materializing the full result set.
    let count_filter = CustomerFilter {
        email: params.email.clone(),
        status,
        tag: params.tag.clone(),
        accepts_marketing: params.accepts_marketing,
        limit: None,
        offset: None,
        after_cursor: None,
    };
    // Fetch the requested page
    let filter = CustomerFilter {
        email: params.email,
        status,
        tag: params.tag,
        accepts_marketing: params.accepts_marketing,
        limit: Some(overfetch_limit(limit)),
        offset: if after_cursor.is_some() { Some(0) } else { Some(offset) },
        after_cursor,
    };

    let (total, mut customers) = state
        .run_blocking(tenant_id.as_deref(), move |commerce| {
            let total =
                usize::try_from(commerce.customers().count(count_filter)?).unwrap_or(usize::MAX);
            let customers = commerce.customers().list(filter)?;
            Ok((total, customers))
        })
        .await?;

    let has_more = finalize_page(&mut customers, limit);
    let next_cursor = if has_more {
        customers.last().map(|c| encode_cursor(&c.created_at.to_rfc3339(), &c.id.to_string()))
    } else {
        None
    };
    Ok(Json(CustomerListResponse {
        customers: customers.into_iter().map(CustomerResponse::from).collect(),
        total,
        limit,
        offset,
        next_cursor,
        has_more,
    }))
}

/// `PATCH /api/v1/customers/:id`
#[utoipa::path(
    patch,
    path = "/api/v1/customers/{id}",
    tag = "customers",
    params(("id" = String, Path, description = "Customer ID (UUID)")),
    request_body = UpdateCustomerRequest,
    responses(
        (status = 200, description = "Customer updated", body = CustomerResponse),
        (status = 404, description = "Customer not found", body = ErrorBody),
        (status = 400, description = "Invalid request", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers, req))]
pub(crate) async fn update_customer(
    State(state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<CustomerId>,
    Json(req): Json<UpdateCustomerRequest>,
) -> Result<Json<CustomerResponse>, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);

    let status = req.status.as_deref().map(CustomerStatus::from_str).transpose().map_err(|e| {
        HttpError::BadRequest(format!(
            "Invalid status: {e}. Valid values: active, inactive, suspended"
        ))
    })?;

    let input = UpdateCustomer {
        email: req.email,
        first_name: req.first_name,
        last_name: req.last_name,
        phone: req.phone,
        status,
        accepts_marketing: req.accepts_marketing,
        tags: req.tags,
        metadata: req.metadata,
    };
    let customer = state
        .run_blocking(tenant_id.as_deref(), move |commerce| {
            Ok(commerce.customers().update(id, input)?)
        })
        .await?;
    Ok(Json(CustomerResponse::from(customer)))
}

/// `DELETE /api/v1/customers/:id`
#[utoipa::path(
    delete,
    path = "/api/v1/customers/{id}",
    tag = "customers",
    params(("id" = String, Path, description = "Customer ID (UUID)")),
    responses(
        (status = 204, description = "Customer deleted"),
        (status = 404, description = "Customer not found", body = ErrorBody),
    )
)]
#[tracing::instrument(skip(state, headers))]
pub(crate) async fn delete_customer(
    State(state): State<AppState>,
    headers: HeaderMap,
    Path(id): Path<CustomerId>,
) -> Result<axum::http::StatusCode, HttpError> {
    let tenant_id = tenant_id_from_headers(&headers);
    state
        .run_blocking(tenant_id.as_deref(), move |commerce| Ok(commerce.customers().delete(id)?))
        .await?;
    Ok(axum::http::StatusCode::NO_CONTENT)
}

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

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

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

    #[tokio::test]
    async fn create_customer_returns_201() {
        let body = serde_json::json!({
            "email": "alice@example.com",
            "first_name": "Alice",
            "last_name": "Smith"
        });
        let resp = app()
            .oneshot(
                Request::post("/customers")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);

        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["email"], "alice@example.com");
    }

    #[tokio::test]
    async fn get_customer_not_found() {
        let id = CustomerId::new();
        let resp = app()
            .oneshot(Request::get(format!("/customers/{id}")).body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn list_customers_empty() {
        let resp =
            app().oneshot(Request::get("/customers").body(Body::empty()).unwrap()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["total"], 0);
    }

    #[tokio::test]
    async fn create_and_get_customer() {
        let state = AppState::new(Commerce::new(":memory:").expect("in-memory Commerce"));
        let app = router().with_state(state.clone());

        // Create
        let body = serde_json::json!({
            "email": "bob@example.com",
            "first_name": "Bob",
            "last_name": "Jones"
        });
        let resp = app
            .oneshot(
                Request::post("/customers")
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::CREATED);

        let resp_body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let created: serde_json::Value = serde_json::from_slice(&resp_body).unwrap();
        let id = created["id"].as_str().unwrap();

        // Get
        let app2 = router().with_state(state);
        let resp = app2
            .oneshot(Request::get(format!("/customers/{id}")).body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let resp_body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let fetched: serde_json::Value = serde_json::from_slice(&resp_body).unwrap();
        assert_eq!(fetched["email"], "bob@example.com");
    }

    #[tokio::test]
    async fn list_customers_reports_total_before_pagination() {
        let (app, state) = app_with_state();

        for i in 0..2 {
            state
                .commerce()
                .customers()
                .create(stateset_core::CreateCustomer {
                    email: format!("paging-customer-{i}@example.com"),
                    first_name: "Paging".into(),
                    last_name: "Customer".into(),
                    ..Default::default()
                })
                .unwrap();
        }

        let resp = app
            .oneshot(Request::get("/customers?limit=1&offset=0").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["total"], 2);
        assert_eq!(json["customers"].as_array().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn list_customers_filter_by_email() {
        let (app, state) = app_with_state();

        state
            .commerce()
            .customers()
            .create(stateset_core::CreateCustomer {
                email: "alice@filter.com".into(),
                first_name: "Alice".into(),
                last_name: "Filter".into(),
                ..Default::default()
            })
            .unwrap();
        state
            .commerce()
            .customers()
            .create(stateset_core::CreateCustomer {
                email: "bob@filter.com".into(),
                first_name: "Bob".into(),
                last_name: "Filter".into(),
                ..Default::default()
            })
            .unwrap();

        let resp = app
            .oneshot(
                Request::get("/customers?email=alice%40filter.com").body(Body::empty()).unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["total"], 1);
        assert_eq!(json["customers"][0]["email"], "alice@filter.com");
    }

    #[tokio::test]
    async fn list_customers_invalid_status_returns_400() {
        let resp = app()
            .oneshot(Request::get("/customers?status=bogus").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn list_customers_filter_by_accepts_marketing() {
        let (app, state) = app_with_state();

        state
            .commerce()
            .customers()
            .create(stateset_core::CreateCustomer {
                email: "opt-in@test.com".into(),
                first_name: "Opt".into(),
                last_name: "In".into(),
                accepts_marketing: Some(true),
                ..Default::default()
            })
            .unwrap();
        state
            .commerce()
            .customers()
            .create(stateset_core::CreateCustomer {
                email: "opt-out@test.com".into(),
                first_name: "Opt".into(),
                last_name: "Out".into(),
                accepts_marketing: Some(false),
                ..Default::default()
            })
            .unwrap();

        let resp = app
            .oneshot(Request::get("/customers?accepts_marketing=true").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["total"], 1);
        assert_eq!(json["customers"][0]["email"], "opt-in@test.com");
    }

    #[tokio::test]
    async fn update_customer_succeeds() {
        let (app, state) = app_with_state();
        let customer = state
            .commerce()
            .customers()
            .create(stateset_core::CreateCustomer {
                email: "update-me@test.com".into(),
                first_name: "Old".into(),
                last_name: "Name".into(),
                phone: None,
                accepts_marketing: None,
                tags: None,
                metadata: None,
            })
            .unwrap();

        let body = serde_json::json!({ "first_name": "New", "last_name": "Updated" });
        let resp = app
            .oneshot(
                Request::patch(format!("/customers/{}", customer.id))
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["first_name"], "New");
        assert_eq!(json["last_name"], "Updated");
    }

    #[tokio::test]
    async fn update_nonexistent_customer_returns_error() {
        let id = CustomerId::new();
        let body = serde_json::json!({ "first_name": "Ghost" });
        let resp = app()
            .oneshot(
                Request::patch(format!("/customers/{id}"))
                    .header("content-type", "application/json")
                    .body(Body::from(serde_json::to_vec(&body).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert!(resp.status().is_client_error());
    }

    #[tokio::test]
    async fn delete_customer_returns_204() {
        let (app, state) = app_with_state();
        let customer = state
            .commerce()
            .customers()
            .create(stateset_core::CreateCustomer {
                email: "delete-me@test.com".into(),
                first_name: "Delete".into(),
                last_name: "Me".into(),
                phone: None,
                accepts_marketing: None,
                tags: None,
                metadata: None,
            })
            .unwrap();

        let resp = app
            .oneshot(
                Request::delete(format!("/customers/{}", customer.id)).body(Body::empty()).unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
    }

    #[tokio::test]
    async fn delete_nonexistent_customer_is_idempotent() {
        let id = CustomerId::new();
        let resp = app()
            .oneshot(Request::delete(format!("/customers/{id}")).body(Body::empty()).unwrap())
            .await
            .unwrap();
        // Idempotent delete: non-existent resources should not cause a server error
        assert!(resp.status().is_success() || resp.status().is_client_error());
    }
}