Skip to main content

tmf677_usage/
handlers.rs

1//! Request handlers for TMF677 API endpoints
2
3use crate::auth::validate_token;
4use crate::db;
5use crate::models::*;
6use actix_web::{web, HttpResponse, Result as ActixResult};
7use sqlx::PgPool;
8use tmf_apis_core::TmfError;
9use uuid::Uuid;
10
11/// Get all customer usages
12#[utoipa::path(
13    get,
14    path = "/tmf-api/usageConsumptionManagement/v4/usageConsumption",
15    responses(
16        (status = 200, description = "List of customer usages", body = Vec<CustomerUsage>),
17        (status = 401, description = "Unauthorized")
18    ),
19    tag = "TMF677"
20)]
21pub async fn get_usages(
22    pool: web::Data<PgPool>,
23    req: actix_web::HttpRequest,
24) -> ActixResult<HttpResponse> {
25    validate_token(&req)?;
26
27    match db::get_usages(pool.get_ref()).await {
28        Ok(usages) => Ok(HttpResponse::Ok().json(usages)),
29        Err(e) => Ok(HttpResponse::InternalServerError().json(serde_json::json!({
30            "error": e.to_string()
31        }))),
32    }
33}
34
35/// Get customer usage by ID
36#[utoipa::path(
37    get,
38    path = "/tmf-api/usageConsumptionManagement/v4/usageConsumption/{id}",
39    responses(
40        (status = 200, description = "Customer usage found", body = CustomerUsage),
41        (status = 404, description = "Customer usage not found"),
42        (status = 400, description = "Invalid usage ID"),
43        (status = 401, description = "Unauthorized")
44    ),
45    params(
46        ("id" = String, Path, description = "Customer Usage ID (UUID)")
47    ),
48    tag = "TMF677"
49)]
50pub async fn get_usage_by_id(
51    pool: web::Data<PgPool>,
52    req: actix_web::HttpRequest,
53    path: web::Path<String>,
54) -> ActixResult<HttpResponse> {
55    validate_token(&req)?;
56
57    let id = match Uuid::parse_str(&path.into_inner()) {
58        Ok(uuid) => uuid,
59        Err(_) => {
60            return Ok(HttpResponse::BadRequest().json(serde_json::json!({
61                "error": "Invalid customer usage ID format. Expected UUID."
62            })));
63        }
64    };
65
66    match db::get_usage_by_id(pool.get_ref(), id).await {
67        Ok(usage) => Ok(HttpResponse::Ok().json(usage)),
68        Err(TmfError::NotFound(msg)) => Ok(HttpResponse::NotFound().json(serde_json::json!({
69            "error": msg
70        }))),
71        Err(e) => Ok(HttpResponse::InternalServerError().json(serde_json::json!({
72            "error": e.to_string()
73        }))),
74    }
75}
76
77/// Create a new customer usage record
78#[utoipa::path(
79    post,
80    path = "/tmf-api/usageConsumptionManagement/v4/usageConsumption",
81    request_body = CreateCustomerUsageRequest,
82    responses(
83        (status = 201, description = "Customer usage created", body = CustomerUsage),
84        (status = 400, description = "Invalid request"),
85        (status = 401, description = "Unauthorized")
86    ),
87    tag = "TMF677"
88)]
89pub async fn create_usage(
90    pool: web::Data<PgPool>,
91    req: actix_web::HttpRequest,
92    body: web::Json<CreateCustomerUsageRequest>,
93) -> ActixResult<HttpResponse> {
94    validate_token(&req)?;
95
96    match db::create_usage(pool.get_ref(), body.into_inner()).await {
97        Ok(usage) => Ok(HttpResponse::Created().json(usage)),
98        Err(e) => Ok(HttpResponse::InternalServerError().json(serde_json::json!({
99            "error": e.to_string()
100        }))),
101    }
102}