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.1 KB
Raw Blame
use crate::api::context::{ApiContext, AuthorizationService};
use axum::{
    Json,
    extract::{Path, Query, State},
    http::StatusCode,
    response::{IntoResponse, Response},
};
use email::domain::{models::EmailErr, ports::EmailService};
use email::inbound::axum::api_types::DraftEditPrecondition;
use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};
use model::response::{EmptyResponse, ErrorResponse};
use uuid::Uuid;

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct DeleteDraftError(#[from] EmailErr);
impl IntoResponse for DeleteDraftError {
    fn into_response(self) -> Response {
        let status = match &self.0 {
            EmailErr::Unauthorized => StatusCode::FORBIDDEN,
            EmailErr::MessageAlreadySent(_) | EmailErr::MessageDeliveryConflict(_) => {
                StatusCode::CONFLICT
            }
            _ => StatusCode::INTERNAL_SERVER_ERROR,
        };
        (
            status,
            Json(ErrorResponse {
                message: self.to_string().into(),
            }),
        )
            .into_response()
    }
}

/// Delete a draft.
#[utoipa::path(
    delete,
    tag = "Drafts",
    path = "/email/drafts/{id}",
    operation_id = "delete_draft",
    params(
        DraftEditPrecondition,
        ("id" = Uuid, Path, description = "Draft ID."),
    ),
    responses(
            (status = 204, body=EmptyResponse),
            (status = 400, body=ErrorResponse),
            (status = 401, body=ErrorResponse),
            (status = 404, body=ErrorResponse),
            (status = 500, body=ErrorResponse),
    )
)]
#[tracing::instrument(skip(ctx, authorization), err)]
pub async fn handler(
    Query(precondition): Query<DraftEditPrecondition>,
    State(ctx): State<ApiContext>,
    authorization: MacroAuthorizationExtractor<AuthorizationService, UserOrInternal>,
    Path(draft_id): Path<Uuid>,
) -> Result<StatusCode, DeleteDraftError> {
    ctx.email_service
        .service()
        .delete_draft_for_user(
            authorization.authorization.user.macro_user_id,
            draft_id,
            precondition.draft_generation,
        )
        .await?;
    Ok(StatusCode::NO_CONTENT)
}