Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.3 KB
Raw Blame
use std::str::FromStr;

use crate::api::context::{AuthorizationService, EntityAccessService};
use crate::model::response::annotations::AnchorResponse;
use axum::{
    Json,
    extract::{Extension, Path, State},
    http::StatusCode,
    response::{IntoResponse, Response},
};
use entity_access::inbound::axum_extractors::DocumentAccessExtractor;
use macro_db_client::annotations::get::get_pdf_anchors;
use model::{
    annotations::Anchor,
    document::{DocumentBasic, FileType},
    response::ErrorResponse,
};
use models_permissions::share_permission::access_level::ViewAccessLevel;
use sqlx::PgPool;

use super::annotation_error_response;

#[derive(serde::Deserialize)]
pub struct Params {
    pub document_id: String,
}

/// Gets a set of comment anchors for a document
#[utoipa::path(
        get,
        path = "/annotations/anchors/document/{document_id}",
        operation_id = "get_document_anchors",
        params(
            ("document_id" = String, Path, description = "Document ID")
        ),
        responses(
            (status = 200, body=AnchorResponse),
            (status = 401, body=ErrorResponse),
            (status = 404, body=ErrorResponse),
            (status = 500, body=ErrorResponse),
        )
    )]
#[tracing::instrument(err(Debug), skip(_access, db))]
pub async fn get_document_anchors_handler(
    _access: DocumentAccessExtractor<ViewAccessLevel, EntityAccessService, AuthorizationService>,
    Path(Params { document_id }): Path<Params>,
    State(db): State<PgPool>,
    document_context: Extension<DocumentBasic>,
) -> Result<Response, Response> {
    match document_context
        .file_type
        .as_deref()
        .and_then(|f| FileType::from_str(f).ok())
    {
        Some(FileType::Pdf | FileType::Docx) => match get_pdf_anchors(&db, &document_id).await {
            Ok(pdf_anchors) => {
                let anchors: Vec<Anchor> = pdf_anchors.into_iter().map(Anchor::Pdf).collect();
                Ok((StatusCode::OK, Json(AnchorResponse { data: anchors })).into_response())
            }
            Err(e) => Err(annotation_error_response(e, "Error retrieving anchors")),
        },
        _ => Err((
            StatusCode::METHOD_NOT_ALLOWED,
            Json(ErrorResponse {
                message: "Unsupported file type".into(),
            }),
        )
            .into_response()),
    }
}