diff options
Diffstat (limited to 'makima/src/server/handlers')
| -rw-r--r-- | makima/src/server/handlers/orders.rs | 85 |
1 files changed, 10 insertions, 75 deletions
diff --git a/makima/src/server/handlers/orders.rs b/makima/src/server/handlers/orders.rs index c43c406..cddf6a6 100644 --- a/makima/src/server/handlers/orders.rs +++ b/makima/src/server/handlers/orders.rs @@ -11,7 +11,7 @@ use axum::{ use uuid::Uuid; use crate::db::models::{ - ConvertToStepRequest, CreateOrderRequest, DirectiveStep, LinkContractRequest, + CreateOrderRequest, DirectiveStep, LinkDirectiveRequest, Order, OrderListQuery, OrderListResponse, UpdateOrderRequest, }; use crate::db::repository; @@ -32,7 +32,7 @@ use crate::server::state::SharedState; ("type" = Option<String>, Query, description = "Filter by order type"), ("priority" = Option<String>, Query, description = "Filter by priority"), ("directive_id" = Option<Uuid>, Query, description = "Filter by directive ID"), - ("contract_id" = Option<Uuid>, Query, description = "Filter by contract ID"), + ("search" = Option<String>, Query, description = "Text search across title, description, and directive name"), ), responses( (status = 200, description = "List of orders", body = OrderListResponse), @@ -62,7 +62,7 @@ pub async fn list_orders( query.order_type.as_deref(), query.priority.as_deref(), query.directive_id, - query.contract_id, + query.search.as_deref(), ) .await { @@ -327,80 +327,13 @@ pub async fn link_to_directive( } } -/// Link an order to a contract. -#[utoipa::path( - post, - path = "/api/v1/orders/{id}/link-contract", - params(("id" = Uuid, Path, description = "Order ID")), - request_body = LinkContractRequest, - responses( - (status = 200, description = "Order linked to contract", body = Order), - (status = 404, description = "Not found", body = ApiError), - (status = 401, description = "Unauthorized", body = ApiError), - (status = 503, description = "Database not configured", body = ApiError), - ), - security(("bearer_auth" = []), ("api_key" = [])), - tag = "Orders" -)] -pub async fn link_to_contract( - State(state): State<SharedState>, - Authenticated(auth): Authenticated, - Path(id): Path<Uuid>, - Json(req): Json<LinkContractRequest>, -) -> impl IntoResponse { - let Some(ref pool) = state.db_pool else { - return ( - StatusCode::SERVICE_UNAVAILABLE, - Json(ApiError::new("DB_UNAVAILABLE", "Database not configured")), - ) - .into_response(); - }; - - // Verify the contract exists and belongs to this owner - match repository::get_contract_for_owner(pool, auth.owner_id, req.contract_id).await { - Ok(Some(_)) => {} - Ok(None) => { - return ( - StatusCode::NOT_FOUND, - Json(ApiError::new("NOT_FOUND", "Contract not found")), - ) - .into_response(); - } - Err(e) => { - return ( - StatusCode::INTERNAL_SERVER_ERROR, - Json(ApiError::new("GET_FAILED", &e.to_string())), - ) - .into_response(); - } - } - - match repository::link_order_to_contract(pool, auth.owner_id, id, req.contract_id).await { - Ok(Some(order)) => Json(order).into_response(), - Ok(None) => ( - StatusCode::NOT_FOUND, - Json(ApiError::new("NOT_FOUND", "Order not found")), - ) - .into_response(), - Err(e) => { - tracing::error!("Failed to link order to contract: {}", e); - ( - StatusCode::INTERNAL_SERVER_ERROR, - Json(ApiError::new("LINK_FAILED", &e.to_string())), - ) - .into_response() - } - } -} - /// Convert an order to a directive step. -/// Creates a new step in the specified directive using the order's title/description, -/// and links the order to both the directive and the new step. +/// Creates a new step in the order's linked directive using the order's title/description, +/// and links the order to the new step. The order must have a directive_id set. #[utoipa::path( post, path = "/api/v1/orders/{id}/convert-to-step", params(("id" = Uuid, Path, description = "Order ID")), - request_body = ConvertToStepRequest, responses( (status = 201, description = "Directive step created from order", body = DirectiveStep), (status = 404, description = "Order or directive not found", body = ApiError), @@ -414,7 +347,6 @@ pub async fn convert_to_step( State(state): State<SharedState>, Authenticated(auth): Authenticated, Path(id): Path<Uuid>, - Json(req): Json<ConvertToStepRequest>, ) -> impl IntoResponse { let Some(ref pool) = state.db_pool else { return ( @@ -424,11 +356,14 @@ pub async fn convert_to_step( .into_response(); }; - match repository::convert_order_to_step(pool, auth.owner_id, id, req.directive_id).await { + match repository::convert_order_to_step(pool, auth.owner_id, id).await { Ok(Some(step)) => (StatusCode::CREATED, Json(step)).into_response(), Ok(None) => ( StatusCode::NOT_FOUND, - Json(ApiError::new("NOT_FOUND", "Order or directive not found")), + Json(ApiError::new( + "NOT_FOUND", + "Order not found or has no linked directive", + )), ) .into_response(), Err(e) => { |
