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.

6.6 KB
Raw Blame
use crate::api::{ApiContext, context::AuthorizationService};
use axum::{
    extract::State,
    http::StatusCode,
    response::{IntoResponse, Json, Response},
};
use macro_authorization::{InternalOnly, MacroAuthorizationExtractor};
use model::response::ErrorResponse;
use models_email::email::service::backfill::{
    BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
};
use sqlx::types::Uuid;
use utoipa::ToSchema;

#[derive(serde::Serialize, serde::Deserialize, Debug, ToSchema)]
pub struct BackfillParams {
    pub link_ids: Vec<Uuid>,
    pub num_threads: Option<i32>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct BackfillResponse {
    pub pairs: Vec<LinkJobPair>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct LinkJobPair {
    pub link_id: Uuid,
    pub job_id: Uuid,
}

/// Internal endpoint to backfill email threads for users.
#[tracing::instrument(skip(ctx))]
pub async fn handler(
    State(ctx): State<ApiContext>,
    _: MacroAuthorizationExtractor<AuthorizationService, InternalOnly>,
    Json(req_body): Json<BackfillParams>,
) -> Result<Response, Response> {
    let mut link_job_pairs: Vec<LinkJobPair> = Vec::new();

    for link_id in req_body.link_ids {
        let link = email_db_client::links::get::fetch_link_by_id(&ctx.db, link_id)
            .await
            .map_err(|e| {
                tracing::warn!(error=?e, "error fetching link for backfill");
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    Json(ErrorResponse {
                        message: format!("error fetching link for id {}", link_id).into(),
                    }),
                )
                    .into_response()
            })?
            .ok_or_else(|| {
                tracing::warn!("link id not found for backfill: {}", link_id);
                (
                    StatusCode::BAD_REQUEST,
                    Json(ErrorResponse {
                        message: "link id does not exist".into(),
                    }),
                )
                    .into_response()
            })?;

        if !link.is_sync_active {
            return Err((
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    message: format!("sync must be enabled for link {}", link_id).into(),
                }),
            )
                .into_response());
        }

        let backfill_job = email_db_client::backfill::job::insert::create_backfill_job(
            &ctx.db,
            link.id,
            link.fusionauth_user_id.as_str(),
            req_body.num_threads,
            false,
        )
        .await
        .map_err(|e| {
            tracing::warn!(error=?e, "error creating backfill_job");
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    message: format!("error creating backfill job for link {}", link_id).into(),
                }),
            )
                .into_response()
        })?;

        let Some(backfill_job) = backfill_job else {
            // An active backfill already exists for this link; reuse it and skip re-enqueue.
            let existing =
                email_db_client::backfill::job::get::get_active_backfill_job(&ctx.db, link.id)
                    .await
                    .map_err(|e| {
                        tracing::warn!(error=?e, "error fetching active backfill_job");
                        (
                            StatusCode::INTERNAL_SERVER_ERROR,
                            Json(ErrorResponse {
                                message: format!(
                                    "error fetching active backfill job for link {}",
                                    link_id
                                )
                                .into(),
                            }),
                        )
                            .into_response()
                    })?
                    .ok_or_else(|| {
                        (
                            StatusCode::INTERNAL_SERVER_ERROR,
                            Json(ErrorResponse {
                                message: format!(
                                    "backfill conflict but no active job for link {}",
                                    link_id
                                )
                                .into(),
                            }),
                        )
                            .into_response()
                    })?;
            link_job_pairs.push(LinkJobPair {
                link_id,
                job_id: existing.id,
            });
            continue;
        };

        link_job_pairs.push(LinkJobPair {
            link_id,
            job_id: backfill_job.id,
        });

        let ps_message = BackfillPubsubMessage {
            backfill_operation: BackfillOperation::Init(JobScopedPayload {
                link_id: link.id,
                job_id: backfill_job.id,
                payload: InitPayload {},
            }),
        };

        if let Err(error) = ctx
            .sqs_client
            .enqueue_email_backfill_message(ps_message)
            .await
        {
            tracing::error!(error = ?error, backfill_id = %backfill_job.id, "Failed to enqueue backfill message");
            email_db_client::backfill::job::update::fail_backfill_job(&ctx.db, backfill_job.id)
                .await
                .inspect_err(|update_error| {
                    tracing::error!(
                        error = ?update_error,
                        backfill_id = %backfill_job.id,
                        "Failed to persist backfill publication failure"
                    );
                })
                .map_err(|_| {
                    (
                        StatusCode::INTERNAL_SERVER_ERROR,
                        Json(ErrorResponse {
                            message: format!(
                                "Failed to clean up backfill job for link {}",
                                link_id
                            )
                            .into(),
                        }),
                    )
                        .into_response()
                })?;

            return Err((
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    message: format!("Failed to enqueue backfill message for link {}", link_id)
                        .into(),
                }),
            )
                .into_response());
        }
    }
    Ok((
        StatusCode::OK,
        Json(BackfillResponse {
            pairs: link_job_pairs,
        }),
    )
        .into_response())
}