Skip to main content

docbox_http/routes/
task.rs

1//! # Tasks
2//!
3//! Endpoints related to background tasks
4
5use crate::{
6    error::{HttpCommonError, HttpErrorResponse, HttpResult},
7    middleware::tenant::{TenantDb, TenantParams},
8    models::{document_box::DocumentBoxScope, task::HttpTaskError},
9};
10use axum::{Json, extract::Path};
11use docbox_core::database::models::tasks::{Task, TaskId};
12
13pub const TASK_TAG: &str = "Task";
14
15/// Get task by ID
16///
17/// Get the details about a specific task, used to poll
18/// the current progress of a task
19#[utoipa::path(
20    get,
21    operation_id = "task_get",
22    tag = TASK_TAG,
23    path = "/box/{scope}/task/{task_id}",
24    responses(
25        (status = 200, description = "Task found successfully", body = Task),
26        (status = 404, description = "Task not found", body = HttpErrorResponse),
27        (status = 500, description = "Internal server error", body = HttpErrorResponse)
28    ),
29    params(
30        ("scope" = String, Path, description = "Scope the task is within"),
31        ("task_id" = Uuid, Path, description = "ID of the task to query"),
32        TenantParams
33    )
34)]
35#[tracing::instrument(skip_all, fields(%scope, %task_id))]
36pub async fn get(
37    TenantDb(db): TenantDb,
38    Path((scope, task_id)): Path<(DocumentBoxScope, TaskId)>,
39) -> HttpResult<Task> {
40    let DocumentBoxScope(scope) = scope;
41
42    let task = Task::find(&db, task_id, &scope)
43        .await
44        // Failed to query the database
45        .map_err(|error| {
46            tracing::error!(?scope, ?task_id, ?error, "failed to query task");
47            HttpCommonError::ServerError
48        })?
49        // Task not found
50        .ok_or(HttpTaskError::UnknownTask)?;
51
52    Ok(Json(task))
53}