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.

4.5 KB
Raw Blame
use models_email::email::db;
use models_email::email::service;
use sqlx::PgPool;
use sqlx::types::Uuid;

#[tracing::instrument(skip(pool), err)]
pub async fn get_backfill_job(
    pool: &PgPool,
    job_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 id = $1
        "#,
        job_id
    )
    .fetch_optional(pool)
    .await?;

    Ok(record.map(Into::into))
}

#[tracing::instrument(skip(pool), err)]
pub async fn get_backfill_job_with_link_id(
    pool: &PgPool,
    job_id: Uuid,
    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 id = $1
        AND link_id = $2
        "#,
        job_id,
        link_id
    )
    .fetch_optional(pool)
    .await?;

    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?;

    Ok(record.map(Into::into))
}

/// Retrieves the most recent backfill job for a link, regardless of status.
/// Used to derive the inbox sync hint shown in multi-inbox settings.
#[tracing::instrument(skip(pool), err)]
pub async fn get_latest_backfill_job_by_link_id(
    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
        ORDER BY created_at DESC
        LIMIT 1
        "#,
        link_id
    )
    .fetch_optional(pool)
    .await?;

    Ok(record.map(Into::into))
}

/// Retrieves a user's most recent backfill jobs, newest first. Spans all of the
/// user's links and all statuses; `link_id` is intentionally not used so jobs
/// survive a link being deleted and recreated. Capped at 100 so frequent
/// resyncs can't grow the response unbounded — the settings UI only needs the
/// current per-link state, which the newest jobs cover.
#[tracing::instrument(skip(pool), err)]
pub async fn get_all_jobs_by_fusionauth_user_id(
    pool: &PgPool,
    fusionauth_user_id: &str,
) -> anyhow::Result<Vec<service::backfill::BackfillJob>> {
    let records = 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 fusionauth_user_id = $1
        ORDER BY created_at DESC
        LIMIT 100
        "#,
        fusionauth_user_id
    )
    .fetch_all(pool)
    .await?;

    // Convert all database records to service models
    let jobs = records.into_iter().map(Into::into).collect();

    Ok(jobs)
}

#[cfg(test)]
mod test;