diff options
Diffstat (limited to 'makima/src/server/handlers')
| -rw-r--r-- | makima/src/server/handlers/directives.rs | 72 |
1 files changed, 67 insertions, 5 deletions
diff --git a/makima/src/server/handlers/directives.rs b/makima/src/server/handlers/directives.rs index f624d82..585899e 100644 --- a/makima/src/server/handlers/directives.rs +++ b/makima/src/server/handlers/directives.rs @@ -10,10 +10,10 @@ use serde::Deserialize; use uuid::Uuid; use crate::db::models::{ - BatchSetDirectiveMemoryRequest, CreateDirectiveRequest, CreateDirectiveStepRequest, - Directive, DirectiveListResponse, DirectiveMemory, DirectiveMemoryListResponse, - DirectiveStep, DirectiveWithSteps, SetDirectiveMemoryRequest, UpdateDirectiveRequest, - UpdateDirectiveStepRequest, UpdateGoalRequest, + BatchSetDirectiveMemoryRequest, CleanupTasksResponse, CreateDirectiveRequest, + CreateDirectiveStepRequest, Directive, DirectiveListResponse, DirectiveMemory, + DirectiveMemoryListResponse, DirectiveStep, DirectiveWithSteps, SetDirectiveMemoryRequest, + UpdateDirectiveRequest, UpdateDirectiveStepRequest, UpdateGoalRequest, }; use crate::db::repository; use crate::server::auth::Authenticated; @@ -1090,7 +1090,7 @@ pub async fn batch_set_memories( } } - match repository::batch_set_directive_memories(pool, id, &req.memories).await { + match repository::batch_set_directive_memories(pool, id, &req.entries).await { Ok(memories) => Json(memories).into_response(), Err(e) => { tracing::error!("Failed to batch set memories: {}", e); @@ -1226,3 +1226,65 @@ pub async fn clear_memories( } } } + +// ============================================================================= +// Task Cleanup +// ============================================================================= + +/// Clean up terminal tasks associated with a directive. +#[utoipa::path( + post, + path = "/api/v1/directives/{id}/cleanup-tasks", + params(("id" = Uuid, Path, description = "Directive ID")), + responses( + (status = 200, description = "Tasks cleaned up", body = CleanupTasksResponse), + (status = 404, description = "Not found", body = ApiError), + (status = 503, description = "Database not configured", body = ApiError), + ), + security(("bearer_auth" = []), ("api_key" = [])), + tag = "Directives" +)] +pub async fn cleanup_tasks( + State(state): State<SharedState>, + Authenticated(auth): Authenticated, + Path(id): Path<Uuid>, +) -> 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 directive ownership + match repository::get_directive_for_owner(pool, auth.owner_id, id).await { + Ok(Some(_)) => {} + Ok(None) => { + return ( + StatusCode::NOT_FOUND, + Json(ApiError::new("NOT_FOUND", "Directive not found")), + ) + .into_response(); + } + Err(e) => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(ApiError::new("GET_FAILED", &e.to_string())), + ) + .into_response(); + } + } + + match repository::cleanup_directive_tasks(pool, auth.owner_id, id).await { + Ok(deleted) => Json(CleanupTasksResponse { deleted }).into_response(), + Err(e) => { + tracing::error!("Failed to cleanup directive tasks: {}", e); + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(ApiError::new("CLEANUP_FAILED", &e.to_string())), + ) + .into_response() + } + } +} |
