Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(email_service): active backfill endpoint (#72)
Evan Hutnik1e9eb584502c
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-460e4c3dcd4adba352eb7c00381bac9d3c4b8b107dd02fd35ec991e848919eed.json b/rust/cloud-storage/email_db_client/.sqlx/query-460e4c3dcd4adba352eb7c00381bac9d3c4b8b107dd02fd35ec991e848919eed.jsonnew file mode 100644index 0000000000..f875ee7641--- /dev/null+++ b/rust/cloud-storage/email_db_client/.sqlx/query-460e4c3dcd4adba352eb7c00381bac9d3c4b8b107dd02fd35ec991e848919eed.json@@ -0,0 +1,83 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n id,\n link_id,\n fusionauth_user_id,\n threads_requested_limit,\n total_threads,\n threads_retrieved_count,\n status as \"status: db::backfill::BackfillJobStatus\",\n created_at,\n updated_at\n FROM email_backfill_jobs\n WHERE link_id = $1 AND status IN ('Init', 'InProgress')\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "link_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 2,+ "name": "fusionauth_user_id",+ "type_info": "Text"+ },+ {+ "ordinal": 3,+ "name": "threads_requested_limit",+ "type_info": "Int4"+ },+ {+ "ordinal": 4,+ "name": "total_threads",+ "type_info": "Int4"+ },+ {+ "ordinal": 5,+ "name": "threads_retrieved_count",+ "type_info": "Int4"+ },+ {+ "ordinal": 6,+ "name": "status: db::backfill::BackfillJobStatus",+ "type_info": {+ "Custom": {+ "name": "email_backfill_job_status",+ "kind": {+ "Enum": [+ "Init",+ "InProgress",+ "Complete",+ "Cancelled",+ "Failed"+ ]+ }+ }+ }+ },+ {+ "ordinal": 7,+ "name": "created_at",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 8,+ "name": "updated_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false,+ true,+ false,+ true,+ false,+ false,+ false,+ false,+ false+ ]+ },+ "hash": "460e4c3dcd4adba352eb7c00381bac9d3c4b8b107dd02fd35ec991e848919eed"+}diff --git a/rust/cloud-storage/email_db_client/src/backfill/job/get.rs b/rust/cloud-storage/email_db_client/src/backfill/job/get.rsindex e36706f0e6..11d3f2504b 100644--- a/rust/cloud-storage/email_db_client/src/backfill/job/get.rs+++ b/rust/cloud-storage/email_db_client/src/backfill/job/get.rs@@ -67,6 +67,36 @@ pub async fn get_backfill_job_with_link_id(Ok(record.map(Into::into))}+#[tracing::instrument(skip(pool), err)]+pub async fn get_active_backfill_job(+ pool: &PgPool,+ link_id: Uuid,+) -> anyhow::Result<Option<service::backfill::BackfillJob>> {+ let record = sqlx::query_as!(+ db::backfill::BackfillJob,+ r#"+ SELECT+ id,+ link_id,+ fusionauth_user_id,+ threads_requested_limit,+ total_threads,+ threads_retrieved_count,+ status as "status: db::backfill::BackfillJobStatus",+ created_at,+ updated_at+ FROM email_backfill_jobs+ WHERE link_id = $1 AND status IN ('Init', 'InProgress')+ "#,+ link_id+ )+ .fetch_optional(pool)+ .await+ .with_context(|| "Failed to query for active backfill job".to_string())?;++ Ok(record.map(Into::into))+}+/// Retrieves all backfill jobs created in the last 24 hours for a given macro ID#[tracing::instrument(skip(pool), level = "info")]pub async fn get_recent_jobs_by_fusionauth_user_id(diff --git a/rust/cloud-storage/email_service/src/api/email/backfill/get.rs b/rust/cloud-storage/email_service/src/api/email/backfill/get.rsindex 24a8510f8a..acc2798d45 100644--- a/rust/cloud-storage/email_service/src/api/email/backfill/get.rs+++ b/rust/cloud-storage/email_service/src/api/email/backfill/get.rs@@ -5,11 +5,13 @@ use axum::{http::StatusCode,response::{IntoResponse, Json, Response},};-use model::response::ErrorResponse;+use model::response::{EmptyResponse, ErrorResponse};use model::user::UserContext;use models_email::email::service::backfill::BackfillJob;use models_email::email::service::link::Link;use sqlx::types::Uuid;+use strum_macros::AsRefStr;+use thiserror::Error;use utoipa::ToSchema;/// The response returned from the get backfill job endpoint@@ -28,8 +30,8 @@ pub struct GetBackfillJobResponse {("id" = Uuid, Path, description = "Job ID."),),responses(- (status = 201, body=GetBackfillJobResponse),- (status = 429, body=ErrorResponse),+ (status = 200, body=GetBackfillJobResponse),+ (status = 401, body=ErrorResponse),(status = 404, body=ErrorResponse),(status = 500, body=ErrorResponse),)@@ -72,3 +74,59 @@ pub async fn handler(Ok(Json(GetBackfillJobResponse { job }).into_response())}++/// The response returned from the get backfill job endpoint+#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]+pub struct GetActiveBackfillJobResponse {+ pub job: BackfillJob,+}++/// Get any active backfill job for the user.+#[utoipa::path(+ get,+ tag = "Init",+ path = "/email/backfill/gmail/active",+ operation_id = "get_backfill_gmail_active",+ responses(+ (status = 200, body=GetActiveBackfillJobResponse),+ (status = 204, body=EmptyResponse),+ (status = 401, body=ErrorResponse),+ (status = 500, body=ErrorResponse),+ )+)]+#[tracing::instrument(skip(ctx), err)]+pub async fn active_handler(+ State(ctx): State<ApiContext>,+ link: Extension<Link>,+) -> Result<Response, GetActiveBackfillError> {+ let job = email_db_client::backfill::job::get::get_active_backfill_job(&ctx.db, link.id)+ .await+ .map_err(GetActiveBackfillError::QueryError)?;++ match job {+ Some(job) => Ok(Json(GetActiveBackfillJobResponse { job }).into_response()),+ None => Ok(StatusCode::NO_CONTENT.into_response()),+ }+}++#[derive(Debug, Error, AsRefStr)]+pub enum GetActiveBackfillError {+ #[error("Failed to get active backfill job from database")]+ QueryError(#[from] anyhow::Error),+}++impl IntoResponse for GetActiveBackfillError {+ fn into_response(self) -> Response {+ let status_code = match self {+ GetActiveBackfillError::QueryError(_) => StatusCode::INTERNAL_SERVER_ERROR,+ };++ (+ status_code,+ Json(ErrorResponse {+ message: self.to_string().as_str(),+ }),+ )+ .into_response()+ }+}diff --git a/rust/cloud-storage/email_service/src/api/email/backfill/mod.rs b/rust/cloud-storage/email_service/src/api/email/backfill/mod.rsindex fec1351ec2..bdb0c46534 100644--- a/rust/cloud-storage/email_service/src/api/email/backfill/mod.rs+++ b/rust/cloud-storage/email_service/src/api/email/backfill/mod.rs@@ -10,6 +10,7 @@ pub fn router(state: ApiContext) -> Router<ApiContext> {Router::new().route("/gmail", delete(cancel::handler)).route("/gmail/:id", get(get::handler))+ .route("/gmail/active", get(get::active_handler)).layer(axum::middleware::from_fn_with_state(state,crate::api::middleware::link::attach_link_context,diff --git a/rust/cloud-storage/email_service/src/api/email/init.rs b/rust/cloud-storage/email_service/src/api/email/init.rsindex e76c4f47d5..f7a3aee221 100644--- a/rust/cloud-storage/email_service/src/api/email/init.rs+++ b/rust/cloud-storage/email_service/src/api/email/init.rs@@ -68,9 +68,9 @@ impl IntoResponse for InitError {}#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]-struct InitResponse {- link_id: Uuid,- backfill_job_id: Uuid,+pub struct InitResponse {+ pub link_id: Uuid,+ pub backfill_job_id: Uuid,}/// Initialize email functionality for the user. Populates initial threads and enables inbox syncing.diff --git a/rust/cloud-storage/email_service/src/api/swagger.rs b/rust/cloud-storage/email_service/src/api/swagger.rsindex 651b5f5711..c4af7cb844 100644--- a/rust/cloud-storage/email_service/src/api/swagger.rs+++ b/rust/cloud-storage/email_service/src/api/swagger.rs@@ -1,18 +1,32 @@use crate::api::email::attachments::get::GetAttachmentResponse;+use crate::api::email::backfill::cancel::CancelBackfillParams;+use crate::api::email::backfill::get::{GetActiveBackfillJobResponse, GetBackfillJobResponse};use crate::api::email::contacts::list::ListContactsResponse;+use crate::api::email::drafts::create::{CreateDraftRequest, CreateDraftResponse};+use crate::api::email::init::InitResponse;use crate::api::email::labels::create::CreateLabelRequest;use crate::api::email::labels::create::CreateLabelResponse;use crate::api::email::labels::list::ListLabelsResponse;use crate::api::email::links::list::ListLinksResponse;+use crate::api::email::messages::labels::{UpdateLabelBatchRequest, UpdateLabelBatchResponse};+use crate::api::email::messages::send::{SendMessageRequest, SendMessageResponse};+use crate::api::email::threads::archived::ArchiveThreadRequest;use crate::api::email::threads::get::GetThreadResponse;-use crate::api::email::threads::previews::cursor::GetPreviewsFrecencyResponse;+use crate::api::email::threads::previews::cursor::{+ GetPreviewsFrecencyResponse, ThreadPreviewFrecency, ThreadPreviewPagination,+};use crate::api::{email, health};use model::response::EmptyResponse;use models_email::email::service;+use models_email::email::service::address::ContactInfoWithInteraction;+use models_email::email::service::backfill::BackfillJob;+use models_email::email::service::link::Link;use models_email::email::service::thread::{- GetPreviewsCursorResponse, PreviewView, PreviewViewStandardLabel,+ GetPreviewsCursorParams, GetPreviewsCursorResponse, PreviewView, PreviewViewStandardLabel,};-use models_email::service::thread::ApiSortMethod;+use models_email::service::label::Label;+use models_email::service::message::{MessageToSend, ParsedMessage};+use models_email::service::thread::{APIThread, ApiSortMethod, ThreadPreviewCursor};use utoipa::OpenApi;#[derive(OpenApi)]@@ -25,6 +39,7 @@ use utoipa::OpenApi;email::attachments::get::handler,email::backfill::cancel::handler,email::backfill::get::handler,+ email::backfill::get::active_handler,email::init::handler,email::drafts::create::handler,email::drafts::delete::handler,@@ -48,18 +63,52 @@ use utoipa::OpenApi;components(schemas(EmptyResponse,+ // Backfill types+ CancelBackfillParams,+ GetBackfillJobResponse,+ GetActiveBackfillJobResponse,+ BackfillJob,+ // Draft types+ CreateDraftRequest,+ CreateDraftResponse,+ // Init types+ InitResponse,+ // Label typesCreateLabelRequest,CreateLabelResponse,ListLabelsResponse,+ Label,+ // Message types+ UpdateLabelBatchRequest,+ UpdateLabelBatchResponse,+ SendMessageRequest,+ SendMessageResponse,+ ParsedMessage,+ MessageToSend,+ // Thread typesGetThreadResponse,+ ArchiveThreadRequest,+ APIThread,+ ThreadPreviewCursor,+ // Preview types+ GetPreviewsCursorParams,GetPreviewsCursorResponse,GetPreviewsFrecencyResponse,+ ThreadPreviewFrecency,+ ThreadPreviewPagination,+ PreviewView,+ PreviewViewStandardLabel,+ // Attachment typesGetAttachmentResponse,+ // Link typesListLinksResponse,+ Link,+ // Contact typesListContactsResponse,+ ContactInfoWithInteraction,+ // Sort/filter typesApiSortMethod,- PreviewViewStandardLabel,- PreviewView,+ // Legacy service types (keeping for backward compatibility)service::thread::ThreadList,service::address::ContactInfo,service::label::LabelInfo,